当前位置:网站首页>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
边栏推荐
- Object.keys后key值数组乱序的问题
- The continuous construction of the Internet industry platform is not only able to collect traffic
- Analysis of InnoDB execution process in MySQL
- Can I take the CPDA data analyst certificate for 0 foundation
- C#,二维贝塞尔拟合曲线(Bézier Curve)参数点的计算代码
- At instruction of nbiot
- SSM框架系列——注解开发day2-2
- 云原生KubeSphere部署Mysql
- Record a website for querying compatibility, string Replaceall() compatibility error
- NPDP|产品经理如何做到不会被程序员排斥?
猜你喜欢
Unlock openharmony technology day! The annual event is about to open!
The accuracy and speed are perfectly balanced, and the latest image segmentation SOTA model is released!!!
XinChaCha Trust SSL Organization Validated
Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy
box-sizing
产品开发都应该知道的8个网站,增强工作体验
Remote sensing image classification and recognition system based on convolutional neural network
Servlet监听器&过滤器介绍
Pytorch: a pit about the implementation of gradreverselayer
4. DRF permission & access frequency & filtering & sorting
随机推荐
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
The accuracy and speed are perfectly balanced, and the latest image segmentation SOTA model is released!!!
Customize the shortcut options in El date picker, and dynamically set the disabled date
[Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)
Buuctf Web [gxyctf2019] no dolls
Try the server for one month for free, and attach the tutorial
Luogu p5540 [balkanoi2011] timeismoney | minimum product spanning tree problem solution
World Book Day: I'd like to recommend these books
Wonderful review | the sixth issue of "source" - open source economy and industrial investment
大家帮我看一下这是啥情况,MySQL5.5的。谢了
leetcode:437. 路径总和 III【dfs 选还是不选?】
leetcode:437. Path sum III [DFS selected or not selected?]
Date time type in database
BUUCTF WEB [BUUCTF 2018]Online Tool
Remote sensing image classification and recognition system based on convolutional neural network
leetcode-791. Custom string sorting
Redis deployment of cloud native kubesphere
免费试用一个月的服务器,并附上教程
Keyword interpretation and some APIs in RT thread