zoukankan      html  css  js  c++  java
  • GO语言密码加解密(bcrypt)

    bcrypt加密算法介绍
    bcrypt算法对于同一个密码,每次生成的hash不一样

    业务流程

    对用户注册时传入的密码进行加密
    //此方法生成hash值

    HashAndSalt([]byte("password"))     //password为string类型
    

    将第一次生成的密码hash值存入数据库

    密码验证

    hashedPwd为保存在数据库中的密码hash,password为前端传过来要验证的密码
    ComparePasswords 方法返回true说明密码验证通过

    ValidatePasswords(hashedPwd string,[]byte("password"))      
    

    封装加密和验证方法

    package utils
    
    import (
        "golang.org/x/crypto/bcrypt"
    )
    
    // 加密密码
    func HashAndSalt(pwd []byte) string {
        hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)
        if err != nil {
    
        }
        return string(hash)
    }
    
    // 验证密码
    func ValidatePasswords(hashedPwd string, plainPwd []byte) bool {
        byteHash := []byte(hashedPwd)
        err := bcrypt.CompareHashAndPassword(byteHash, plainPwd)
        if err != nil {
            return false
        }
        return true
    }
    链接:http://events.jianshu.io/p/156bed22328f
    
  • 相关阅读:
    wifi应用领域
    wifi主要功能
    Wi-Fi技术原理
    自适应通信类型与基本原理
    自适应通信发展背景
    自适应通信
    无线通信技术
    无线通信的一些专业术语
    无线通信
    Bluetooth vs. Wi-Fi(IEEE 802.11)
  • 原文地址:https://www.cnblogs.com/hsyw/p/14847693.html
Copyright © 2011-2022 走看看