-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.tpl
143 lines (115 loc) · 3.94 KB
/
template.tpl
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
___TERMS_OF_SERVICE___
By creating or modifying this file you agree to Google Tag Manager's Community
Template Gallery Developer Terms of Service available at
https://developers.google.com/tag-manager/gallery-tos (or such other URL as
Google may provide), as modified from time to time.
___INFO___
{
"type": "MACRO",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "cyrb53 Hasher",
"description": "hash strings using a cyrb53 implementation (standard ascii characters only)",
"categories": ["UTILITY"],
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "TEXT",
"name": "dataStr",
"displayName": "Data String",
"simpleValueType": true
},
{
"type": "TEXT",
"name": "seed",
"displayName": "Seed (optional)",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
},
{
"type": "NON_NEGATIVE_NUMBER"
}
],
"defaultValue": 0
},
{
"type": "CHECKBOX",
"name": "output64",
"checkboxText": "64-bit Output",
"simpleValueType": true,
"defaultValue": true
}
]
___SANDBOXED_JS_FOR_SERVER___
//no String.prototype.charCodeAt in Sandbox :(
function charCodeAt(str) {
const asciiString = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
const asciiOffset = 32;
return asciiString.indexOf(str) + asciiOffset;
}
//polyfills for other missing String / Math standard methods...
function imul(a, b) {
var aHi = (a >>> 16) & 65535;
var aLo = a & 65535;
var bHi = (b >>> 16) & 65535;
var bLo = b & 65535;
return ((aLo * bLo) + (((aHi * bLo + aLo * bHi) << 16) >>> 0) | 0);
}
function padStart(str,targetLength,padString) {
targetLength = targetLength>>0;
padString = padString || ' ';
if (str.length > targetLength) {
return str;
} else {
targetLength = targetLength-str.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength/padString.length);
}
return padString.slice(0,targetLength) + str;
}
}
//cyrb53, adjusted for the limited JavaScript Sandbox capabilites
//find original function at https://stackoverflow.com/a/52171480
function cyrb53(str, seed, fullOutput) {
seed = seed || 0;
let h1 = 3735928559 ^ seed, h2 = 1103547991 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = charCodeAt(str[i]);
h1 = imul(h1 ^ ch, 2654435761);
h2 = imul(h2 ^ ch, 1597334677);
}
h1 = imul(h1 ^ (h1>>>16), 2246822507) ^ imul(h2 ^ (h2>>>13), 3266489909);
h2 = imul(h2 ^ (h2>>>16), 2246822507) ^ imul(h1 ^ (h1>>>13), 3266489909);
if (fullOutput == true)
return padStart((h2>>>0).toString(16), 8,'0') + padStart((h1>>>0).toString(16), 8,'0');
else
return 4294967296 * (2097151 & h2) + (h1>>>0);
}
return cyrb53(data.dataStr, data.seed, data.output64);
___TESTS___
scenarios:
- name: check example values from Stack Overflow answer
code: |
let variableResult = runCode({dataStr: 'a', seed: 0, output64: true});
assertThat(variableResult).isEqualTo('501c2ba782c97901');
variableResult = runCode({dataStr: 'b', seed: 0, output64: true});
assertThat(variableResult).isEqualTo('459eda5bc254d2bf');
variableResult = runCode({dataStr: 'revenge', seed: 0, output64: true});
assertThat(variableResult).isEqualTo('fbce64cc3b748385');
variableResult = runCode({dataStr: 'revenue', seed: 0, output64: true});
assertThat(variableResult).isEqualTo('fb1d85148d13f93a');
variableResult = runCode({dataStr: 'revenue', seed: 1, output64: true});
assertThat(variableResult).isEqualTo('76fee5e6598ccd5c');
variableResult = runCode({dataStr: 'revenue', seed: 2, output64: true});
assertThat(variableResult).isEqualTo('1f672e2831253862');
variableResult = runCode({dataStr: 'revenue', seed: 3, output64: true});
assertThat(variableResult).isEqualTo('2b10de31708e6ab7');
___NOTES___
Created on 30.12.2021, 12:43:38