Crypt-URandom-0.34/0000755000076400007640000000000012117050111012521 5ustar davedaveCrypt-URandom-0.34/MANIFEST0000644000076400007640000000031211553375500013665 0ustar davedaveBuild.PL Changes MANIFEST Makefile.PL README lib/Crypt/URandom.pm t/00.load.t t/pod-coverage.t t/pod.t t/rand.t t/export.t META.yml Module meta-data (added by MakeMaker) Crypt-URandom-0.34/Build.PL0000644000076400007640000000100511553375337014040 0ustar davedaveuse strict; use warnings; use Module::Build; my $builder = Module::Build->new( module_name => 'Crypt::URandom', license => 'perl', dist_author => 'David Dick ', dist_version_from => 'lib/Crypt/URandom.pm', requires => { 'Test::More' => 0, 'Carp' => 0, 'English' => 0, ($^O eq 'MSWin32') ? ( 'Win32::API' => 0 ) : ( 'FileHandle' => 0 ), }, add_to_cleanup => [ 'Crypt-URandom-*' ], ); $builder->create_build_script(); Crypt-URandom-0.34/Changes0000644000076400007640000000255612117050102014024 0ustar davedaveRevision history for Crypt-URandom 0.34 - Sun Mar 10 20:18:43 2011 Removed t/pod-coverage.t 0.33 - Sat Mar 09 15:29:41 2011 Added @CARP_NOT 0.32 - Sat Mar 09 15:15:37 2011 Reformatting 0.31 - Fri Mar 01 17:56:58 2011 More test case fixes 0.30 - Mon Feb 25 07:55:00 2011 Fixed test cases 0.29 - Thu Feb 14 06:40:41 2011 Added binmode call to the open for /dev/urandom Optimised /dev/urandom access to only open a filehandle once and only reopen after read errors 0.28 Wed Apr 20 06:45:41 2011 Dropped extra 0 in version number Now passes perlcritic -1 0.0.27 Wed Apr 20 06:45:41 2011 Included t/export.t in MANIFEST Formatting changes Changed module name from Crypt::Random::NonBlocking to Crypt::URandom 0.0.25 Mon Apr 18 18:55:30 2011 Removed t/perlcritic.t due to other Perl::Critic policies being testing on cpan.org Changed the interface to be Crypt::Random::NonBlocking::urandom(); Allowed urandom to be exported 0.0.23 Mon Apr 18 07:56:17 2011 Added t/rand.t to actually test the module 0.0.21 Mon Apr 18 07:00:17 2011 Removed dependency Readonly entirely as it is a non-core module 0.0.19 Sun Apr 17 20:42:17 2011 Added dependency Readonly to fix RT Ticket 67532 filed by YSAWANT 0.0.11 Sun Apr 17 10:45:13 2011 Initial release. Crypt-URandom-0.34/META.yml0000664000076400007640000000113312117050111013772 0ustar davedave--- #YAML:1.0 name: Crypt-URandom version: 0.34 abstract: Provide non blocking randomness author: - David Dick license: perl distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: Carp: 0 English: 0 FileHandle: 0 Test::More: 0 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.57_05 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 Crypt-URandom-0.34/lib/0000755000076400007640000000000012117050111013267 5ustar davedaveCrypt-URandom-0.34/lib/Crypt/0000755000076400007640000000000012117050111014370 5ustar davedaveCrypt-URandom-0.34/lib/Crypt/URandom.pm0000644000076400007640000002410412117047735016316 0ustar davedave## no critic (RequireRcsKeywords) # here's 20 cents kid, go buy yourself a real revision control system package Crypt::URandom; use warnings; use strict; use Carp(); use English qw( -no_match_vars ); use Exporter(); *import = \&Exporter::import; our @EXPORT_OK = qw( urandom ); our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, ); our $VERSION = '0.34'; our @CARP_NOT = ('Crypt::URandom'); sub CRYPT_SILENT { return 64; } # hex 40 sub PROV_RSA_FULL { return 1; } sub VERIFY_CONTEXT { return 4_026_531_840; } # hex 'F0000000' sub W2K_MAJOR_VERSION { return 5; } sub W2K_MINOR_VERSION { return 0; } sub SINGLE_QUOTE { return q[']; } sub PATH { my $path = '/dev/urandom'; if ( $OSNAME eq 'freebsd' ) { $path = '/dev/random'; # FreeBSD's /dev/random is non-blocking } return $path; } my $_initialised; my $_context; my $_cryptgenrandom; my $_rtlgenrand; my $_urandom_handle; sub _init { if ( $OSNAME eq 'MSWin32' ) { require Win32; require Win32::API; require Win32::API::Type; my ( $major, $minor ) = ( Win32::GetOSVersion() )[ 1, 2 ]; my $ntorlower = ( $major < W2K_MAJOR_VERSION() ) ? 1 : 0; my $w2k = ( $major == W2K_MAJOR_VERSION() and $minor == W2K_MINOR_VERSION() ) ? 1 : 0; if ($ntorlower) { Carp::croak( 'No secure alternative for random number generation for Win32 versions older than W2K' ); } elsif ($w2k) { my $crypt_acquire_context_a = Win32::API->new( 'advapi32', 'CryptAcquireContextA', 'PPPNN', 'I' ); if ( !defined $crypt_acquire_context_a ) { Carp::croak( "Could not import CryptAcquireContext: $EXTENDED_OS_ERROR"); } my $context = chr(0) x Win32::API::Type->sizeof('PULONG'); my $result = $crypt_acquire_context_a->Call( $context, 0, 0, PROV_RSA_FULL(), CRYPT_SILENT() | VERIFY_CONTEXT() ); my $pack_type = Win32::API::Type::packing('PULONG'); $context = unpack $pack_type, $context; if ( !$result ) { Carp::croak("CryptAcquireContext failed: $EXTENDED_OS_ERROR"); } my $crypt_gen_random = Win32::API->new( 'advapi32', 'CryptGenRandom', 'NNP', 'I' ); if ( !defined $crypt_gen_random ) { Carp::croak( "Could not import CryptGenRandom: $EXTENDED_OS_ERROR"); } $_context = $context; $_cryptgenrandom = $crypt_gen_random; } else { my $rtlgenrand = Win32::API->new( 'advapi32', <<'_RTLGENRANDOM_PROTO_'); INT SystemFunction036( PVOID RandomBuffer, ULONG RandomBufferLength ) _RTLGENRANDOM_PROTO_ if ( !defined $rtlgenrand ) { Carp::croak( "Could not import SystemFunction036: $EXTENDED_OS_ERROR"); } $_rtlgenrand = $rtlgenrand; } } else { require FileHandle; $_urandom_handle = FileHandle->new( PATH(), Fcntl::O_RDONLY() ) or Carp::croak( 'Failed to open ' . SINGLE_QUOTE() . PATH() . SINGLE_QUOTE() . " for reading:$OS_ERROR" ); binmode $_urandom_handle; } return; } sub urandom { my ($length) = @_; my $length_ok; if ( defined $length ) { if ( $length =~ /^\d+$/xms ) { $length_ok = 1; } } if ( !$length_ok ) { Carp::croak( 'The length argument must be supplied and must be an integer'); } if ( !$_initialised ) { _init(); $_initialised = 1; } if ( $OSNAME eq 'MSWin32' ) { my $buffer = chr(0) x $length; if ($_cryptgenrandom) { my $result = $_cryptgenrandom->Call( $_context, $length, $buffer ); if ( !$result ) { Carp::croak("CryptGenRandom failed: $EXTENDED_OS_ERROR"); } } elsif ($_rtlgenrand) { my $result = $_rtlgenrand->Call( $buffer, $length ); if ( !$result ) { Carp::croak("RtlGenRand failed: $EXTENDED_OS_ERROR"); } } return $buffer; } else { my $result = $_urandom_handle->read( my $buffer, $length ); if ( defined $result ) { if ( $result == $length ) { return $buffer; } else { $_urandom_handle = undef; Carp::croak( "Only read $result bytes from " . SINGLE_QUOTE() . PATH() . SINGLE_QUOTE() ); } } else { my $error = $OS_ERROR; $_urandom_handle = undef; Carp::croak( 'Failed to read from ' . SINGLE_QUOTE() . PATH() . SINGLE_QUOTE() . ":$error" ); } } } 1; # Magic true value required at end of module __END__ =head1 NAME Crypt::URandom - Provide non blocking randomness =head1 VERSION This document describes Crypt::URandom version 0.34 =head1 SYNOPSIS use Crypt::URandom(); my $random_string_50_bytes_long = Crypt::URandom::urandom(50); OR use Crypt::URandom qw( urandom ); my $random_string_50_bytes_long = urandom(50); =head1 DESCRIPTION This Module is intended to provide an interface to the strongest available source of non-blocking randomness on the current platform. Platforms currently supported are anything supporting /dev/urandom and versions of Windows greater than or equal to Windows 2000. =head1 SUBROUTINES/METHODS =over =item C =for stopwords cryptographic This function accepts an integer and returns a string of the same size filled with random data. The first call will initialize the native cryptographic libraries (if necessary) and load all the required Perl libraries =back =head1 DIAGNOSTICS =over =item C The module cannot run on versions of Windows earlier than Windows 2000 as there is no cryptographic functions provided by the operating system. =item C =for stopwords CryptAcquireContextA advapi32 The module was unable to load the CryptAcquireContextA function from the advapi32 dynamic library. The advapi32 library cannot probably be loaded. =item C =for stopwords advapi32 The module was unable to call the CryptAcquireContextA function from the advapi32 dynamic library. =item C =for stopwords advapi32 CryptGenRandom The module was unable to load the CryptGenRandom function from the advapi32 dynamic library. =item C =for stopwords SystemFunction036 The module was unable to load the SystemFunction036 function from the advapi32 dynamic library. =item C The get method must be called with an integer argument to describe how many random bytes are required. =item C The Windows 2000 CryptGenRandom method call failed to generate the required amount of randomness =item C =for stopwords RtlGenRand The post Windows 2000 RtlGenRand method call failed to generate the required amount of randomness =item C The /dev/urandom device did not return the desired amount of random bytes =item C The /dev/urandom device returned an error when being read from =item C The /dev/urandom device returned an error when being opened =back =head1 CONFIGURATION AND ENVIRONMENT Crypt::URandom requires no configuration files or environment variables. =head1 DEPENDENCIES =over =for stopwords perl If the platform is Win32, the Win32::API module will be required. Otherwise no other modules other than those provided by perl will be required =back =head1 INCOMPATIBILITIES None reported. =head1 BUGS AND LIMITATIONS No bugs have been reported. Please report any bugs or feature requests to C, or through the web interface at L. =head1 AUTHOR David Dick C<< >> =for stopwords ACKNOWLEDGEMENTS =head1 ACKNOWLEDGEMENTS =for stopwords CryptoAPI Kanat-Alexander The Win32::API code for interacting with Microsoft's CryptoAPI was stolen with extreme gratitude from Crypt::Random::Source::Strong::Win32 by Max Kanat-Alexander =head1 LICENSE AND COPYRIGHT Copyright (c) 2011, David Dick C<< >>. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 DISCLAIMER OF WARRANTY =for stopwords MERCHANTABILITY LICENCE BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Crypt-URandom-0.34/Makefile.PL0000644000076400007640000000134211553375337014522 0ustar davedaveuse strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Crypt::URandom', AUTHOR => 'David Dick ', VERSION_FROM => 'lib/Crypt/URandom.pm', ABSTRACT_FROM => 'lib/Crypt/URandom.pm', PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, 'Carp' => 0, 'English' => 0, ($^O eq 'MSWin32') ? ( 'Win32::API' => 0 ) : ( 'FileHandle' => 0 ), }, ($ExtUtils::MakeMaker::VERSION ge '6.30_00' ? # shamelessly copied this from Template Toolkit (LICENSE => 'perl') : ()), dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'Crypt-URandom-*' }, ); Crypt-URandom-0.34/README0000644000076400007640000000134311553375337013431 0ustar davedaveCrypt-URandom version 0.0.27 This Module is intended to provide an interface to the strongest available source of non-blocking randomness on the current platform. Platforms currently supported are anything supporting /dev/urandom and versions of Windows greater than or equal to Windows 2000. INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install Alternatively, to install with Module::Build, you can use the following commands: perl Build.PL ./Build ./Build test ./Build install DEPENDENCIES None. COPYRIGHT AND LICENCE Copyright (C) 2011, David Dick This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Crypt-URandom-0.34/t/0000755000076400007640000000000012117050111012764 5ustar davedaveCrypt-URandom-0.34/t/00.load.t0000644000076400007640000000017611553375337014341 0ustar davedaveuse Test::More tests => 1; BEGIN { use_ok( 'Crypt::URandom' ); } diag( "Testing Crypt::URandom $Crypt::URandom::VERSION" ); Crypt-URandom-0.34/t/export.t0000644000076400007640000000030311553375337014515 0ustar davedaveuse Test::More tests => 2; use Crypt::URandom qw(urandom); ok(length(urandom(5000)) == 5000, 'urandom(5000) called successfully'); ok(length(urandom(1)) == 1, 'urandom(1) called successfully'); Crypt-URandom-0.34/t/pod.t0000644000076400007640000000021411552434031013741 0ustar davedave#!perl -T use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok(); Crypt-URandom-0.34/t/rand.t0000644000076400007640000000104312114050546014104 0ustar davedaveuse Test::More tests => 3; use Crypt::URandom(); ok(length(Crypt::URandom::urandom(500)) == 500, 'Crypt::URandom::urandom(500) called successfully'); ok(length(Crypt::URandom::urandom(50)) == 50, 'Crypt::URandom::urandom(50) called successfully'); SKIP: { eval { require Encode; }; if ($@) { skip("Encode module cannot be loaded", 1); } else { my $returns_binary_data = 1; if (Encode::is_utf8(Crypt::URandom::urandom(2))) { $returns_binary_data = 0; } ok($returns_binary_data, 'Crypt::Urandom::urandom returns binary data'); } }