将密码哈希脚本从GO转换为Node.js

将密码哈希脚本从GO转换为Node.js

问题描述:

I have a hard time converting an existing GO script to NodeJS. It basically a hashing script which takes in 2 arguments agreedUponKey and salt and returns a password hash.

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))
}

I have managed to code up to the below point

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");

The 2 hashes are different. Any help would be greatly appreciated. I am a Noob in GO Lang

Please Note : You can use https://play.golang.org/ to compile and run the Go Script

我很难将现有的GO脚本转换为NodeJS。 它基本上是一个散列脚本,它接受两个参数 agreedUponKey strong>和 salt strong>并返回密码散列。 p>

  package main \  n 
import(
“ fmt” 
“ hash” 
“ crypto / sha256” 
)
 
func main(){
 var同意的UponKey字符串
 var salt字符串
 var h hash.Hash \  n 
同意了UpUpKey =“ giri” 
盐=“ 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  ))
} 
  code>  pre> 
 
 

我设法编码到以下位置 p>

  var crypto = require  ('crypto'); 
var convert = require('convert-string'); 
 
功能测试(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(“密码的哈希码:”,hash); 
}; 
test  (“ XYZabc987”,“ giri”); 
  code>  pre> 
 
 

两个哈希值不同。 任何帮助将不胜感激。 我是GO Lang的Noob p>

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

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");

You could verify the correct hash using this online tool.