发布时间:2024-12-23 02:14:36
import (
"crypto/md5"
"encoding/hex"
"fmt"
)
type User struct {
Username string
Password string
}
func generateHash(user User) string {
// 将结构体转换为字符串
userStr := fmt.Sprintf("%v", user)
// 计算MD5哈希值
hasher := md5.New()
hasher.Write([]byte(userStr))
hash := hex.EncodeToString(hasher.Sum(nil))
return hash
}
func main() {
// 创建一个用户实例
user := User{
Username: "admin",
Password: "password123",
}
// 生成MD5哈希值
hash := generateHash(user)
// 打印哈希值
fmt.Println("MD5 Hash:", hash)
}
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
)
type User struct {
Username string
Password string
}
func generateHash(user User) string {
userStr := fmt.Sprintf("%v", user)
hasher := md5.New()
hasher.Write([]byte(userStr))
hash := hex.EncodeToString(hasher.Sum(nil))
return hash
}
func main() {
user := User{
Username: "admin",
Password: "password123",
}
hash := generateHash(user)
fmt.Println("MD5 Hash:", hash)
}
MD5 Hash: e6fb06210fafc02fd7479ddbed2d042cc3a5155e