我是一名 Go 初学者,遇到了一个问题。 我想用 UTF16 little endian 编码一个字符串,然后用 MD5(十六进制)对其进行哈希处理。我找到了一段 Python 代码,它完全符合我的要求。但我无法将其传输到 Google Go。
md5 = hashlib.md5()
md5.update(challenge.encode('utf-16le'))
response = md5.hexdigest()
挑战是一个包含字符串的变量。
最佳答案
通过使用 golang.org/x/text/encoding,您可以用更少的工作(或者至少更容易理解,IMO)来完成它和 golang.org/x/text/transform创建一个 Writer 链,无需太多手动字节 slice 处理即可进行编码和散列。等效函数:
func utf16leMd5(s string) []byte {
enc := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
hasher := md5.New()
t := transform.NewWriter(hasher, enc)
t.Write([]byte(s))
return hasher.Sum(nil)
}
关于hash - Golang 使用 MD5 编码字符串 UTF16 little endian 和哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33710672/