node.js - 将密码哈希脚本从 GO 转换为 Nodejs

标签 node.js go hash passwords sha256

我很难将现有的 GO 脚本转换为 NodeJS。它基本上是一个哈希脚本,它接受 2 个参数 agreedUponKeysalt 并返回密码哈希。

package main

import (
    "fmt"
    "hash"
    "crypto/sha256"
)

func main() {
    var agreedUponKey string
    var salt string
    var h hash.Hash

    agreedUponKey = "giri"
    salt = "XYZabc987"

    h = sha256.New()
    h.Write([]byte(agreedUponKey))
    h.Write([]byte(salt))

    sha256Sum := h.Sum(nil)
    print("calculated passwordHash:", sha256Sum)

    var hexHash = make([]byte, 0, 64)
    for _, v := range sha256Sum {
        hexHash = append(hexHash,[]byte(fmt.Sprintf("%02x", v))...)
    }

    print("calculated passwordHash:", string(hexHash))
}

我已经设法编写了以下代码

var crypto = require('crypto');
var convert = require('convert-string');

function test(pwd,key) {
  console.log("Password :",pwd);
  var byteKey=convert.stringToBytes(key);
  var bytePwd=convert.stringToBytes(pwd);    
  var hash = crypto.createHash('sha256').update(byteKey+bytePwd).digest('base64');
  console.log("hashcode of password :",hash);
};
test("XYZabc987","giri");

2 个哈希值不同。任何帮助将不胜感激。我是 GO Lang 的菜鸟

请注意:您可以使用 https://play.golang.org/编译并运行 Go 脚本

最佳答案

var crypto = require('crypto');
function test(pwd, key) {
    var input = key.concat(pwd)
    var hash = crypto.createHash('sha256').update(input).digest('hex');
    console.log("hashcode of password :", hash);
};
test("XYZabc987", "giri");

您可以使用此 online tool 验证正确的哈希值.

关于node.js - 将密码哈希脚本从 GO 转换为 Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42063110/

相关文章:

angularjs - 如何将倒计时器同步给所有用户

c++ - 对 C++ unordered_map 和散列冲突感到困惑

c# - 报告哈希进度

hash - pbkdf2 key 长度

javascript - 覆盖 Jade 脚本。堵塞

javascript - Angular 7 : NullInjectorError: No provider for PagerService

node.js - "npm i"显示损坏的输出 ( "Enter passphrase for key...") 和微调器,输入密码不执行任何操作

html - 来自 html.NewTokenizer.Token() 的意外 HTML 标记

Go 中的递归

go - 将所有外部 golang 模块导入一个文件,然后从该文件导入?