将密码哈希脚本从 GO 转换为 Nodejs

Converting a Password Hashing Script from GO to Nodejs

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

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.

验证正确的散列