doufu1504 2017-02-06 08:16
浏览 73
已采纳

将密码哈希脚本从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

  • 写回答

1条回答 默认 最新

  • dqkx69935 2017-02-06 09:43
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!