I need to do following operations in Go:
I could compute MD5 hash of the string using "crypto/md5" package, but having trouble implementing step #3. Here is the code I came up with which is not correct I think and didn't see any links to get MSBs and LSBs from a string.
func GenerateHashKey(s1 string, s2 string) string {
if s1 == "" {
return ""
}
data := []byte(s1 + s2)
md5sum := md5.Sum(data)
// 0: uint32
lsb := bytes.NewBuffer(md5sum[:9]) // 0-8
msb := bytes.NewBuffer(md5sum[9:]) // 9-16
return msb ^ lsb; //This results in an error
}
Here is a corresponding working Java code that I need to translate to Go.
//input is a concatenated string
byte[] str = input.getBytes("UTF-8");
byte[] md5sum = MessageDigest.getInstance("MD5").digest(str);
long lsb =
ByteBuffer.wrap(md5sum).order(ByteOrder.LITTLE_ENDIAN).getLong(0);
long msb =
ByteBuffer.wrap(md5sum).order(ByteOrder.LITTLE_ENDIAN).getLong(8);
return msb ^ lsb;
You cannot use a bitwise operator on a bytes.Buffer
, it only applies to integer values. You can use the encoding/binary
package to convert the bytes into suitable 64 bit values to XOR, and use the little endian byte order here as shown in the java code provided.
Once you have the value, you can format the returned string as desired with fmt.Sprintf
or the strconv
package.
func GenerateHashKey(s1 string, s2 string) string {
data := []byte(s1 + s2)
md5sum := md5.Sum(data)
lsb := binary.LittleEndian.Uint64(md5sum[:8])
msb := binary.LittleEndian.Uint64(md5sum[8:])
return strconv.FormatUint(lsb^msb, 10)
}