当前位置:网站首页>Golang implements MD5, sha256 and bcrypt encryption
Golang implements MD5, sha256 and bcrypt encryption
2022-04-23 12:56:00 【Play ha ha 527】
golang Realization MD5,SHA256,bcrypt encryption
- Preface
- One 、MD5 Algorithm It's not safe enough now , It's easy to crack through the rainbow watch
- Two 、SHA256 Algorithm relative MD5 More secure , Of course SHA512 Longer and safer , Greater performance requirements
- 3、 ... and 、bcrypt Algorithm The cost of cracking is higher , More secure ,bcrypt The algorithm includes random salt addition , It's more convenient
Preface
The encryption algorithm can certainly decrypt .MD5,SHA256,bcrypt Algorithms are irreversible and non decryptable , So it's not an encryption algorithm , for example 2+3 obtain 5, however 5 I don't know if 2+3 obtain , Or maybe 1+4,0+5, So it's irreversible . What is often confused is called encryption algorithm , But the essence is hash algorithm .
One 、MD5 Algorithm It's not safe enough now , It's easy to crack through the rainbow watch
The code is as follows ( Example ):
func MD5(password string) string {
hash := MD5.New()
hash.Write([]byte(password))
res:=hex.EncodeToString(hash.Sum(nil))
fmt.Println(len(res))
return res
}
//MD5 Add salt and encrypt It's safer than not adding salt , But it's not safe enough
func MD5(password string) string {
const salt = "2021/10/21"// Custom salt
hash := MD5.New()
hash.Write([]byte(password+salt))// Custom combination of password and salt
res:=hex.EncodeToString(hash.Sum(nil))
fmt.Println(len(res))
return res
}
Two 、SHA256 Algorithm relative MD5 More secure , Of course SHA512 Longer and safer , Greater performance requirements
The code is as follows ( Example ):
func SHA256(password string) string {
hash := sha256.New()
hash.Write([]byte(password))
res:=hex.EncodeToString(hash.Sum(nil))
fmt.Println(len(res))
return res
}
// Add salt and encrypt
func SHA256(password string) string {
const salt = "2021/10/21"// Custom salt
hash := sha256.New()
hash.Write([]byte(password+salt))// Custom combination of password and salt
res:=hex.EncodeToString(hash.Sum(nil))
fmt.Println(len(res))
return res
}
3、 ... and 、bcrypt Algorithm The cost of cracking is higher , More secure ,bcrypt The algorithm includes random salt addition , It's more convenient
bcrypt Installation package go get golang.org/x/crypto/bcrypt
The code is as follows ( Example ):
// The password code is Hashi value
func HashPassword(password string) (string, error) {
start:=time.Now()
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)//bcrypt.DefaultCost Default value 10, Code once 100ms within , The value can be increased , Increase the time and cost of cracking , For example, set to 14, Code once 1s above
time:=time.Since(start)
log.Printf("Encode Password time:%s",time)
return string(bytes), err
}
// Verify password , For example, in the actual business, the login password is compared with the hash value stored in the database , To verify equality
func MatchPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
版权声明
本文为[Play ha ha 527]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230615231263.html
边栏推荐
- V-model binding value in El select, data echo only displays value, not label
- 解决disagrees about version of symbol device_create
- Record some NPM related problems (messy records)
- 产品开发都应该知道的8个网站,增强工作体验
- 数据库中的日期时间类型
- Resolve disagrees about version of symbol device_ create
- Kubernets Getting started tutoriel
- SSM framework series - data source configuration day2-1
- SSM framework series - JUnit unit test optimization day2-3
- 网站首页文件被攻击篡改的形式有哪些
猜你喜欢

Software testing weekly (issue 68): the best way to solve difficult problems is to wait and see the changes and push the boat with the current.

22. 括号生成

Object.keys后key值数组乱序的问题

The quill editor image zooms, multiple rich text boxes are used on one page, and the quill editor upload image address is the server address

如何实现点击一下物体播放一次动画

风尚云网学习-input属性总结

How to click an object to play an animation

STM32控制步进电机(ULN2003+28byj)

NPDP | how can product managers not be excluded by programmers?

SSM framework series - JUnit unit test optimization day2-3
随机推荐
The accuracy and speed are perfectly balanced, and the latest image segmentation SOTA model is released!!!
BUUCTF WEB [GXYCTF2019]禁止套娃
[csnote] ER diagram
C#,二维贝塞尔拟合曲线(Bézier Curve)参数点的计算代码
Record some NPM related problems (messy records)
Idea的src子文件下无法创建servlet
Ad20 supplementary note 3 - shortcut key + continuous update
Huawei cloud MVP email
leetcode-791. 自定义字符串排序
Buuctf Web [gxyctf2019] no dolls
SSM framework series - data source configuration day2-1
Resolve disagrees about version of symbol device_ create
8 websites that should be known for product development to enhance work experience
ZigBee CC2530 minimum system and register configuration (1)
Common problems of unity (1)
(1) Openjuterpyrab comparison scheme
leetcode:437. 路径总和 III【dfs 选还是不选?】
Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
洛谷P3236 [HNOI2014]画框 题解
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box