javascriptgocryptojssha-3

cryptojs and golang give different sha3 hash values


I'm trying to generate a sha3-512 hash in JS and check it in a golang server. However, cryptoJS is producing different hashes than golang.

CryptoJS:

CryptoJS.algo.SHA3.create().update("foo").finalize().toString(CryptoJS.enc.Hex)

Output:

1597842aac52bc9d13fe249d808afbf44da13524759477404c3592ee331173e89fe1cbf21a7e4360990d565fad4643cdb209d80fa41a91dea97e665022c92135


Golang:

hex.EncodeToString(crypto.SHA3_512.New().Sum([]byte("foo")))

Output:

666f6fa69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26

I expect these hashes to be equal, but they aren't


Solution

  • Obviously, your output is 134-width which should be 128-width.

    Let us decode your output:

    bytes, _ := hex.DecodeString("666f6fa69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26")
    fmt.Printf("%s\n", bytes)
    

    We found the output is foo��s̢:��ȵg�Zun�ɂO�XY����G\���:���L��@,:�X������u��(�&.

    That means what you did actually is to output:

    "foo" + sha3("")

    where sha3_512("") is "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26" from Examples of SHA-3 variants.