Skip to content

Commit 52ebc0f

Browse files
committed
Use Crypt::URandom to get the random seed
This is a platform-independent method for querying /dev/urandom or equivalents. It also uses the Win32::API on Windows, and it should work on Perl v5.6.
1 parent 246f8b7 commit 52ebc0f

File tree

2 files changed

+8
-41
lines changed

2 files changed

+8
-41
lines changed

Makefile.PL

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ use strict;
33

44
use ExtUtils::MakeMaker;
55

6-
my $deps = {};
7-
8-
$deps->{'Crypt::Random::Source::Strong::Win32'} = 0
9-
if $^O =~ /mswin/i;
6+
my %deps = (
7+
'Crypt::URandom' => '0.37',
8+
);
109

1110
my %args = (
1211
NAME => 'Session::Token',
1312
VERSION_FROM => 'lib/Session/Token.pm',
14-
PREREQ_PM => $deps,
13+
PREREQ_PM => \%deps,
1514
LIBS => [''],
1615
DEFINE => '',
1716
INC => '-I.',
@@ -20,7 +19,7 @@ my %args = (
2019
dist => {
2120
PREOP => 'pod2text $(VERSION_FROM) > $(DISTVNAME)/README',
2221
},
23-
MIN_PERL_VERSION => '5.8.0',
22+
MIN_PERL_VERSION => '5.6.0',
2423
);
2524

2625

lib/Session/Token.pm

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package Session::Token;
33
use strict;
44

55
use Carp qw/croak/;
6+
use Crypt::URandom qw/urandom/;
67
use POSIX qw/ceil/;
78

8-
99
our $VERSION = '1.503';
1010

1111
require XSLoader;
@@ -15,14 +15,6 @@ XSLoader::load('Session::Token', $VERSION);
1515
my $default_alphabet = join('', ('0'..'9', 'a'..'z', 'A'..'Z',));
1616
my $default_entropy = 128;
1717

18-
my $is_windows;
19-
20-
if ($^O =~ /mswin/i) {
21-
require Crypt::Random::Source::Strong::Win32;
22-
$is_windows = 1;
23-
}
24-
25-
2618
sub new {
2719
my ($class, @args) = @_;
2820

@@ -43,26 +35,9 @@ sub new {
4335
}
4436

4537
if (!defined $seed) {
46-
if ($is_windows) {
47-
my $windows_rng_source = Crypt::Random::Source::Strong::Win32->new;
48-
$seed = $windows_rng_source->get(1024);
49-
die "Win32 RNG source didn't provide 1024 bytes" unless length($seed) == 1024;
50-
} else {
51-
my ($fh, $err1, $err2);
52-
53-
open($fh, '<:raw', '/dev/urandom') || ($err1 = $!);
54-
open($fh, '<:raw', '/dev/arandom') || ($err2 = $!)
55-
unless defined $fh;
56-
57-
if (!defined $fh) {
58-
croak "unable to open /dev/urandom ($err1) or /dev/arandom ($err2)";
59-
}
60-
61-
sysread($fh, $seed, 1024) == 1024 || croak "unable to read from random device: $!";
62-
}
38+
$seed = urandom(1024);
6339
}
6440

65-
6641
## Init alphabet
6742

6843
my $alphabet = defined $args{alphabet} ? $args{alphabet} : $default_alphabet;
@@ -140,7 +115,7 @@ Session::Token - Secure, efficient, simple random session token generation
140115
141116
This module provides a secure, efficient, and simple interface for creating session tokens, password reset codes, temporary passwords, random identifiers, and anything else you can think of.
142117
143-
When a Session::Token object is created, 1024 bytes are read from C</dev/urandom> (Linux, Solaris, most BSDs), C</dev/arandom> (some older BSDs), or L<Crypt::Random::Source::Strong::Win32> (Windows). These bytes are used to seed the L<ISAAC-32|http://www.burtleburtle.net/bob/rand/isaacafa.html> pseudo random number generator.
118+
When a Session::Token object is created, 1024 bytes are read from L<Crypt::URandom>. These bytes are used to seed the L<ISAAC-32|http://www.burtleburtle.net/bob/rand/isaacafa.html> pseudo random number generator.
144119
145120
Once a generator is created, you can repeatedly call the C<get> method on the generator object and it will return a new token each time.
146121
@@ -420,13 +395,6 @@ Would be cool if it could detect forks and warn or re-seed in the child process
420395
421396
There is currently no way to extract the seed from a Session::Token object. Note when implementing this: The saved seed must either store the current state of the ISAAC round as well as the 1024 byte C<randsl> array or else do some kind of minimum fast forwarding in order to protect against a partially duplicated output-stream bug.
422397
423-
Doesn't work on perl 5.6 and below due to the use of C<:raw> (thanks CPAN testers). It could probably use C<binmode> instead, but meh.
424-
425-
On windows we use L<Crypt::Random::Source::Strong::Win32> which has a big dependency tree. We should instead use a slimmer module like L<Crypt::Random::Seed>.
426-
427-
428-
429-
430398
=head1 SEE ALSO
431399
432400
L<The Session::Token github repo|https://github.com/hoytech/Session-Token>

0 commit comments

Comments
 (0)