【发布时间】:2014-01-02 04:27:54
【问题描述】:
#!/usr/bin/perlml -Tw
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser); # show errors in browser
use Authen::Passphrase::BlowfishCrypt;
use Bytes::Random::Secure;
print "Content-type:text/html\n\n";
# read the CGI params
my $cgi = CGI->new;
my $username = $cgi->param("username");
my $password = $cgi->param("password");
if ($username =~ /[^a-zA-Z0-9]/) { die "Illegal characters" };
if ($password =~ /[^a-zA-Z0-9]/) { die "Illegal characters" };
my $settings = './settings.cnf';
use DBI;
my $dsn =
"DBI:mysql:DB;" .
"mysql_read_default_file=$dbsettings";
my $dbh = DBI->connect($dsn, undef, undef,{RaiseError=>1})
or die "Could not connect to database: $DBI::errstr";
# check the username and password in the database
my $statement = qq{SELECT username,password FROM table WHERE username=? and password=?};
my $sth = $dbh->prepare($statement)
or die $dbh->errstr;
$sth->execute($username, $password)
or die $sth->errstr;
my ($userID) = $sth->fetchrow_array;
# create a JSON string according to the database result
my $json = ($userID) ?
qq{{"success" : "login is successful", "userid" : "$userID"}} :
qq{{"error" : "username or password is wrong"}};
# return JSON string
print $json;
$dbh->disconnect();
我现在正在尝试实现 bcrypt,在这里...但找不到任何好的示例来学习。我无法生成随机盐,因为 cpan 上的文档对于像我这样的 perl nobie 来说太晦涩难懂了。
我尝试过这样的事情:
my $gen = Authen::Passphrase::SaltedSHA512->new( passphrase => 'Sneaky!' );
my $salt = $gen->salt_hex;
my $hash = bcrypt_hash({
key_nul => 1,
cost => 8,
salt => $salt,
}, $password);
试图打印 $hash,得到一个“salt must be 16 octet long exactly”的错误
那只是我懒惰和无知.. 在黑暗中射箭。我真的需要一个很好的例子,经过 5 个小时的杂念和谷歌搜索,我的头很痛。
非常感谢您的帮助。
PS:我看到了 2-3 个非常模糊的示例,其中一个在 stackflow 中,这些并没有给我任何线索。需要新鲜的东西。
【问题讨论】:
标签: perl hash salt bcrypt blowfish