-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.js
64 lines (58 loc) · 1.65 KB
/
hash.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// FNV-1 hash paramerters - http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param
// ------------ FNV Primes ------------------ //
// By Bit size (32, 64, 128, 256)
var FNV_PRIME_32 = 0x1000193
var FNV_PRIME_64 = 0x100000001B3
var FNV_PRIME_128 = 0x1000000000000000000013B
var FNV_PRIME_256 = 0x1000000000000000000000000000000000000000163
// ------------ FNV Offset Basis -------------- //
var FNV_OFFSET_32 = 0x811C9DC5
var FNV_OFFSET_64 = 0xCBF29CE484222325
var FNV_OFFSET_128 = 0x6C62272E07BB014262B821756295C58D
var FNV_OFFSET_256 = 0xDD268DBCAAC550362D98C384C4E576CCC8B1536847B6BBB31023B4C8CAEE0535
// ------------ FNV Hash Functions ------------- //
var HashAlgorithms = {
/**
* Creates a 32 bit FNV-1 hash from a json string
*
* @param {String} data
* @return {Number} (hash of data)
*/
fnv32: function(data) {
var hash = FNV_OFFSET_32;
for (var i = 0; i < data.length; i++) {
hash ^= data.charCodeAt(i);
hash *= FNV_PRIME_32;
}
return hash;
},
/**
* Creates a 64 bit FNV-1 hash from a json string
*
* @param {String} data
* @return {Number} (hash of data)
*/
fnv64: function(data) {
var hash = FNV_OFFSET_64;
for (var i = 0; i < data.length; i++) {
hash ^= data.charCodeAt(i);
hash *= FNV_PRIME_64;
}
return hash;
},
/**
* Creates a 128 bit FNV-1 hash from a json string
*
* @param {String} data
* @return {Number} (hash of data)
*/
fnv128: function(data) {
var hash = FNV_OFFSET_128;
for (var i = 0; i < data.length; i++) {
hash ^= data.charCodeAt(i);
hash *= FNV_PRIME_128;
}
return hash;
}
};
module.exports = HashAlgorithms