@@ -4,7 +4,7 @@ use Moose;
4
4
with ' MooseX::Emulate::Class::Accessor::Fast' ;
5
5
use MRO::Compat;
6
6
use Catalyst::Exception ();
7
- use Digest ();
7
+ use Crypt::SysRandom ();
8
8
use overload ();
9
9
use Object::Signature ();
10
10
use HTML::Entities ();
@@ -598,11 +598,7 @@ sub initialize_session_data {
598
598
}
599
599
600
600
sub generate_session_id {
601
- my $c = shift ;
602
-
603
- my $digest = $c -> _find_digest();
604
- $digest -> add( $c -> session_hash_seed() );
605
- return $digest -> hexdigest;
601
+ return unpack ( " H*" , Crypt::SysRandom::random_bytes(16) );
606
602
}
607
603
608
604
sub create_session_id_if_needed {
@@ -624,33 +620,10 @@ sub create_session_id {
624
620
return $sid ;
625
621
}
626
622
627
- my $counter ;
628
-
629
- sub session_hash_seed {
630
- my $c = shift ;
631
-
632
- return join ( " " , ++$counter , time , rand , $$ , {}, overload::StrVal($c ), );
633
- }
623
+ sub session_hash_seed { }
634
624
635
625
my $usable ;
636
626
637
- sub _find_digest () {
638
- unless ($usable ) {
639
- foreach my $alg (qw/ SHA-1 SHA-256 MD5/ ) {
640
- if ( eval { Digest-> new($alg ) } ) {
641
- $usable = $alg ;
642
- last ;
643
- }
644
- }
645
- Catalyst::Exception-> throw(
646
- " Could not find a suitable Digest module. Please install "
647
- . " Digest::SHA1, Digest::SHA, or Digest::MD5" )
648
- unless $usable ;
649
- }
650
-
651
- return Digest-> new($usable );
652
- }
653
-
654
627
sub dump_these {
655
628
my $c = shift ;
656
629
@@ -973,53 +946,22 @@ insensitive hexadecimal characters.
973
946
974
947
=item generate_session_id
975
948
976
- This method will return a string that can be used as a session ID. It is
977
- supposed to be a reasonably random string with enough bits to prevent
978
- collision. It basically takes C<session_hash_seed > and hashes it using SHA-1,
979
- MD5 or SHA-256, depending on the availability of these modules.
980
-
981
- =item session_hash_seed
982
-
983
- This method is actually rather internal to generate_session_id, but should be
984
- overridable in case you want to provide more random data.
949
+ This method will return a string that can be used as a session ID.
985
950
986
- Currently it returns a concatenated string which contains:
951
+ The string is simply 32 hex digits from the system randomness source using L<Crypt::SysRandom> .
987
952
988
- =over 4
989
-
990
- =item * A counter
991
-
992
- =item * The current time
993
-
994
- =item * One value from C<rand > .
995
-
996
- =item * The stringified value of a newly allocated hash reference
997
-
998
- =item * The stringified value of the Catalyst context object
999
-
1000
- =back
953
+ You can override this if you prefer to use a different source of randomness or different format of session ids:
1001
954
1002
- in the hopes that those combined values are entropic enough for most uses. If
1003
- this is not the case you can replace C<session_hash_seed > with e.g.
1004
-
1005
- sub session_hash_seed {
1006
- open my $fh, "<", "/dev/random";
1007
- read $fh, my $bytes, 20;
1008
- close $fh;
1009
- return $bytes;
1010
- }
1011
-
1012
- Or even more directly, replace C<generate_session_id > :
955
+ use Crypt::URandom::Token ();
1013
956
1014
957
sub generate_session_id {
1015
- open my $fh, "<", "/dev/random";
1016
- read $fh, my $bytes, 20;
1017
- close $fh;
1018
- return unpack("H*", $bytes);
958
+ state $tok = Crypt::URandom::Token->new();
959
+ return $tok->get;
1019
960
}
1020
961
1021
- Also have a look at L<Crypt::Random> and the various openssl bindings - these
1022
- modules provide APIs for cryptographically secure random data.
962
+ =item session_hash_seed
963
+
964
+ This method is no longer used.
1023
965
1024
966
=item finalize_session
1025
967
@@ -1249,5 +1191,3 @@ Robert Rothenberg <
[email protected] > (on behalf of Foxtons Ltd.)
1249
1191
it and/or modify it under the same terms as Perl itself.
1250
1192
1251
1193
=cut
1252
-
1253
-
0 commit comments