Hi,
We are using different client libraries for libsodium, specifically, we use libsodium-go for Go projects and sodium-native for JavaScript projects. The operation of generic hashing produces different value in libsodium-go when compared to the same operation in sodium-native and pynacl. Initially, I thought it was due to some bug in sodium-native but pynacl also produce the same value as sodium-native, so the conclusion was it was specific to libsodium-go. Below is a very simple code for testing, I am not sure if I am missing some encoding or setting some parameters. Looking at the libsodium-go code for generichash, it is just making the C call so I am totally puzzled at why the values are different.
Also, I am ensuring that the libsodium linking is the same between all the tests, in this case, I am using 1.0.16
Go Code
package main
import (
b64 "encoding/base64"
"fmt"
"github.com/GoKillers/libsodium-go/cryptogenerichash"
"github.com/GoKillers/libsodium-go/sodium"
)
//Hash a Message and return Hash String as b64, Hash bytes
func HashSimple(message string) (hashString string, hashBytes []byte) {
m := []byte(message)
k := make([]byte, generichash.CryptoGenericHashKeyBytes())
sodium.MemZero(k)
hashBytes, _ = generichash.CryptoGenericHash(generichash.CryptoGenericHashBytes(), m, k)
hashString = b64.StdEncoding.EncodeToString(hashBytes)
return hashString, hashBytes
}
func main() {
message := "T"
hashString, hashBytes := HashSimple(message)
fmt.Println(HashSimple(hashString))
fmt.Printf("%x\n", hashBytes)
}
Equivalent Python code using pynacl
from nacl.hash import blake2b
import base64
from binascii import unhexlify
def hextobase64(txt):
raw = unhexlify(txt)
return base64.b64encode(raw)
encodedStr = str.encode("T")
hash = blake2b(encodedStr)
hash64 = hextobase64(hash)
print(hash64)
print(hash)
Result from libsodium-go
eqgBf1Il5zRJaYnGDIkPr6fgkuacNfxJN3S4Aikb7Ck=
7aa8017f5225e734496989c60c890fafa7e092e69c35fc493774b802291bec29
Results from pynacl and sodium-native
b'iWEBxL5gXFXlQLCWfEBZeRt+TgpfKxGkYHTonIon62Y='
b'896101c4be605c55e540b0967c4059791b7e4e0a5f2b11a46074e89c8a27eb66'
Code from sodium-native for reference that produces the same output as pynacl
const sodium = require('/Users/jaye/projects/att3/sodium-native');
const hashMessage = (input) => {
const output = sodium.sodium_malloc(sodium.crypto_generichash_BYTES)
const data = Buffer.from("T",sodium.crypto_generichash_BYTES)
const key = sodium.sodium_malloc(sodium.crypto_generichash_KEYBYTES)
sodium.crypto_generichash(output, data)
return output
}
message = "T"
const hashOut = hashMessage(message)
console.log(hashOut.toString('base64'))
console.log(hashOut.toString('hex'))
Note that both b64 and bytes are different so it also not an issue with b64 encoding. Any suggestions to get the same behavior as other libsodium would be great because we really like the library and would like to use in Go program.