当前位置:网站首页>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
边栏推荐
- Use source insight to view and edit source code
- BUUCTF WEB [BJDCTF2020]The mystery of ip
- Packet capturing and sorting -- TCP protocol [8]
- Web17 -- use of El and JSTL
- 4.DRF 权限&访问频率&过滤&排序
- Teach you to quickly develop a werewolf killing wechat applet (with source code)
- [csnote] ER diagram
- Resolve disagrees about version of symbol device_ create
- Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
- SSL certificate refund instructions
猜你喜欢

SSM framework series - data source configuration day2-1

0基础可以考CPDA数据分析师证书吗

Free and open source agricultural Internet of things cloud platform (version: 3.0.1)

98. Error s.e.errormvcautoconfiguration $staticview reported by freemaker framework: cannot render error page for request

SSM framework series - JUnit unit test optimization day2-3

云原生KubeSphere部署Mysql

Redis deployment of cloud native kubesphere

How to click an object to play an animation

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

Unlock openharmony technology day! The annual event is about to open!
随机推荐
STM32控制步进电机(ULN2003+28byj)
Softbank vision fund entered the Web3 security industry and led a new round of investment of US $60 million in certik
The El table horizontal scroll bar is fixed at the bottom of the visual window
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
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
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.
Web17——EL与JSTL的使用
SSM框架系列——注解开发day2-2
Jiachen chapter Genesis "inner universe" joint Edition
If you were a golang interviewer, what questions would you ask?
4.DRF 权限&访问频率&过滤&排序
云原生KubeSphere部署Mysql
22. Bracket generation
标签与路径
进程虚拟地址空间区域划分
风尚云网学习-h5的input:type属性的image属性
About the 'enum' enumeration type and structure.
【每日一题】棋盘问题
PHP generates JSON to process Chinese
BUUCTF WEB [BJDCTF2020]The mystery of ip