2022-03-13 08:28:37 +03:00
|
|
|
import { Element } from './index'
|
|
|
|
|
2022-02-24 06:21:14 +03:00
|
|
|
/***
|
|
|
|
* This is insecure hash function, just for example only
|
|
|
|
* @param data
|
|
|
|
* @param seed
|
|
|
|
* @param hashLength
|
|
|
|
*/
|
|
|
|
|
2022-02-28 10:00:28 +03:00
|
|
|
export function simpleHash<T>(data: T[], seed?: number, hashLength = 40): string {
|
2022-02-24 06:21:14 +03:00
|
|
|
const str = data.join('')
|
|
|
|
let i, l,
|
|
|
|
hval = seed ?? 0x811c9dcc5
|
|
|
|
for (i = 0, l = str.length; i < l; i++) {
|
|
|
|
hval ^= str.charCodeAt(i)
|
|
|
|
hval += (hval << 1) + (hval << 4) + (hval << 6) + (hval << 8) + (hval << 24)
|
|
|
|
}
|
|
|
|
const hash = (hval >>> 0).toString(16)
|
|
|
|
return BigInt('0x' + hash.padEnd(hashLength - (hash.length - 1), '0')).toString(10)
|
|
|
|
}
|
|
|
|
|
2022-03-13 08:28:37 +03:00
|
|
|
export default (left: Element, right: Element): string => simpleHash([left, right])
|