- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Dancer2::Plugin::Passphrase 生成了一个 SHA-1 散列,代码如下:
get '/generate' => {
my $phrase = passphrase('my_password')->generate({ algorithm => 'SHA-1'});
return $phrase->rfc2307();
};
结果看起来像这样:
{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK
这就是我存储在我的 PostgreSQL 数据库中的内容。
我正在使用 Dancer2::Plugin::Auth::Extensible 作为我的登录解决方案,但我还没有让它使用加密密码。我将一个测试帐户放入我的数据库中,其中用户名='test' 和密码='test',并且工作正常。但是 username='test2' and password='{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK' 不起作用。登录页面只是默默地失败并重新加载。
我打开了 DBI_TRACE,除了使用明文密码的帐户返回以下信息外,两者之间没有太大区别:
[glm::App:3515] debug @2016-05-10 21:02:23> users accepted user test in /usr/local/share/perl/5.20.2/Dancer2/Core/Route.pm l. 137
并且使用加密密码的帐户返回:
[glm::App:3523] core @2016-05-10 21:04:21> looking for get /login in /usr/local/share/perl/5.20.2/Dancer2/Core/App.pm l. 1210
[glm::App:3523] core @2016-05-10 21:04:21> Entering hook core.app.before_request in (eval 62) l. 1
[glm::App:3523] core @2016-05-10 21:04:21> Entering hook core.app.after_request in (eval 62) l. 1
127.0.0.1 - - [10/May/2016:21:04:21 +0100] "POST /login?return_url=%2F HTTP/1.1" 401 383 "http://localhost:5000/login?return_url=%2F" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0"
我确定我遗漏了什么,但是 the CPAN page没有详细说明如何处理加密密码。它只是说这很容易。我想我是在读它,因为“加密的密码将自动处理”。我错过了什么?
配置
这是我配置的相关部分
plugins:
Auth::Extensible:
realms:
users:
provider: 'Database'
Database:
dsn: 'dbi:Pg:service=test'
App.pm
下面是我在 App.pm 中所做的。你可以看到我只是想要求登录主页。也许我需要一些“/登录”代码?
package glm::App;
use Dancer2;
use Dancer2::Plugin::Database;
use Dancer2::Plugin::Auth::Extensible;
use Dancer2::Plugin::Passphrase;
use Template;
our $VERSION = '0.1';
get '/' => require_login sub {
my $sth = database->prepare('SELECT name FROM product', { RaiseError => 1 });
$sth->execute();
template 'create_list', {
'products' => $sth->fetchall_hashref('name'),
};
};
get '/generate'=> sub {
my $phrase = passphrase('my_password')->generate({ algorithm => 'SHA-1' });
return $phrase->rfc2307(); # right now I just manually copy and paste this into the database
};
我的数据库遵循 suggested schema用于用户、密码和角色。
也许我能想到的唯一其他相关信息是,如果我使用 Digest 无法识别的加密方案,我会从 Digest.pm 收到错误消息。这似乎表明它正在识别散列密码并试图对其进行解密,但无论出于何种原因,它都无法正常工作。或者它正在工作并重定向回主页...但是为什么它不使用“测试,测试”来做到这一点?
最佳答案
TL;DR您正在使用两种不同的散列方法,因此生成的散列不兼容。
Dancer2::Plugin::Auth::Extensible::Provider::Database使用 Crypt::SaltedHash:
sub encrypt_password {
my ($self, $password, $algorithm) = @_;
$algorithm ||= 'SHA-1';
my $crypt = Crypt::SaltedHash->new(algorithm => $algorithm);
$crypt->add($password);
$crypt->generate;
}
这会生成如下哈希:
{SSHA}qTEaPf8KRPt6XBQXIlQhlWstgBz64coW
将其与您从 Dancer2::Plugin::Passphrase 获得的内容进行比较:
{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK
请注意长度不同。 Dancer2::Plugin::Passphrase 默认使用 16 字节的盐,而 Crypt::SaltedHash 使用 4 字节的盐。
尽管您可以告诉 Dancer2::Plugin::Passphrase 使用 4 字节的盐,但在任何地方都使用 Crypt::SaltedHash 会容易得多。 Dancer2::Plugin::Auth::Extensible documentation解释如何做到这一点:
A simple script called
generate-crypted-password
to generate RFC2307-style hashed passwords is included, or you can use Crypt::SaltedHash yourself to do so, or use theslappasswd
utility if you have it installed.
例如:
$ generate-crypted-password
Enter plain-text password ?> foo
Result: {SSHA}zdXPS0QqxyKlzXwHxjJ3rsU19Td4ABzW
关于postgresql - Dancer2 Auth::Extensible 不接受散列密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37148248/
我是一名优秀的程序员,十分优秀!