How to implement encryption and decryption with md5 in golang
In Go language, you can utilize the crypto/md5 package for MD5 encryption and decryption operations. Below is a simple example code:
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
)
func encryptMD5(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}
func main() {
// 加密
encrypted := encryptMD5("Hello World")
fmt.Println("MD5加密结果:", encrypted)
// 解密(MD5是单向加密算法,无法进行解密)
}
In the code above, the encryptMD5 function takes a string as a parameter, converts it to a byte array, uses the md5.Sum function to perform MD5 encryption, then converts the encrypted result to a string using the hex.EncodeToString function and returns it. In the main function, we call the encryptMD5 function to perform MD5 encryption on the string “Hello World” and print out the encrypted result.
It should be noted that MD5 is a one-way encryption algorithm that cannot be decrypted. This means that it is not possible to recover the original text from the encrypted result of MD5. MD5 is typically used for encrypting and storing sensitive information such as passwords, but it is not suitable for scenarios where decryption is needed.