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 / DataFormat.pm < prev    next >
Encoding:
Perl POD Document  |  2003-01-08  |  4.3 KB  |  187 lines

  1. #!/usr/bin/perl -s
  2. ##
  3. ## Crypt::RSA::DataFormat -- Functions for converting, shaping and 
  4. ##                           creating and reporting data formats.
  5. ##
  6. ## Copyright (c) 2001, Vipul Ved Prakash.  All rights reserved.
  7. ## This code is free software; you can redistribute it and/or modify
  8. ## it under the same terms as Perl itself.
  9. ##
  10. ## $Id: DataFormat.pm,v 1.13 2001/05/20 23:37:45 vipul Exp $
  11.  
  12. package Crypt::RSA::DataFormat; 
  13. use lib qw(lib);
  14. use vars qw(@ISA);
  15. use Math::Pari qw(PARI pari2pv floor pari2num);
  16. use Crypt::Random qw(makerandom);
  17. use Digest::SHA1 qw(sha1);
  18. use Carp;
  19. require Exporter;
  20. @ISA = qw(Exporter);
  21.  
  22. @EXPORT_OK = qw(i2osp os2ip h2osp octet_xor octet_len bitsize 
  23.                 generate_random_octet mgf1 steak);
  24.  
  25.  
  26. sub i2osp {
  27.     my $num = PARI(shift); 
  28.     my $d = $num;
  29.     my $l = shift || 0;
  30.     my $base = PARI(256); my $result = '';
  31.     if ($l) { return if $num > $base ** $l }
  32.  
  33.     do { 
  34.         my $r = $d % $base;
  35.         $d = ($d-$r) / $base;
  36.         $result = chr($r) . $result;
  37.     } until ($d <= $base);
  38.     $result = chr($d) . $result if $d != 0;
  39.  
  40.     if (length($result) < $l) { 
  41.         $result = chr(0)x($l-length($result)) . $result;
  42.     }
  43.     return $result;
  44. }
  45.  
  46.  
  47. sub os2ip {
  48.     my $string = shift;
  49.     my $base = PARI(256);
  50.     my $result = 0;
  51.     my $l = length($string); 
  52.     for (0 .. $l-1) {
  53.         my ($c) = unpack "x$_ a", $string;
  54.         my $a = int(ord($c));
  55.         my $val = int($l-$_-1); 
  56.         $result += $a * ($base**$val);
  57.     }
  58.     return $result;
  59. }
  60.  
  61.  
  62. sub h2osp { 
  63.     my $hex = shift;
  64.     $hex =~ s/[ \n]//ig;
  65.     my $num = Math::Pari::_hex_cvt($hex);
  66.     return i2osp ($num);
  67. }
  68.  
  69.  
  70. sub generate_random_octet {
  71.     my ( $l, $str ) = @_;
  72.     my $r = makerandom ( Size => int($l*8), Strength => $str );
  73.     return i2osp ($r, $l);
  74. }
  75.  
  76.  
  77. sub bitsize ($) {
  78.     return pari2num(floor(Math::Pari::log(shift)/Math::Pari::log(2)) + 1);
  79. }
  80.  
  81.  
  82. sub octet_len { 
  83.     return pari2num(floor(PARI((bitsize(shift)+7)/8)));
  84. }
  85.  
  86.  
  87. sub octet_xor { 
  88.     my ($a, $b) = @_; my @xor;
  89.     my @ba = split //, unpack "B*", $a; 
  90.     my @bb = split //, unpack "B*", $b; 
  91.     if (@ba != @bb) {
  92.         if (@ba < @bb) { 
  93.             for (1..@bb-@ba) { unshift @ba, '0' }
  94.         } else { 
  95.             for (1..@ba-@bb) { unshift @bb, '0' }
  96.         }
  97.     } 
  98.     for (0..$#ba) { 
  99.         $xor[$_] = ($ba[$_] xor $bb[$_]) || 0; 
  100.     }
  101.     return pack "B*", join '',@xor; 
  102. }
  103.  
  104.  
  105. sub mgf1 {
  106.     my ($seed, $l) = @_;
  107.     my $hlen = 20;  my ($T, $i) = ("",0);
  108.     while ($i <= $l) { 
  109.         my $C = i2osp (int($i), 4);
  110.         $T .= sha1("$seed$C");
  111.         $i += $hlen;
  112.     }
  113.     my ($output) = unpack "a$l", $T;
  114.     return $output;
  115. }
  116.  
  117.  
  118. sub steak { 
  119.     my ($text, $blocksize) = @_; 
  120.     my $textsize = length($text);
  121.     my $chunkcount = $textsize % $blocksize 
  122.         ? int($textsize/$blocksize) + 1 : $textsize/$blocksize;
  123.     my @segments = unpack "a$blocksize"x$chunkcount, $text;
  124.     return @segments;
  125. }
  126.  
  127. 1;
  128.  
  129. =head1 NAME
  130.  
  131. Crypt::RSA::DataFormat - Data creation, conversion and reporting primitives.
  132.  
  133. =head1 DESCRIPTION
  134.  
  135. This module implements several data creation, conversion and reporting
  136. primitives used throughout the Crypt::RSA implementation. Primitives are
  137. available as exportable functions.
  138.  
  139. =head1 FUNCTIONS
  140.  
  141. =over 4
  142.  
  143. =item B<i2osp> Integer, Length
  144.  
  145. Integer To Octet String Primitive. Converts an integer into its equivalent
  146. octet string representative of length B<Length>. If necessary, the
  147. resulting string is prefixed with nulls.
  148.  
  149. =item B<os2ip> String
  150.  
  151. Octet String to Integer Primitive. Converts an octet string into its
  152. equivalent integer representative.
  153.  
  154. =item B<generate_random_octet> Length, Strength
  155.  
  156. Generates a random octet string of length B<Length>. B<Strength> specifies
  157. the degree of randomness. See Crypt::Random(3) for an explanation of the
  158. B<Strength> parameter.
  159.  
  160. =item B<bitsize> Integer
  161.  
  162. Returns the length of the B<Integer> in bits.
  163.  
  164. =item B<octet_len> Integer
  165.  
  166. Returns the octet length of the integer. If the length is not a whole
  167. number, the fractional part is dropped to make it whole.
  168.  
  169. =item B<octet_xor> String1, String2
  170.  
  171. Returns the result of B<String1> XOR B<String2>.
  172.  
  173. =item B<steak> String, Length 
  174.  
  175. Returns an array of segments of length B<Length> from B<String>. The final
  176. segment can be smaller than B<Length>.
  177.  
  178. =back
  179.  
  180. =head1 AUTHOR
  181.  
  182. Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>
  183.  
  184. =cut
  185.  
  186.  
  187.