当前位置:网站首页>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
边栏推荐
- 洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
- Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
- BUUCTF WEB [BJDCTF2020]The mystery of ip
- Zero trust in network information security
- Ad20 supplementary note 3 - shortcut key + continuous update
- 风尚云网学习-h5的input:type属性的image属性
- Kubernetes 入门教程
- Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
- Labels and paths
- SSM framework series - annotation development day2-2
猜你喜欢
Homomorphic encryption technology learning
If you were a golang interviewer, what questions would you ask?
Calculate the past date and days online, and calculate the number of live days
Kubernetes 入門教程
【蓝桥杯】4月17日省赛刷题训练(前3道题)
有趣的IDEA插件推荐,给你的开发工作增添色彩
进程虚拟地址空间区域划分
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
Kubernets Getting started tutoriel
SSM framework series - JUnit unit test optimization day2-3
随机推荐
There is no need to crack the markdown editing tool typora
Keyword interpretation and some APIs in RT thread
Use compressorjs to compress pictures, optimize functions, and compress pictures in all formats
标签与路径
航芯技术分享 | ACM32 MCU安全特性概述
C#,二维贝塞尔拟合曲线(Bézier Curve)参数点的计算代码
Kubernetes 入门教程
Introduction to kubernetes
Aviation core technology sharing | overview of safety characteristics of acm32 MCU
Jiachen chapter Genesis "inner universe" joint Edition
4.DRF 权限&访问频率&过滤&排序
leetcode:437. Path sum III [DFS selected or not selected?]
Buuctf Web [bjdctf2020] zjctf, but so
box-sizing
[daily question] chessboard question
Fashion cloud learning - input attribute summary
Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
Introduction to servlet listener & filter
5 free audio material websites, recommended collection
22. 括号生成