home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Random.pm < prev    next >
Encoding:
Perl POD Document  |  2003-03-11  |  5.6 KB  |  233 lines

  1. #!/usr/bin/perl -s
  2. ##
  3. ## Crypt::Random -- Interface to /dev/random and /dev/urandom.
  4. ##
  5. ## Copyright (c) 1998, Vipul Ved Prakash.  All rights reserved.
  6. ## This code is free software; you can redistribute it and/or modify
  7. ## it under the same terms as Perl itself.
  8. ##
  9. ## $Id: Random.pm,v 1.11 2001/07/12 15:59:47 vipul Exp $
  10.  
  11. package Crypt::Random; 
  12. require Exporter;
  13. use vars qw($VERSION @EXPORT_OK); 
  14. use Math::Pari qw(PARI floor Mod pari2pv pari2num lift); 
  15. use Carp; 
  16. use Data::Dumper;
  17. use Class::Loader;
  18. use Crypt::Random::Generator;
  19. *import      = \&Exporter::import;
  20.  
  21. @EXPORT_OK   = qw( makerandom makerandom_itv makerandom_octet );
  22. ($VERSION) = do { my @r = (q$Revision: 1.13 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
  23.  
  24.  
  25. sub _pickprovider { 
  26.  
  27.     my (%params) = @_;
  28.  
  29.     return $params{Provider} if $params{Provider};
  30.     $params{Strength} ||= 0;
  31.     my $gen = new Crypt::Random::Generator Strength => $params{Strength};
  32.     return $gen->{Provider};
  33.  
  34. }
  35.  
  36. sub makerandom { 
  37.  
  38.     my ( %params ) = @_;
  39.  
  40.     $params{Verbosity} = 0 unless $params{Verbosity};
  41.  
  42.     local $| = 1;
  43.  
  44.     my $provider = _pickprovider(%params);
  45.     my $loader = new Class::Loader;
  46.     my $po = $loader->_load ( Module => "Crypt::Random::Provider::$provider", 
  47.                               Args => [ %params ] ) 
  48.         or die "Unable to load module Crypt::Random::Provider::$provider - $!";
  49.     my $r = $po->get_data( %params );
  50.  
  51.     my $size     = $params{Size};
  52.     my $down     = $size - 1;
  53.     $y = unpack "H*",     pack "B*", '0' x ( $size%8 ? 8-$size % 8 : 0 ). '1'.
  54.          unpack "b$down", $r;
  55.  
  56.     return Math::Pari::_hex_cvt ( "0x$y" );
  57.  
  58. }
  59.  
  60.  
  61. sub makerandom_itv { 
  62.  
  63.     my ( %params ) = @_; 
  64.  
  65.     my $a  = $params{ Lower } || 0; $a = PARI ( $a ); 
  66.     my $b  = $params{ Upper }; $b = PARI ( $b );
  67.  
  68.     my $itv    = Mod ( 0, $b - $a );
  69.     my $size   = length ( $itv ) * 5;
  70.     my $random = makerandom %params, Size => $size;
  71.  
  72.     $itv += $random; 
  73.     my $r = PARI ( lift ( $itv ) + $a );
  74.  
  75.     undef $itv; undef $a; undef $b; 
  76.     return "$r";
  77.  
  78. }
  79.  
  80.  
  81. sub makerandom_octet  {
  82.  
  83.     my ( %params ) = @_; 
  84.  
  85.     $params{Verbosity} = 0 unless $params{Verbosity};
  86.  
  87.     my $provider = _pickprovider(%params); 
  88.     my $loader = new Class::Loader;
  89.     my $po = $loader->_load ( Module => "Crypt::Random::Provider::$provider", 
  90.                               Args => [ %params ] );
  91.     return $po->get_data( %params );
  92.  
  93.  
  94. }
  95.  
  96.  
  97. 'True Value';
  98.  
  99. =head1 NAME
  100.  
  101. Crypt::Random - Cryptographically Secure, True Random Number Generator. 
  102.  
  103. =head1 VERSION
  104.  
  105.  $Revision: 1.11 $
  106.  $Date: 2001/07/12 15:59:47 $
  107.  
  108. =head1 SYNOPSIS
  109.  
  110.  use Crypt::Random qw( makerandom ); 
  111.  my $r = makerandom ( Size => 512, Strength => 1 ); 
  112.  
  113. =head1 DESCRIPTION
  114.  
  115. Crypt::Random is an interface module to the /dev/random device found on
  116. most modern unix systems. It also interfaces with egd, a user space
  117. entropy gathering daemon, available for systems where /dev/random (or
  118. similar) devices are not available. When Math::Pari is installed,
  119. Crypt::Random can generate random integers of arbritary size of a given
  120. bitsize or in a specified interval.
  121.  
  122. =head1 BLOCKING BEHAVIOUR
  123.  
  124. The /dev/random driver maintains an estimate of true randomness in the
  125. pool and decreases it every time random strings are requested for use.
  126. When the estimate goes down to zero, the routine blocks and waits for the
  127. occurrence of non-deterministic events to refresh the pool.
  128.  
  129. When the routine is blocked, Crypt::Random's read() will be blocked till
  130. desired amount of random bytes have been read off of the device. The
  131. /dev/random kernel module also provides another interface, /dev/urandom,
  132. that does not wait for the entropy-pool to recharge and returns as many
  133. bytes as requested. For applications that must not block (for a
  134. potentially long time) should use /dev/urandom. /dev/random should be
  135. reserved for instances where very high quality randomness is desired.
  136.  
  137. =head1 HARDWARE RNG
  138.  
  139. If there's a hardware random number generator available, for instance the
  140. Intel i8x0 random number generator, please use it instead of /dev/random!.
  141. It'll be high quality, a lot faster and it won't block! Usually your OS
  142. will provide access to the RNG as a device, eg (/dev/intel_rng).
  143.  
  144. =head1 METHODS 
  145.  
  146. =over 4
  147.  
  148. =item B<makerandom()>
  149.  
  150. Generates a random number of requested bitsize in base 10. Following
  151. arguments can be specified.
  152.  
  153. =over 4
  154.  
  155. =item B<Size> 
  156.  
  157. Bitsize of the random number. 
  158.  
  159. =item B<Strength> 0 || 1 
  160.  
  161. Value of 1 implies that /dev/random should be used
  162. for requesting random bits while 0 implies /dev/urandom.
  163.  
  164. =item B<Device> 
  165.  
  166. Alternate device to request random bits from. 
  167.  
  168. =back 
  169.  
  170. =item B<makerandom_itv()> 
  171.  
  172. Generates a random number in the specified interval.  In addition 
  173. to the arguments to makerandom() following attributes can be 
  174. specified. 
  175.  
  176. =over 4
  177.  
  178. =item B<Lower> 
  179.  
  180. Inclusive Lower limit.  
  181.  
  182. =item B<Upper> 
  183.  
  184. Exclusive Upper limit. 
  185.  
  186. =back 
  187.  
  188. =item B<makerandom_octet()>
  189.  
  190. Generates a random octet string of specified length. In addition to
  191. B<Strength>, B<Device> and B<Verbosity>, following arguments can be
  192. specified.
  193.  
  194. =over 4
  195.  
  196. =item B<Length>
  197.  
  198. Length of the desired octet string. 
  199.  
  200. =item B<Skip>
  201.  
  202. An octet string consisting of characters to be skipped while reading from
  203. the random device.
  204.  
  205. =back
  206.  
  207. =back
  208.  
  209. =head1 DEPENDENCIES
  210.  
  211. Crypt::Random needs Math::Pari 2.001802 or higher. As of this writing, the
  212. latest version of Math::Pari isn't available from CPAN. Fetch it from
  213. ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl/modules/
  214.  
  215. =head1 BIBLIOGRAPHY 
  216.  
  217. =over 4
  218.  
  219. =item 1 random.c by Theodore Ts'o.  Found in drivers/char directory of 
  220. the Linux kernel sources.
  221.  
  222. =item 2 Handbook of Applied Cryptography by Menezes, Paul C. van Oorschot
  223. and Scott Vanstone.
  224.  
  225. =back
  226.  
  227. =head1 AUTHOR
  228.  
  229. Vipul Ved Prakash, <mail@vipul.net>
  230.  
  231. =cut
  232.  
  233.