File Coverage

blib/lib/String/Urandom.pm
Criterion Covered Total %
statement 32 39 82.0
branch 6 14 42.8
condition 4 4 100.0
subroutine 8 8 100.0
pod 4 5 80.0
total 54 70 77.1


line stmt bran cond sub pod time code
1             #----------------------------------------------------------------------------+
2             #
3             # String::Urandom - An alternative to using /dev/random
4             #
5             # DESCRIPTION
6             # Using output of /dev/urandom. Simply convert bytes into 8-bit characters.
7             #
8             # AUTHOR
9             # Marc S. Brooks
10             #
11             # This module is free software; you can redistribute it and/or
12             # modify it under the same terms as Perl itself.
13             #
14             #----------------------------------------------------------------------------+
15              
16             package String::Urandom;
17              
18 2     2   74173 use strict;
  2         5  
  2         77  
19 2     2   11 use warnings;
  2         4  
  2         62  
20 2     2   2168 use Params::Validate qw( :all );
  2         33600  
  2         4953  
21              
22             our $VERSION = 0.16;
23              
24             #~~~~~~~~~~~~~~~~~~~~~~~~~~[ OBJECT METHODS ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
25              
26             #----------------------------------------------------------------------------+
27             # new(\%params)
28             #
29             # General object constructor.
30              
31             sub new {
32 2     2 1 1070 my $class = shift;
33 2 50       15 my $params = (ref $_[0] eq 'HASH') ? shift : { @_ };
34 2   100     46 return bless( {
      100        
35             LENGTH => $params->{LENGTH} || 32,
36             CHARS => $params->{CHARS} ||
37             [ qw/ a b c d e f g h i j k l m n o p q r s t u v w x y z
38             A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
39             1 2 3 4 5 6 7 8 9 / ]
40             }, $class );
41             }
42              
43             #----------------------------------------------------------------------------+
44             # str_length($value)
45             #
46             # Set/Get the string length.
47              
48             sub str_length {
49 1     1 1 452 my ( $self, $value )
50             = validate_pos( @_,
51             { type => OBJECT },
52             { type => SCALAR, optional => 1 }
53             );
54              
55 1 50       11 return $self->{LENGTH} unless ($value);
56 0 0       0 return $self->{LENGTH} unless ($value =~ /^[\d]*$/);
57 0         0 $self->{LENGTH} = $value;
58 0         0 return $self->{LENGTH};
59             }
60              
61             #----------------------------------------------------------------------------+
62             # str_chars($value)
63             #
64             # Set/Get the string characters.
65              
66             sub str_chars {
67 1     1 1 17 my ( $self, $value )
68             = validate_pos( @_,
69             { type => OBJECT },
70             { type => SCALAR, optional => 1 }
71             );
72              
73 1 50       8 return $self->{CHARS} unless ($value);
74 0 0       0 return $self->{CHARS} unless ($value =~ /^[\w\s]*$/);
75 0         0 my @chars = split(/\s+/, $value);
76 0         0 $self->{CHARS} = \@chars;
77 0         0 return $self->{CHARS};
78             }
79              
80             #----------------------------------------------------------------------------+
81             # rand_string()
82             #
83             # Generate a new random string.
84              
85             sub rand_string {
86 1     1 1 16 my ($self)
87             = validate_pos( @_,
88             { type => OBJECT }
89             );
90              
91 1         3 my @chars = @{ $self->{CHARS} };
  1         4  
92              
93 1         3 shuffle_array(\@chars);
94              
95 1 50       36 open (DEV, "/dev/urandom") or die "Cannot open file: $!";
96 1         985 read (DEV, my $bytes, $self->{LENGTH});
97              
98 1         3 my $string;
99 1         103 my @randoms = split(//, $bytes);
100 1         12 foreach (@randoms) {
101 255         388 $string .= $chars[ ord($_) % @chars ];
102             }
103 1         28 return $string;
104             }
105              
106             #----------------------------------------------------------------------------+
107             # shuffle_array()
108             #
109             # Fisher-Yates shuffle algorithm - Perl Cookbook, Recipe 4.17
110              
111             sub shuffle_array {
112 1     1 0 3 my $array = shift;
113              
114 1         4 for (my $i = @$array; --$i;) {
115 5         73 my $j = int rand ($i + 1);
116 5 100       14 next if ($i == $j);
117 4         15 @$array[$i, $j] = @$array[$j, $i];
118             }
119             }
120              
121             1;
122              
123             __END__