From 9f84c2ba374a07fcf12fdf41c0d6f1fc3f315655 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 17 May 2011 14:42:25 +0200 Subject: Move create_salt to App::Raps2::Password, make $pw->salt() return current salt --- lib/App/Raps2/Password.pm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib/App/Raps2') diff --git a/lib/App/Raps2/Password.pm b/lib/App/Raps2/Password.pm index 10431ed..b236b7b 100644 --- a/lib/App/Raps2/Password.pm +++ b/lib/App/Raps2/Password.pm @@ -17,6 +17,10 @@ sub new { $conf{cost} //= 12; + if (not defined $conf{salt}) { + $conf{salt} = create_salt(); + } + if (not (defined $conf{salt} and length($conf{salt}) == 16)) { confess('incorrect salt length'); } @@ -30,9 +34,24 @@ sub new { return bless($ref, $obj); } +sub create_salt { + my ($self) = @_; + my $salt = q{}; + + for (1 .. 16) { + $salt .= chr(0x21 + int(rand(90))); + } + + return $salt; +} + sub salt { my ($self, $salt) = @_; + if (not defined $salt) { + return $self->{salt}; + } + if (not (defined $salt and length($salt) == 16)) { confess('incorrect salt length'); } -- cgit v1.2.3