【问题标题】:Can anyone translate this function from python to javascript? [closed]任何人都可以将此函数从 python 转换为 javascript 吗? [关闭]
【发布时间】:2019-07-12 16:53:26
【问题描述】:
我在 python 中有一个脚本,它需要一个盐并使用返回证明
sha256.
我不太了解 javascript 或任何库。
import sys
from hashlib import sha256
def generate_proof(salt):
secret_salt = SECRET + salt
hexadecimal = secret_salt.decode('hex')
proof = sha256(hexadecimal).hexdigest()
return proof
有人可以翻译或解释我如何将这种方法翻译成javascript吗?
我认为我最大的问题是在 JS 中找到 sha256 等效库。
【问题讨论】:
标签:
javascript
python-2.7
authentication
sha256
【解决方案1】:
// node.js
const filename = process.argv[2];
const crypto = require('crypto');
const fs = require('fs');
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(filename);
input.on('readable', () => {
// Only one element is going to be produced by the
// hash stream.
const data = input.read();
if (data)
hash.update(data);
else {
console.log(`${hash.digest('hex')} ${filename}`);
}
});