Bgrypt密码哈希在Golang(兼容Node.js)?

我使用Node.js护照设置了一个用于用户身份验证的网站。

现在我需要迁移到Golang,并需要使用保存在db中的用户密码进行身份验证。

Node.js加密代码是:

var bcrypt = require('bcrypt');

    bcrypt.genSalt(10,function(err,salt) {
        if(err) return next(err);

        bcrypt.hash(user.password,salt,hash) {
            if(err) return next(err);
            user.password = hash;
            next();
        });
    });

如何使同一个散列的字符串作为Node.js bcrypt与Golang?

使用 golang.org/x/crypto/bcrypt软件包,我相信相当于:
hashedPassword,err := bcrypt.GenerateFromPassword(password,bcrypt.DefaultCost)

工作示例:

package main

import (
    "golang.org/x/crypto/bcrypt"
    "fmt"
)

func main() {
    password := []byte("MyDarkSecret")

    // Hashing the password with the default cost of 10
    hashedPassword,bcrypt.DefaultCost)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(hashedPassword))

    // Comparing the password with the hash
    err = bcrypt.CompareHashAndPassword(hashedPassword,password)
    fmt.Println(err) // nil means it is a match
}

相关文章

什么是Go的接口? 接口可以说是一种类型,可以粗略的理解为他...
1、Golang指针 在介绍Golang指针隐式间接引用前,先简单说下...
1、概述 1.1 Protocol buffers定义 Protocol buffe...
判断文件是否存在,需要用到"os"包中的两个函数: os.Stat(...
1、编译环境 OS :Loongnix-Server Linux release 8.3 CPU指...
1、概述 Golang是一种强类型语言,虽然在代码中经常看到i:=1...