Python的pbkdf2_sha256.verify的NodeJS实现 [英] NodeJS implementation for Python's pbkdf2_sha256.verify

查看:141
本文介绍了Python的pbkdf2_sha256.verify的NodeJS实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将此Python代码转换为NodeJS:

I have to translate this Python code to NodeJS:

from passlib.hash import pbkdf2_sha256
pbkdf2_sha256.verify('12345678', '$pbkdf2-sha256$2000$8R7jHOOcs7YWImRM6V1LqQ$CIdNv8YlLlCZfeFJihZs7eQxBsauvVfV05v07Ca2Yzg')
>> True

上面的代码是完整的代码,即没有其他参数/​​设置(只需运行 pip安装passlib ,然后运行它以安装 passlib 软件包)。

The code above is the entire code, i.e. there is no othe parameters/settings (just run pip install passlib before you run it to install the passlib package).

我正在寻找Node中 validatePassword 函数的正确实现,它将通过此​​积极的实现测试:

I am looking for the correct implementation of validatePassword function in Node that will pass this positive implementation test:

validatePassword('12345678', '$pbkdf2-sha256$2000$8R7jHOOcs7YWImRM6V1LqQ$CIdNv8YlLlCZfeFJihZs7eQxBsauvVfV05v07Ca2Yzg')
>> true

这是文档

我试图按照此处的答案进行操作,上面的Python代码,但是该解决方案没有通过测试。

I tried to follow the answers from here with the data from the Python code above, but that solutions didn't pass the test.

我希望此实现有一些帮助(最好使用内置的NodeJS crypto 包)。

I would appreciate some help with this implementation (preferably using built-in NodeJS crypto package).

谢谢。

推荐答案

这将起作用:

const crypto = require('crypto')
function validatePassword(secret, format) {
    let parts = format.split('$')
    return parts[4] == crypto.pbkdf2Sync(secret, Buffer.from(parts[3].replace(/\./g, '+') + '='.repeat(parts[3].length % 3), 'base64'),
        +parts[2], 32, parts[1].split('-')[1]).toString('base64').replace(/=/g, '').replace(/\+/g, '.')
}

这篇关于Python的pbkdf2_sha256.verify的NodeJS实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆