File Coverage

blib/lib/Data/Rand/Obscure.pm
Criterion Covered Total %
statement 30 30 100.0
branch 1 2 50.0
condition 2 3 66.6
subroutine 12 12 100.0
pod 3 3 100.0
total 48 50 96.0


line stmt bran cond sub pod time code
1             package Data::Rand::Obscure;
2              
3 3     3   69056 use warnings;
  3         9  
  3         100  
4 3     3   15 use strict;
  3         7  
  3         166  
5              
6             =head1 NAME
7              
8             Data::Rand::Obscure - Generate (fairly) random strings easily.
9              
10             =head1 VERSION
11              
12             Version 0.021
13              
14             =cut
15              
16             our $VERSION = '0.021';
17              
18             =head1 SYNOPSIS
19              
20             use Data::Rand::Obscure qw/create create_b64/;
21              
22             # Some random hexadecimal string value.
23             $value = create;
24              
25             ...
26              
27             # Random base64 value:
28             $value = create_b64;
29              
30             # Random binary value:
31             $value = create_bin;
32              
33             # Random hexadecimal value:
34             $value = create_hex;
35              
36             ...
37              
38             # A random value containing only hexadecimal characters and 103 characters in length:
39             $value = create_hex(length => 103);
40              
41             # Object-orientated
42              
43             my $generator = Data::Rand::Obscure->new(seeder => sub { ... # My special seeding algorithm # },
44             digester => sub { return $my_favorite_digester; });
45              
46             $value = $generator->create;
47              
48             $value = $generator->create_hex(length => 32);
49              
50             =head1 DESCRIPTION
51              
52             Data::Rand::Obscure provides a method for generating random hexadecimal, binary, and base64 strings of varying length.
53             To do this, it first generates a pseudo-random "seed" and hashes it using a SHA-1, SHA-256, or MD5 digesting algorithm.
54              
55             Currently, the seed generator is:
56              
57             join("", <an increasing counter>, time, rand, $$, {})
58              
59             You can use the output to make obscure "one-shot" identifiers for cookie data, "secret" values, etc.
60              
61             Values are not GUARANTEED to be unique (see L<Data::UUID> for that), but should be sufficient for most purposes.
62              
63             This package was inspired by (and contains code taken from) the L<Catalyst::Plugin::Session> package by Yuval Kogman
64              
65             =cut
66              
67 3     3   2469 use Digest;
  3         1646  
  3         77  
68 3     3   2550 use Carp::Clan;
  3         14046  
  3         20  
69              
70             my $SINGLETON; # Could be cute and keep this untouchable, but why bother.
71             sub singleton() {
72 1021   66 1021 1 7071 return $SINGLETON ||= __PACKAGE__->new;
73             }
74              
75             =head1 METHODS
76              
77             =head2 Data::Rand::Obscure->new([ seeder => <seeder>, digester => <digester> ])
78              
79             Returns a Data::Rand::Obscure::Generator with the following methods:
80              
81             create
82             create_hex
83             create_bin
84             create_b64
85              
86             You may optionally supply a seeder subroutine, which is called everytime a new value is to be generated.
87             It should return some seed value that will be digested.
88              
89             You may also optionally supply a digester subroutine, which is also called everytime a new value is to be generated.
90             It should return a L<Digest> object of some kind (which will be used to take the digest of the seed value).
91              
92             =cut
93              
94             sub new {
95 3     3   2466 use Data::Rand::Obscure::Generator; # Long enough? :(
  3         11  
  3         29  
96 2     2 1 19 my $class = shift;
97 2 50       11 croak "You should extend Data::Rand::Obscure::Generator instead" unless $class eq __PACKAGE__;
98 2         21 return Data::Rand::Obscure::Generator->new(@_);
99             }
100              
101             =head1 EXPORTS
102              
103             =cut
104              
105 3     3   236 use vars qw/@ISA @EXPORT_OK/; use Exporter(); @ISA = qw/Exporter/;
  3     3   5  
  3         146  
  3         16  
  3         4  
  3         325  
106             @EXPORT_OK = qw/create create_hex create_bin create_b64/;
107              
108             =head2 $value = create([ length => <length> ])
109              
110             =head2 $value = create_hex([ length => <length> ])
111              
112             Create a random hexadecimal value and return it. If <length> is specificied, then the string will be <length> characters long.
113              
114             If <length> is specified and not a multiple of 2, then $value will technically not be a valid hexadecimal value.
115              
116             =head2 $value = create_bin([ length => <length> ])
117              
118             Create a random binary value and return it. If <length> is specificied, then the value will be <length> bytes long.
119              
120             =head2 $value = create_b64([ length => <length> ])
121              
122             Create a random base64 value and return it. If <length> is specificied, then the value will be <length> bytes long.
123              
124             If <length> is specified, then $value is (technically) not guaranteed to be a "legal" b64 value (since padding may be off, etc).
125              
126             =cut
127              
128             sub create {
129 250     250 1 115736 return singleton->create_hex(@_);
130             }
131              
132             for my $name (map { "create_$_" } qw/hex bin b64/) {
133 3     3   17 no strict 'refs';
  3         7  
  3         267  
134             *$name = sub {
135 771     771   11472 return singleton->$name(@_);
136             };
137             }
138              
139             =head1 FUNCTIONS
140              
141             =head2 singleton
142              
143             Returns the Data::Rand::Obscure::Generator used in the above exported functions
144             You probably don't need to use this.
145              
146             =head1 AUTHOR
147              
148             Robert Krimen, C<< <rkrimen at cpan.org> >>
149              
150             =head1 BUGS
151              
152             Please report any bugs or feature requests to C<bug-data-rand-obscure at rt.cpan.org>, or through
153             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Rand-Obscure>. I will be notified, and then you'll
154             automatically be notified of progress on your bug as I make changes.
155              
156              
157              
158              
159             =head1 SUPPORT
160              
161             You can find documentation for this module with the perldoc command.
162              
163             perldoc Data::Rand::Obscure
164              
165              
166             You can also look for information at:
167              
168             =over 4
169              
170             =item * RT: CPAN's request tracker
171              
172             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Rand-Obscure>
173              
174             =item * AnnoCPAN: Annotated CPAN documentation
175              
176             L<http://annocpan.org/dist/Data-Rand-Obscure>
177              
178             =item * CPAN Ratings
179              
180             L<http://cpanratings.perl.org/d/Data-Rand-Obscure>
181              
182             =item * Search CPAN
183              
184             L<http://search.cpan.org/dist/Data-Rand-Obscure>
185              
186             =back
187              
188              
189             =head1 ACKNOWLEDGEMENTS
190              
191             This package was inspired by (and contains code taken from) the L<Catalyst::Plugin::Session> package by Yuval Kogman
192              
193             =head1 COPYRIGHT & LICENSE
194              
195             Copyright 2007 Robert Krimen, all rights reserved.
196              
197             This program is free software; you can redistribute it and/or modify it
198             under the same terms as Perl itself.
199              
200              
201             =cut
202              
203             1; # End of Data::Rand::Obscure
204              
205             __END__