/
fnv64.ts
35 lines (27 loc) · 1.02 KB
/
fnv64.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Ported from Go:
// https://github.com/golang/go/tree/go1.13.10/src/hash/fnv/fnv.go
// Copyright 2011 The Go Authors. All rights reserved. BSD license.
// https://github.com/golang/go/blob/master/LICENSE
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { mul64, swap32 } from "./util.ts";
const prime64Lo = 435;
const prime64Hi = 256;
export const fnv64 = (data: Uint8Array): ArrayBuffer => {
let hashLo = 2216829733;
let hashHi = 3421674724;
data.forEach((c) => {
[hashHi, hashLo] = mul64([hashHi, hashLo], [prime64Hi, prime64Lo]);
hashLo ^= c;
});
return new Uint32Array([swap32(hashHi >>> 0), swap32(hashLo >>> 0)]).buffer;
};
export const fnv64a = (data: Uint8Array): ArrayBuffer => {
let hashLo = 2216829733;
let hashHi = 3421674724;
data.forEach((c) => {
hashLo ^= c;
[hashHi, hashLo] = mul64([hashHi, hashLo], [prime64Hi, prime64Lo]);
});
return new Uint32Array([swap32(hashHi >>> 0), swap32(hashLo >>> 0)]).buffer;
};