optimized_go_tools/am_hashsalt/hashsalt.go
2024-07-08 11:14:19 +08:00

30 lines
698 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package am_hashsalt
import "golang.org/x/crypto/bcrypt"
/*
* 哈希加盐加密
* 参数:
* originData string明文
*/
func HashData(originData string) (string, error) {
// 哈希加盐加密
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(originData), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(hashedPassword), nil
}
/*
* 比对密文和明文
* 参数:
* 1. hashedData string哈希加盐加密过后的数据
* 2. data string要比对的明文数据
*/
func CompareData(hashedData, data string) bool {
// 比对加密后数据和所需数据
err := bcrypt.CompareHashAndPassword([]byte(hashedData), []byte(data))
return err == nil
}