当前位置:网站首页>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
边栏推荐
- unity常见的问题(一)
- STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
- SSM framework series - data source configuration day2-1
- The accuracy and speed are perfectly balanced, and the latest image segmentation SOTA model is released!!!
- What are the forms of attack and tampering on the home page of the website
- 【每日一题】棋盘问题
- 免费试用一个月的服务器,并附上教程
- About the 'enum' enumeration type and structure.
- Image attribute of input: type attribute of fashion cloud learning -h5
- 【蓝桥杯】4月17日省赛刷题训练(前3道题)
猜你喜欢
Try the server for one month for free, and attach the tutorial
Servlet监听器&过滤器介绍
SSL certificate refund instructions
Introduction to kubernetes
精度、速度完美平衡,最新图像分割SOTA模型重磅发布!!!
Softbank vision fund entered the Web3 security industry and led a new round of investment of US $60 million in certik
Plato Farm-以柏拉图为目标的农场元宇宙游戏
22. Bracket generation
How to prevent the website from being hacked and tampered with
How to click an object to play an animation
随机推荐
mysql中 innoDB执行过程分析
实现一个盒子在父盒子中水平垂直居中的几种“姿势”
BUUCTF WEB [BUUCTF 2018]Online Tool
Pytorch: a pit about the implementation of gradreverselayer
mysql支持ip访问
BUUCTF WEB [GXYCTF2019]禁止套娃
Plato farm - a game of farm metauniverse with Plato as the goal
ZigBee CC2530 minimum system and register configuration (1)
Buuctf Web [gxyctf2019] no dolls
leetcode:437. Path sum III [DFS selected or not selected?]
Importerror after tensorflow installation: DLL load failed: the specified module cannot be found, and the domestic installation is slow
Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy
mysql8安装
BUUCTF WEB [BUUCTF 2018]Online Tool
如何实现点击一下物体播放一次动画
Stm32cubeprogrammer basic instructions
World Book Day: I'd like to recommend these books
精度、速度完美平衡,最新图像分割SOTA模型重磅发布!!!
Use source insight to view and edit source code
产品开发都应该知道的8个网站,增强工作体验