]> jfr.im git - irc/SurrealServices/srsv.git/blob - branches/erry-devel/SrSv/Hash/Random.pm
initial commit of erry's Insp work.
[irc/SurrealServices/srsv.git] / branches / erry-devel / SrSv / Hash / Random.pm
1 # This file is part of SurrealServices.
2 #
3 # SurrealServices is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # SurrealServices is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with SurrealServices; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 package SrSv::Hash::Random;
18
19 =head1 NAME
20
21 SrSv::Hash::Random - generates random strings for use as salt
22
23 =cut
24
25 use strict;
26 #use SrSv::Conf qw( main );
27
28 use Exporter 'import';
29 BEGIN {
30 our @EXPORT = qw( randomByte randomBytes );
31 }
32
33 sub randomByte() {
34 return chr(int(rand(256)));
35 }
36
37 sub randomBytes($) {
38 my ($count) = @_;
39 my $string;
40 for(1..$count) {
41 $string .= randomByte();
42 }
43 return $string;
44 }
45
46 =cut
47 sub randomBytes($) {
48 my ($count) = @_;
49 open((my $fh), '<', '/dev/urandom');
50 binmode $fh;
51 my $bytes = '';
52 sysread($fh, $bytes, $count);
53 close $fh;
54 return $bytes;
55 }
56
57 sub randomByte() {
58 return randomBytes(1);
59 }
60 =cut
61
62 1;
63