You can use the unicode/utf16
package for UTF-16 encoding. utf16.Encode()
returns the UTF-16 encoding of the Unicode code point sequence (slice of runes: []rune
). You can simply convert a string
to a slice of runes, e.g. []rune("some string")
, and you can easily produce the byte sequence of the little-endian encoding by ranging over the uint16
codes and sending/appending first the low byte then the high byte to the output (this is what Little Endian means).
For Little Endian encoding, alternatively you can use the encoding/binary
package: it has an exported LittleEndian
variable and it has a PutUint16()
method.
As for the MD5 checksum, the crypto/md5
package has what you want, md5.Sum()
simply returns the MD5 checksum of the byte slice passed to it.
Here's a little function that captures what you want to do:
func utf16leMd5(s string) [16]byte {
codes := utf16.Encode([]rune(s))
b := make([]byte, len(codes)*2)
for i, r := range codes {
b[i*2] = byte(r)
b[i*2+1] = byte(r >> 8)
}
return md5.Sum(b)
}
Using it:
s := "Hello, playground"
fmt.Printf("%x\n", utf16leMd5(s))
s = "エヌガミ"
fmt.Printf("%x\n", utf16leMd5(s))
Output:
8f4a54c6ac7b88936e990256cc9d335b
5f0db9e9859fd27f750eb1a212ad6212
Try it on the Go Playground.
The variant that uses encoding/binary
would look like this:
for i, r := range codes {
binary.LittleEndian.PutUint16(b[i*2:], r)
}
(Although this is slower as it creates lots of new slice headers.)