From 8590bd6de76c7bda0a8489f2ba64f0d25b6f6d4a Mon Sep 17 00:00:00 2001 From: Michael Hohn Date: Wed, 14 May 2025 10:38:17 -0700 Subject: [PATCH] tuple hashing functions across languages --- experimental/qldb-specification/readme.org | 22 +++++++++++++++ experimental/qldb-specification/tuple-hash.go | 28 +++++++++++++++++++ experimental/qldb-specification/tuple-hash.js | 9 ++++++ experimental/qldb-specification/tuple-hash.py | 12 ++++++++ 4 files changed, 71 insertions(+) create mode 100644 experimental/qldb-specification/readme.org create mode 100644 experimental/qldb-specification/tuple-hash.go create mode 100644 experimental/qldb-specification/tuple-hash.js create mode 100644 experimental/qldb-specification/tuple-hash.py diff --git a/experimental/qldb-specification/readme.org b/experimental/qldb-specification/readme.org new file mode 100644 index 0000000..5975400 --- /dev/null +++ b/experimental/qldb-specification/readme.org @@ -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 + diff --git a/experimental/qldb-specification/tuple-hash.go b/experimental/qldb-specification/tuple-hash.go new file mode 100644 index 0000000..1e0f745 --- /dev/null +++ b/experimental/qldb-specification/tuple-hash.go @@ -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) +} diff --git a/experimental/qldb-specification/tuple-hash.js b/experimental/qldb-specification/tuple-hash.js new file mode 100644 index 0000000..c2c2b91 --- /dev/null +++ b/experimental/qldb-specification/tuple-hash.js @@ -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); diff --git a/experimental/qldb-specification/tuple-hash.py b/experimental/qldb-specification/tuple-hash.py new file mode 100644 index 0000000..c8a5782 --- /dev/null +++ b/experimental/qldb-specification/tuple-hash.py @@ -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)