More entropy

This commit is contained in:
Denis Zheleztsov 2015-07-10 12:55:59 +03:00
parent b9a9ecf3ef
commit d3e5db8305

View File

@ -7,6 +7,9 @@ use utf8;
use Database; use Database;
use GPG; use GPG;
use Digest::MD5;
use MIME::Base64;
# Debug # Debug
use Data::Dumper; use Data::Dumper;
@ -152,16 +155,39 @@ sub save {
sub generate { sub generate {
my $value; my $value;
open my $rnd, "<", "/dev/random"; # Defaults
read $rnd, $value, 32; my $length = 16;
my $digest;
for (1..32) {
open my $rnd, "<", "/dev/urandom";
read $rnd, $value, 1000;
my $c = unpack( "H*", $value ); my $c = unpack( "H*", $value );
close $rnd; close $rnd;
my @chars = split( //, $c ); # MORE ENTROPY
push @chars, $_ for ( '!', '@', '(', ')', 'A' .. 'Z' ); my $ctx = Digest::MD5->new();
$ctx->add($c);
# MORE
my $encoded = encode_base64( $ctx->hexdigest() );
$encoded =~ s/=//g;
$encoded =~ s/\n//g;
$digest .= $encoded;
}
my @chars = split( //, $digest );
my @r_special = ( '!', '@', '(', ')', '#', '$', '%', '^', '&' );
for (1..10) {
foreach my $special (@r_special) {
$chars[ rand(@chars) ] = $special ];
}
}
my $string; my $string;
$string .= $chars[ rand @chars ] for 1 .. 16; $string .= $chars[ rand @chars ] for 1 .. $length;
return $string; return $string;
} }