当前位置:网站首页>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
边栏推荐
- Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
- 洛谷P3236 [HNOI2014]画框 题解
- Record a website for querying compatibility, string Replaceall() compatibility error
- 航芯技术分享 | ACM32 MCU安全特性概述
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- 梳理网络IP代理的几大用途
- Deploying MySQL in cloud native kubesphere
- 标签与路径
- Jiachen chapter Genesis "inner universe" joint Edition
- Record some NPM related problems (messy records)
猜你喜欢

Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward

CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer

Servlet监听器&过滤器介绍

8 websites that should be known for product development to enhance work experience
![[daily question] chessboard question](/img/70/54a924d07c935965e54b96d9b07278.png)
[daily question] chessboard question

Mysql8 installation

Idea的src子文件下无法创建servlet

Can I take the CPDA data analyst certificate for 0 foundation

Remote sensing image classification and recognition system based on convolutional neural network

PC starts multiple wechat at one time
随机推荐
只是不断地建构平台,不断地收拢流量,并不能够做好产业互联网
SSM framework series - JUnit unit test optimization day2-3
Use source insight to view and edit source code
Homomorphic encryption technology learning
Zero trust in network information security
How to click an object to play an animation
Packet capturing and sorting -- TCP protocol [8]
Process virtual address space partition
Buuctf Web [gxyctf2019] no dolls
Markdown grammar learning
mysql中 innoDB执行过程分析
世界读书日:我想推荐这几本书
SSM framework series - annotation development day2-2
Deploying MySQL in cloud native kubesphere
Web17 -- use of El and JSTL
Get the punch in record of nailing attendance machine
Record Alibaba cloud server mining program processing
Customize the shortcut options in El date picker, and dynamically set the disabled date
云原生KubeSphere部署Redis
Plato farm - a game of farm metauniverse with Plato as the goal