tuple hashing functions across languages
This commit is contained in:
committed by
=Michael Hohn
parent
5bdbd60cc5
commit
8590bd6de7
22
experimental/qldb-specification/readme.org
Normal file
22
experimental/qldb-specification/readme.org
Normal 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
|
||||||
|
|
||||||
28
experimental/qldb-specification/tuple-hash.go
Normal file
28
experimental/qldb-specification/tuple-hash.go
Normal 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)
|
||||||
|
}
|
||||||
9
experimental/qldb-specification/tuple-hash.js
Normal file
9
experimental/qldb-specification/tuple-hash.js
Normal 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);
|
||||||
12
experimental/qldb-specification/tuple-hash.py
Normal file
12
experimental/qldb-specification/tuple-hash.py
Normal 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)
|
||||||
Reference in New Issue
Block a user