tuple hashing functions across languages

This commit is contained in:
Michael Hohn
2025-05-14 10:38:17 -07:00
committed by =Michael Hohn
parent 5bdbd60cc5
commit 8590bd6de7
4 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
* tuple hashing functions across languages
There are three parallel implementations of a hash for every entry of a tuple
list. The functions produce identical results across 3 languages and can be
used across agent / server / client.
#+BEGIN_SRC sh
hohn@ghm3 ~/work-gh/mrva/mrvacommander/experimental/qldb-specification
0:$ node tuple-hash.js
[
'91b80a9933218ff5bc62df8ff71f1252',
'b0934b29293e91aefaac73c99fc75e94'
]
hohn@ghm3 ~/work-gh/mrva/mrvacommander/experimental/qldb-specification
0:$ python3 tuple-hash.py
['91b80a9933218ff5bc62df8ff71f1252', 'b0934b29293e91aefaac73c99fc75e94']
hohn@ghm3 ~/work-gh/mrva/mrvacommander/experimental/qldb-specification
0:$ go run tuple-hash.go
[91b80a9933218ff5bc62df8ff71f1252 b0934b29293e91aefaac73c99fc75e94]
#+END_SRC

View File

@@ -0,0 +1,28 @@
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
)
func main() {
atl_L := [][2]interface{}{
{1, "s1"},
{2, "str"},
}
var sl_hash []string
for _, item := range atl_L {
jsonBytes, err := json.Marshal(item)
if err != nil {
panic(err)
}
sum := md5.Sum(jsonBytes)
sl_hash = append(sl_hash, hex.EncodeToString(sum[:]))
}
fmt.Println(sl_hash)
}

View File

@@ -0,0 +1,9 @@
const crypto = require("crypto");
const atl_L = [[1, "s1"], [2, "str"]];
const sl_hash = atl_L.map(item => {
const json = JSON.stringify(item);
return crypto.createHash("md5").update(json).digest("hex");
});
console.log(sl_hash);

View File

@@ -0,0 +1,12 @@
import hashlib
import json
atl_L = [(1, "s1"), (2, "str")]
sl_hash = []
for item in atl_L:
encoded = json.dumps(item, separators=(',', ':')).encode("utf-8")
md5sum = hashlib.md5(encoded).hexdigest()
sl_hash.append(md5sum)
print(sl_hash)