更新:完整的答案是 Perl 代码:
my $cipher = Crypt::EksBlowFish->new($cost, $salt, $key);
相当于这个 Python 代码:
bf = Eksblowfish()
bf.expandkey(salt, key)
for i in xrange(cost << 1):
bf.expandkey(0, key)
bf.expandkey(0, salt)
请参阅此 repo 以获取示例代码:https://github.com/erantapaa/python-bcrypt-tests
原答案:
部分答案...
我假设你是这样调用这个 Perl 代码的:
use Crypt::EksBlowfish;
my $cipher = Crypt::EksBlowFish->new($cost, $salt, $key);
$encoded = $cipher->encrypt("some plaintext");
new 方法由 lib/Crypt/EksBlowfish.xs 中的 C 函数 setup_eksblowfish_ks() 实现。这看起来和Python代码(link)中的expandKey方法一样@
主要区别在于 Python 方法中不存在 $cost 参数。在 Perl 代码中,$cost 参数控制在设置键调度后此循环执行的次数:
for(count = 1U << cost; count--; ) {
for(j = 0; j != 2; j++) {
merge_key(j == 0 ? expanded_key : expanded_salt, ks);
munge_subkeys(ks);
}
}
Perl ->encrypt() 方法加密一个 64 位字。等效的 Python 代码是:
bf.cipher(xl, xr, bf.ENCRYPT)
其中 xl 和 xr 是分别代表左 32 位和右 32 位的整数。
所以食谱应该是这样的:
- 创建 Python 对象:
bf = EksBlowfish()
- 初始化密钥计划:
bf.expandkey(salt, key)
- 使用成本参数 (TBD) 进一步调整关键计划
- 用
bf.cipher(xl, xr, bf.ENCRYPT)加密