当前位置:网站首页>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
边栏推荐
- SSM framework series - JUnit unit test optimization day2-3
- 4. DRF permission & access frequency & filtering & sorting
- Date time type in database
- Huawei cloud MVP email
- Bert base Chinese Download (SMART)
- Get the punch in record of nailing attendance machine
- STM32控制步进电机(ULN2003+28byj)
- Homomorphic encryption technology learning
- Introducing vant components on demand
- Web17——EL与JSTL的使用
猜你喜欢

Remote access to raspberry pie at home (Part 1)

V-model binding value in El select, data echo only displays value, not label

Plato farm - a game of farm metauniverse with Plato as the goal

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

Analysis of InnoDB execution process in MySQL

SSM框架系列——注解开发day2-2

mysql中 innoDB执行过程分析

梳理網絡IP代理的幾大用途

SSM framework series - JUnit unit test optimization day2-3

解决disagrees about version of symbol device_create
随机推荐
NBIOT的AT指令
Mysql8 installation
If you were a golang interviewer, what questions would you ask?
At instruction of nbiot
Wonderful review | the sixth issue of "source" - open source economy and industrial investment
98. Error s.e.errormvcautoconfiguration $staticview reported by freemaker framework: cannot render error page for request
The continuous construction of the Internet industry platform is not only able to collect traffic
Common problems of unity (1)
Labels and paths
【蓝桥杯】4月17日省赛刷题训练(前3道题)
【每日一题】棋盘问题
如何防止网站被黑客入侵篡改
SSM framework series - annotation development day2-2
有趣的IDEA插件推荐,给你的开发工作增添色彩
Introduction to servlet listener & filter
22. Bracket generation
Synchronously update the newly added and edited data to the list
Record a website for querying compatibility, string Replaceall() compatibility error
云原生KubeSphere部署Redis
There is no need to crack the markdown editing tool typora