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 / rand.pm < prev    next >
Encoding:
Perl POD Document  |  2001-06-22  |  1.1 KB  |  61 lines

  1. #!/usr/bin/perl -sw
  2. ##
  3. ##
  4. ##
  5. ## Copyright (c) 2001, 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: rand.pm,v 1.2 2001/06/22 18:16:29 vipul Exp $
  10.  
  11. package Crypt::Random::Provider::rand; 
  12. use strict;
  13. use Math::Pari qw(pari2num);
  14.  
  15. sub new { 
  16.  
  17.     my ($class, %params) = @_;
  18.     my $self = { Source => $params{Source} || sub { return rand($_[0]) } };
  19.     return bless $self, $class;
  20.  
  21. }
  22.  
  23.  
  24. sub get_data { 
  25.  
  26.     my ($self, %params) = @_;
  27.     $self = {} unless ref $self;
  28.  
  29.     my $size = $params{Size}; 
  30.     my $skip = $params{Skip} || $$self{Skip};
  31.  
  32.     if ($size && ref $size eq "Math::Pari") { 
  33.         $size = pari2num($size);
  34.     }
  35.  
  36.     my $bytes = $params{Length} || (int($size / 8) + 1);
  37.     my $source = $$self{Source} || sub { rand($_[0]) };
  38.     
  39.     my($r, $read, $rt) = ('', 0);
  40.     while ($read < $bytes) {
  41.         $rt = chr(int(&$source(256)));
  42.         unless ($skip && $skip =~ /\Q$rt\E/) {
  43.             $r .= $rt; $read++;
  44.         }
  45.     }
  46.  
  47.     $r;
  48.  
  49. }
  50.  
  51.  
  52. sub available { 
  53.  
  54.     return 1;
  55.  
  56. }
  57.  
  58.  
  59. 1;
  60.  
  61.