Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove insecure random #35

Open
Sc00bz opened this issue Apr 16, 2018 · 1 comment
Open

Remove insecure random #35

Sc00bz opened this issue Apr 16, 2018 · 1 comment

Comments

@Sc00bz
Copy link

Sc00bz commented Apr 16, 2018

Remove

  • Basic.pm
  • RandomDotOrg.pm

If for some reason you want to keep RandomDotOrg.pm you should fix

my $dec = $line/$RDO_MAX_INT;
unless($dec >= 0 && $dec <=1){

        my $dec = $line/($RDO_MAX_INT+1);
        unless($dec >= 0 && $dec <1){

Also consider fixing the slight bias in doing itemIndex = floor(rand * numberOfItems). You can do something like https://github.com/Sc00bz/ModRandom/blob/4fc203bc18bd32254c33369205557f720145abb6/random.c#L137

Edit: Changed link

@Sc00bz
Copy link
Author

Sc00bz commented Apr 16, 2018

I just assumed that you did itemIndex = floor(rand * numberOfItems). It's actually much worse

my $ans = ($self->_rand() * 1_000_000) % $max;

You should replace RNG.pm's random floats [0, 1) with random 32 bit integers [0, 4294967295]. Then replace _random_int with:

sub _random_int{
    my @args = @_;
    my $self = shift @args;
    _force_instance($self);
    
    # validate args
    state $args_check = compile(NonZeroPositiveInteger);
    my $count = $args_check->(@args);
    
    # calculate the random number
    my $ans = $self->_rand();
    
    # If nice base 2 modulo (ie 2**n)
    if ((($count - 1) & $count) == 0){
        $ans = $ans & ($count - 1);
    }else{
        my $skip = 4_294_967_295 - 4_294_967_295 % $count;
        while ($ans >= $skip){
            $ans = $self->_rand();
        }
        $ans = $ans % $count;
    }
    
    # return it
    _debug("returning $ans (max=$max)");
    return $ans;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant