@mark1russell7/rigor-generators 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -0
- package/build/hash.d.ts +47 -0
- package/build/hash.d.ts.map +1 -0
- package/build/hash.js +240 -0
- package/build/hash.js.map +1 -0
- package/build/index.d.ts +2 -1
- package/build/index.js +2 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -104,6 +104,27 @@ The option `input` of `fnv1a32` selects the units that the hash reads. Use it to
|
|
|
104
104
|
|
|
105
105
|
For a string without a lone surrogate, `"utf8"` and `"wtf8"` give the same hash.
|
|
106
106
|
|
|
107
|
+
## A content hash
|
|
108
|
+
|
|
109
|
+
`canonicalHash(value)` gives the SHA-256 digest of the canonical form of a value. Equal values give equal hashes, also when the keys of their objects have another order.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { canonicalHash, canonicalJson, sha256Hex } from "@mark1russell7/rigor-generators";
|
|
113
|
+
|
|
114
|
+
canonicalJson({ b: [1, undefined], a: "x" }); // '{"a":"x","b":[1,null]}'
|
|
115
|
+
canonicalHash({ b: 1, a: 2 }) === canonicalHash({ a: 2, b: 1 }); // true
|
|
116
|
+
sha256Hex("abc"); // "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The default form of `canonicalJson` is RFC 8785 (JCS). Two options change it:
|
|
120
|
+
|
|
121
|
+
| Option | The default | The other value |
|
|
122
|
+
| --- | --- | --- |
|
|
123
|
+
| `form` | `"jcs"`: `NaN`, an infinite number and a lone surrogate cause a `RangeError` | `"json"`: the form of `JSON.stringify`, so these values become `null` or an escape |
|
|
124
|
+
| `bigint` | `"error"`: a `bigint` causes a `TypeError` | `"suffix"`: the digits and `n`, for example `12n`. This text is not JSON, so use it only for a key |
|
|
125
|
+
|
|
126
|
+
The option `input` of `sha256Hex` has the values `"utf8"` and `"wtf8"` of `fnv1a32`. The two functions use no API of Node or of the browser. `sha256Hex` is not for passwords.
|
|
127
|
+
|
|
107
128
|
## Move from a copy of mulberry32
|
|
108
129
|
|
|
109
130
|
Each common form of mulberry32 gives the same numbers as `random(seed).next`: the first published form (`| 0` on the state) and the form with `>>> 0`. These also give the same values:
|
package/build/hash.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** The options of `sha256Hex`. */
|
|
2
|
+
export interface Sha256Options {
|
|
3
|
+
/**
|
|
4
|
+
* The bytes that the hash reads, as in `fnv1a32`:
|
|
5
|
+
*
|
|
6
|
+
* - `"utf8"` (the default): the UTF-8 bytes. A lone surrogate becomes the bytes of U+FFFD, as `TextEncoder` writes them.
|
|
7
|
+
* - `"wtf8"`: the UTF-8 bytes of each code point. A lone surrogate becomes its own 3 bytes (WTF-8).
|
|
8
|
+
*
|
|
9
|
+
* For a string without a lone surrogate, the two inputs give the same hash.
|
|
10
|
+
*/
|
|
11
|
+
readonly input?: "utf8" | "wtf8";
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* This function gives the SHA-256 digest of the bytes of a string, as 64 lowercase hex characters.
|
|
15
|
+
* The option `input` selects the bytes of a lone surrogate. It is not for passwords: it has no salt and no cost.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sha256Hex(text: string, options?: Sha256Options): string;
|
|
18
|
+
/** The options of `canonicalJson` and `canonicalHash`. */
|
|
19
|
+
export interface CanonicalOptions {
|
|
20
|
+
/**
|
|
21
|
+
* The form of the values that JSON does not have:
|
|
22
|
+
*
|
|
23
|
+
* - `"jcs"` (the default): RFC 8785. `NaN`, an infinite number and a string with a lone surrogate cause a `RangeError`.
|
|
24
|
+
* - `"json"`: the form of `JSON.stringify`. `NaN` and an infinite number become `null`, and a lone surrogate becomes an escape.
|
|
25
|
+
*/
|
|
26
|
+
readonly form?: "jcs" | "json";
|
|
27
|
+
/**
|
|
28
|
+
* The form of a `bigint`. `"error"` (the default) causes a `TypeError`, as `JSON.stringify` does.
|
|
29
|
+
* `"suffix"` writes the digits and the letter `n`, for example `12n`. This text is not JSON, so use it only for a key.
|
|
30
|
+
*/
|
|
31
|
+
readonly bigint?: "error" | "suffix";
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* This function gives the canonical form of a value. The default form is RFC 8785 (JCS):
|
|
35
|
+
*
|
|
36
|
+
* - The keys of each object are in the order of their UTF-16 code units, with no whitespace.
|
|
37
|
+
* - The numbers and the strings have the form of `JSON.stringify`, which is the form of RFC 8785.
|
|
38
|
+
* - A key with the value `undefined` is not in the form. An `undefined` in an array, or as the value, becomes `null`.
|
|
39
|
+
* - An object with a method `toJSON`, for example a `Date`, gives the form of the result of that method.
|
|
40
|
+
* - A function, a symbol and a cycle cause a `TypeError`.
|
|
41
|
+
*
|
|
42
|
+
* `canonicalJson(JSON.parse(canonicalJson(v)))` is equal to `canonicalJson(v)`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function canonicalJson(value: unknown, options?: CanonicalOptions): string;
|
|
45
|
+
/** This function gives the content hash of a value: `sha256Hex(canonicalJson(value, options))`. */
|
|
46
|
+
export declare function canonicalHash(value: unknown, options?: CanonicalOptions): string;
|
|
47
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","names":[],"sources":["../src/hash.ts"],"mappings":";iBAiBiB;;;;;;;;;WASN;;;;;;wBAmDK,UAAU,cAAc,UAAU;;iBAwDjC;;;;;;;WAON;;;;;WAKA;;;;;;;;;;;;;wBAcK,cAAc,gBAAgB,UAAU;;wBAgDxC,cAAc,gBAAgB,UAAU"}
|
package/build/hash.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// src/hash.ts
|
|
2
|
+
/**
|
|
3
|
+
* The content hash of a document: SHA-256 of a string, and a canonical JSON form of a value.
|
|
4
|
+
*
|
|
5
|
+
* The two functions use no API of Node or of the browser, so a hash is the same on each platform.
|
|
6
|
+
* `canonicalHash(value)` is `sha256Hex(canonicalJson(value))`: equal values give equal hashes, also when their keys have another order.
|
|
7
|
+
*/
|
|
8
|
+
/** The round constants of SHA-256. */
|
|
9
|
+
const K = [
|
|
10
|
+
1116352408,
|
|
11
|
+
1899447441,
|
|
12
|
+
3049323471,
|
|
13
|
+
3921009573,
|
|
14
|
+
961987163,
|
|
15
|
+
1508970993,
|
|
16
|
+
2453635748,
|
|
17
|
+
2870763221,
|
|
18
|
+
3624381080,
|
|
19
|
+
310598401,
|
|
20
|
+
607225278,
|
|
21
|
+
1426881987,
|
|
22
|
+
1925078388,
|
|
23
|
+
2162078206,
|
|
24
|
+
2614888103,
|
|
25
|
+
3248222580,
|
|
26
|
+
3835390401,
|
|
27
|
+
4022224774,
|
|
28
|
+
264347078,
|
|
29
|
+
604807628,
|
|
30
|
+
770255983,
|
|
31
|
+
1249150122,
|
|
32
|
+
1555081692,
|
|
33
|
+
1996064986,
|
|
34
|
+
2554220882,
|
|
35
|
+
2821834349,
|
|
36
|
+
2952996808,
|
|
37
|
+
3210313671,
|
|
38
|
+
3336571891,
|
|
39
|
+
3584528711,
|
|
40
|
+
113926993,
|
|
41
|
+
338241895,
|
|
42
|
+
666307205,
|
|
43
|
+
773529912,
|
|
44
|
+
1294757372,
|
|
45
|
+
1396182291,
|
|
46
|
+
1695183700,
|
|
47
|
+
1986661051,
|
|
48
|
+
2177026350,
|
|
49
|
+
2456956037,
|
|
50
|
+
2730485921,
|
|
51
|
+
2820302411,
|
|
52
|
+
3259730800,
|
|
53
|
+
3345764771,
|
|
54
|
+
3516065817,
|
|
55
|
+
3600352804,
|
|
56
|
+
4094571909,
|
|
57
|
+
275423344,
|
|
58
|
+
430227734,
|
|
59
|
+
506948616,
|
|
60
|
+
659060556,
|
|
61
|
+
883997877,
|
|
62
|
+
958139571,
|
|
63
|
+
1322822218,
|
|
64
|
+
1537002063,
|
|
65
|
+
1747873779,
|
|
66
|
+
1955562222,
|
|
67
|
+
2024104815,
|
|
68
|
+
2227730452,
|
|
69
|
+
2361852424,
|
|
70
|
+
2428436474,
|
|
71
|
+
2756734187,
|
|
72
|
+
3204031479,
|
|
73
|
+
3329325298
|
|
74
|
+
];
|
|
75
|
+
/**
|
|
76
|
+
* This function writes the bytes of a string into `out` and gives the count of the bytes. With no `out`, it only counts.
|
|
77
|
+
* It reads the UTF-16 code units, so it makes no string for each character.
|
|
78
|
+
*/
|
|
79
|
+
function writeBytes(text, wtf8, out) {
|
|
80
|
+
let n = 0;
|
|
81
|
+
for (let i = 0; i < text.length; i++) {
|
|
82
|
+
let c = text.charCodeAt(i);
|
|
83
|
+
if (c >= 55296 && c < 57344) {
|
|
84
|
+
const low = i + 1 < text.length ? text.charCodeAt(i + 1) : 0;
|
|
85
|
+
if (c < 56320 && low >= 56320 && low < 57344) {
|
|
86
|
+
c = 65536 + (c - 55296 << 10) + (low - 56320);
|
|
87
|
+
i++;
|
|
88
|
+
} else if (!wtf8) c = 65533;
|
|
89
|
+
}
|
|
90
|
+
if (c < 128) {
|
|
91
|
+
if (out !== void 0) out[n] = c;
|
|
92
|
+
n += 1;
|
|
93
|
+
} else if (c < 2048) {
|
|
94
|
+
if (out !== void 0) {
|
|
95
|
+
out[n] = 192 | c >> 6;
|
|
96
|
+
out[n + 1] = 128 | c & 63;
|
|
97
|
+
}
|
|
98
|
+
n += 2;
|
|
99
|
+
} else if (c < 65536) {
|
|
100
|
+
if (out !== void 0) {
|
|
101
|
+
out[n] = 224 | c >> 12;
|
|
102
|
+
out[n + 1] = 128 | c >> 6 & 63;
|
|
103
|
+
out[n + 2] = 128 | c & 63;
|
|
104
|
+
}
|
|
105
|
+
n += 3;
|
|
106
|
+
} else {
|
|
107
|
+
if (out !== void 0) {
|
|
108
|
+
out[n] = 240 | c >> 18;
|
|
109
|
+
out[n + 1] = 128 | c >> 12 & 63;
|
|
110
|
+
out[n + 2] = 128 | c >> 6 & 63;
|
|
111
|
+
out[n + 3] = 128 | c & 63;
|
|
112
|
+
}
|
|
113
|
+
n += 4;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return n;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* This function gives the SHA-256 digest of the bytes of a string, as 64 lowercase hex characters.
|
|
120
|
+
* The option `input` selects the bytes of a lone surrogate. It is not for passwords: it has no salt and no cost.
|
|
121
|
+
*/
|
|
122
|
+
function sha256Hex(text, options) {
|
|
123
|
+
const input = options?.input ?? "utf8";
|
|
124
|
+
if (input !== "utf8" && input !== "wtf8") throw new RangeError(`sha256Hex: input is "utf8" or "wtf8", not ${String(input)}.`);
|
|
125
|
+
const wtf8 = input === "wtf8";
|
|
126
|
+
const length = writeBytes(text, wtf8);
|
|
127
|
+
const bits = length * 8;
|
|
128
|
+
const total = Math.ceil((length + 9) / 64) * 64;
|
|
129
|
+
const data = new Uint8Array(total);
|
|
130
|
+
writeBytes(text, wtf8, data);
|
|
131
|
+
data[length] = 128;
|
|
132
|
+
const view = new DataView(data.buffer);
|
|
133
|
+
view.setUint32(total - 8, Math.floor(bits / 4294967296));
|
|
134
|
+
view.setUint32(total - 4, bits >>> 0);
|
|
135
|
+
const h = [
|
|
136
|
+
1779033703,
|
|
137
|
+
3144134277,
|
|
138
|
+
1013904242,
|
|
139
|
+
2773480762,
|
|
140
|
+
1359893119,
|
|
141
|
+
2600822924,
|
|
142
|
+
528734635,
|
|
143
|
+
1541459225
|
|
144
|
+
];
|
|
145
|
+
const w = /* @__PURE__ */ new Uint32Array(64);
|
|
146
|
+
const rotr = (x, n) => x >>> n | x << 32 - n;
|
|
147
|
+
for (let off = 0; off < total; off += 64) {
|
|
148
|
+
for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + 4 * i);
|
|
149
|
+
for (let i = 16; i < 64; i++) {
|
|
150
|
+
const a = w[i - 15];
|
|
151
|
+
const b = w[i - 2];
|
|
152
|
+
const s0 = rotr(a, 7) ^ rotr(a, 18) ^ a >>> 3;
|
|
153
|
+
const s1 = rotr(b, 17) ^ rotr(b, 19) ^ b >>> 10;
|
|
154
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
|
|
155
|
+
}
|
|
156
|
+
let [a, b, c, d, e, f, g, k] = h;
|
|
157
|
+
for (let i = 0; i < 64; i++) {
|
|
158
|
+
const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
159
|
+
const ch = e & f ^ ~e & g;
|
|
160
|
+
const t1 = k + s1 + ch + K[i] + w[i] >>> 0;
|
|
161
|
+
const t2 = (rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + (a & b ^ a & c ^ b & c) >>> 0;
|
|
162
|
+
k = g;
|
|
163
|
+
g = f;
|
|
164
|
+
f = e;
|
|
165
|
+
e = d + t1 >>> 0;
|
|
166
|
+
d = c;
|
|
167
|
+
c = b;
|
|
168
|
+
b = a;
|
|
169
|
+
a = t1 + t2 >>> 0;
|
|
170
|
+
}
|
|
171
|
+
h[0] = h[0] + a >>> 0;
|
|
172
|
+
h[1] = h[1] + b >>> 0;
|
|
173
|
+
h[2] = h[2] + c >>> 0;
|
|
174
|
+
h[3] = h[3] + d >>> 0;
|
|
175
|
+
h[4] = h[4] + e >>> 0;
|
|
176
|
+
h[5] = h[5] + f >>> 0;
|
|
177
|
+
h[6] = h[6] + g >>> 0;
|
|
178
|
+
h[7] = h[7] + k >>> 0;
|
|
179
|
+
}
|
|
180
|
+
return h.map((x) => x.toString(16).padStart(8, "0")).join("");
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* This function gives the canonical form of a value. The default form is RFC 8785 (JCS):
|
|
184
|
+
*
|
|
185
|
+
* - The keys of each object are in the order of their UTF-16 code units, with no whitespace.
|
|
186
|
+
* - The numbers and the strings have the form of `JSON.stringify`, which is the form of RFC 8785.
|
|
187
|
+
* - A key with the value `undefined` is not in the form. An `undefined` in an array, or as the value, becomes `null`.
|
|
188
|
+
* - An object with a method `toJSON`, for example a `Date`, gives the form of the result of that method.
|
|
189
|
+
* - A function, a symbol and a cycle cause a `TypeError`.
|
|
190
|
+
*
|
|
191
|
+
* `canonicalJson(JSON.parse(canonicalJson(v)))` is equal to `canonicalJson(v)`.
|
|
192
|
+
*/
|
|
193
|
+
function canonicalJson(value, options) {
|
|
194
|
+
const form = options?.form ?? "jcs";
|
|
195
|
+
const bigint = options?.bigint ?? "error";
|
|
196
|
+
if (form !== "jcs" && form !== "json") throw new RangeError(`canonicalJson: form is "jcs" or "json", not ${String(form)}.`);
|
|
197
|
+
if (bigint !== "error" && bigint !== "suffix") throw new RangeError(`canonicalJson: bigint is "error" or "suffix", not ${String(bigint)}.`);
|
|
198
|
+
const jcs = form === "jcs";
|
|
199
|
+
const open = /* @__PURE__ */ new Set();
|
|
200
|
+
const text = (s) => {
|
|
201
|
+
if (jcs && !s.isWellFormed()) throw new RangeError(`canonicalJson: the string ${JSON.stringify(s)} has a lone surrogate, which RFC 8785 does not allow.`);
|
|
202
|
+
return JSON.stringify(s);
|
|
203
|
+
};
|
|
204
|
+
const write = (v, key) => {
|
|
205
|
+
if (v !== null && typeof v === "object" && typeof v.toJSON === "function") v = v.toJSON(key);
|
|
206
|
+
if (v === void 0 || v === null) return "null";
|
|
207
|
+
switch (typeof v) {
|
|
208
|
+
case "boolean": return v ? "true" : "false";
|
|
209
|
+
case "number":
|
|
210
|
+
if (Number.isFinite(v)) return JSON.stringify(v);
|
|
211
|
+
if (jcs) throw new RangeError(`canonicalJson: ${String(v)} is not a JSON number.`);
|
|
212
|
+
return "null";
|
|
213
|
+
case "string": return text(v);
|
|
214
|
+
case "bigint":
|
|
215
|
+
if (bigint === "suffix") return `${v}n`;
|
|
216
|
+
throw new TypeError(`canonicalJson: the bigint ${v}n has no JSON form. Use the option bigint: "suffix" for a key.`);
|
|
217
|
+
case "object": break;
|
|
218
|
+
default: throw new TypeError(`canonicalJson: a ${typeof v} has no JSON form.`);
|
|
219
|
+
}
|
|
220
|
+
const o = v;
|
|
221
|
+
if (open.has(o)) throw new TypeError("canonicalJson: the value has a cycle.");
|
|
222
|
+
open.add(o);
|
|
223
|
+
let out;
|
|
224
|
+
if (Array.isArray(o)) out = `[${o.map((item, i) => write(item, String(i))).join(",")}]`;
|
|
225
|
+
else {
|
|
226
|
+
const r = o;
|
|
227
|
+
out = `{${Object.keys(r).filter((k) => r[k] !== void 0).sort().map((k) => `${text(k)}:${write(r[k], k)}`).join(",")}}`;
|
|
228
|
+
}
|
|
229
|
+
open.delete(o);
|
|
230
|
+
return out;
|
|
231
|
+
};
|
|
232
|
+
return write(value, "");
|
|
233
|
+
}
|
|
234
|
+
/** This function gives the content hash of a value: `sha256Hex(canonicalJson(value, options))`. */
|
|
235
|
+
function canonicalHash(value, options) {
|
|
236
|
+
return sha256Hex(canonicalJson(value, options));
|
|
237
|
+
}
|
|
238
|
+
export { canonicalHash, canonicalJson, sha256Hex };
|
|
239
|
+
|
|
240
|
+
//# sourceMappingURL=hash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","names":[],"sources":["../src/hash.ts"],"sourcesContent":["/**\n * The content hash of a document: SHA-256 of a string, and a canonical JSON form of a value.\n *\n * The two functions use no API of Node or of the browser, so a hash is the same on each platform.\n * `canonicalHash(value)` is `sha256Hex(canonicalJson(value))`: equal values give equal hashes, also when their keys have another order.\n */\n\n/** The round constants of SHA-256. */\nconst K: readonly number[] = [\n 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,\n 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\n 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,\n 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\n 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,\n];\n\n/** The options of `sha256Hex`. */\nexport interface Sha256Options {\n /**\n * The bytes that the hash reads, as in `fnv1a32`:\n *\n * - `\"utf8\"` (the default): the UTF-8 bytes. A lone surrogate becomes the bytes of U+FFFD, as `TextEncoder` writes them.\n * - `\"wtf8\"`: the UTF-8 bytes of each code point. A lone surrogate becomes its own 3 bytes (WTF-8).\n *\n * For a string without a lone surrogate, the two inputs give the same hash.\n */\n readonly input?: \"utf8\" | \"wtf8\";\n}\n\n/**\n * This function writes the bytes of a string into `out` and gives the count of the bytes. With no `out`, it only counts.\n * It reads the UTF-16 code units, so it makes no string for each character.\n */\nfunction writeBytes(text: string, wtf8: boolean, out?: Uint8Array): number {\n let n = 0;\n for (let i = 0; i < text.length; i++) {\n let c = text.charCodeAt(i);\n if (c >= 0xd800 && c < 0xe000) {\n const low = i + 1 < text.length ? text.charCodeAt(i + 1) : 0;\n if (c < 0xdc00 && low >= 0xdc00 && low < 0xe000) {\n c = 0x10000 + ((c - 0xd800) << 10) + (low - 0xdc00);\n i++;\n } else if (!wtf8) c = 0xfffd;\n }\n if (c < 0x80) {\n if (out !== undefined) out[n] = c;\n n += 1;\n } else if (c < 0x800) {\n if (out !== undefined) {\n out[n] = 0xc0 | (c >> 6);\n out[n + 1] = 0x80 | (c & 63);\n }\n n += 2;\n } else if (c < 0x10000) {\n if (out !== undefined) {\n out[n] = 0xe0 | (c >> 12);\n out[n + 1] = 0x80 | ((c >> 6) & 63);\n out[n + 2] = 0x80 | (c & 63);\n }\n n += 3;\n } else {\n if (out !== undefined) {\n out[n] = 0xf0 | (c >> 18);\n out[n + 1] = 0x80 | ((c >> 12) & 63);\n out[n + 2] = 0x80 | ((c >> 6) & 63);\n out[n + 3] = 0x80 | (c & 63);\n }\n n += 4;\n }\n }\n return n;\n}\n\n/**\n * This function gives the SHA-256 digest of the bytes of a string, as 64 lowercase hex characters.\n * The option `input` selects the bytes of a lone surrogate. It is not for passwords: it has no salt and no cost.\n */\nexport function sha256Hex(text: string, options?: Sha256Options): string {\n const input = options?.input ?? \"utf8\";\n if (input !== \"utf8\" && input !== \"wtf8\") throw new RangeError(`sha256Hex: input is \"utf8\" or \"wtf8\", not ${String(input)}.`);\n const wtf8 = input === \"wtf8\";\n // The bytes go straight into the padded buffer, so the digest makes one array, not a list of the bytes and a copy.\n const length = writeBytes(text, wtf8);\n const bits = length * 8;\n const total = Math.ceil((length + 9) / 64) * 64;\n const data = new Uint8Array(total);\n writeBytes(text, wtf8, data);\n data[length] = 0x80;\n const view = new DataView(data.buffer);\n view.setUint32(total - 8, Math.floor(bits / 0x100000000));\n view.setUint32(total - 4, bits >>> 0);\n const h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];\n const w = new Uint32Array(64);\n const rotr = (x: number, n: number): number => (x >>> n) | (x << (32 - n));\n for (let off = 0; off < total; off += 64) {\n for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + 4 * i);\n for (let i = 16; i < 64; i++) {\n const a = w[i - 15]!;\n const b = w[i - 2]!;\n const s0 = rotr(a, 7) ^ rotr(a, 18) ^ (a >>> 3);\n const s1 = rotr(b, 17) ^ rotr(b, 19) ^ (b >>> 10);\n w[i] = (w[i - 16]! + s0 + w[i - 7]! + s1) >>> 0;\n }\n let [a, b, c, d, e, f, g, k] = h as [number, number, number, number, number, number, number, number];\n for (let i = 0; i < 64; i++) {\n const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);\n const ch = (e & f) ^ (~e & g);\n const t1 = (k + s1 + ch + K[i]! + w[i]!) >>> 0;\n const s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);\n const maj = (a & b) ^ (a & c) ^ (b & c);\n const t2 = (s0 + maj) >>> 0;\n k = g;\n g = f;\n f = e;\n e = (d + t1) >>> 0;\n d = c;\n c = b;\n b = a;\n a = (t1 + t2) >>> 0;\n }\n h[0] = (h[0]! + a) >>> 0;\n h[1] = (h[1]! + b) >>> 0;\n h[2] = (h[2]! + c) >>> 0;\n h[3] = (h[3]! + d) >>> 0;\n h[4] = (h[4]! + e) >>> 0;\n h[5] = (h[5]! + f) >>> 0;\n h[6] = (h[6]! + g) >>> 0;\n h[7] = (h[7]! + k) >>> 0;\n }\n return h.map((x) => x.toString(16).padStart(8, \"0\")).join(\"\");\n}\n\n/** The options of `canonicalJson` and `canonicalHash`. */\nexport interface CanonicalOptions {\n /**\n * The form of the values that JSON does not have:\n *\n * - `\"jcs\"` (the default): RFC 8785. `NaN`, an infinite number and a string with a lone surrogate cause a `RangeError`.\n * - `\"json\"`: the form of `JSON.stringify`. `NaN` and an infinite number become `null`, and a lone surrogate becomes an escape.\n */\n readonly form?: \"jcs\" | \"json\";\n /**\n * The form of a `bigint`. `\"error\"` (the default) causes a `TypeError`, as `JSON.stringify` does.\n * `\"suffix\"` writes the digits and the letter `n`, for example `12n`. This text is not JSON, so use it only for a key.\n */\n readonly bigint?: \"error\" | \"suffix\";\n}\n\n/**\n * This function gives the canonical form of a value. The default form is RFC 8785 (JCS):\n *\n * - The keys of each object are in the order of their UTF-16 code units, with no whitespace.\n * - The numbers and the strings have the form of `JSON.stringify`, which is the form of RFC 8785.\n * - A key with the value `undefined` is not in the form. An `undefined` in an array, or as the value, becomes `null`.\n * - An object with a method `toJSON`, for example a `Date`, gives the form of the result of that method.\n * - A function, a symbol and a cycle cause a `TypeError`.\n *\n * `canonicalJson(JSON.parse(canonicalJson(v)))` is equal to `canonicalJson(v)`.\n */\nexport function canonicalJson(value: unknown, options?: CanonicalOptions): string {\n const form = options?.form ?? \"jcs\";\n const bigint = options?.bigint ?? \"error\";\n if (form !== \"jcs\" && form !== \"json\") throw new RangeError(`canonicalJson: form is \"jcs\" or \"json\", not ${String(form)}.`);\n if (bigint !== \"error\" && bigint !== \"suffix\") throw new RangeError(`canonicalJson: bigint is \"error\" or \"suffix\", not ${String(bigint)}.`);\n const jcs = form === \"jcs\";\n const open = new Set<object>();\n const text = (s: string): string => {\n if (jcs && !(s as string & { isWellFormed(): boolean }).isWellFormed()) throw new RangeError(`canonicalJson: the string ${JSON.stringify(s)} has a lone surrogate, which RFC 8785 does not allow.`);\n return JSON.stringify(s);\n };\n const write = (v: unknown, key: string): string => {\n if (v !== null && typeof v === \"object\" && typeof (v as { toJSON?: unknown }).toJSON === \"function\") v = (v as { toJSON(key: string): unknown }).toJSON(key);\n if (v === undefined || v === null) return \"null\";\n switch (typeof v) {\n case \"boolean\":\n return v ? \"true\" : \"false\";\n case \"number\":\n if (Number.isFinite(v)) return JSON.stringify(v);\n if (jcs) throw new RangeError(`canonicalJson: ${String(v)} is not a JSON number.`);\n return \"null\";\n case \"string\":\n return text(v);\n case \"bigint\":\n if (bigint === \"suffix\") return `${v}n`;\n throw new TypeError(`canonicalJson: the bigint ${v}n has no JSON form. Use the option bigint: \"suffix\" for a key.`);\n case \"object\":\n break;\n default:\n throw new TypeError(`canonicalJson: a ${typeof v} has no JSON form.`);\n }\n const o = v as object;\n if (open.has(o)) throw new TypeError(\"canonicalJson: the value has a cycle.\");\n open.add(o);\n let out: string;\n if (Array.isArray(o)) out = `[${o.map((item: unknown, i) => write(item, String(i))).join(\",\")}]`;\n else {\n const r = o as Record<string, unknown>;\n const keys = Object.keys(r).filter((k) => r[k] !== undefined).sort();\n out = `{${keys.map((k) => `${text(k)}:${write(r[k], k)}`).join(\",\")}}`;\n }\n open.delete(o);\n return out;\n };\n return write(value, \"\");\n}\n\n/** This function gives the content hash of a value: `sha256Hex(canonicalJson(value, options))`. */\nexport function canonicalHash(value: unknown, options?: CanonicalOptions): string {\n return sha256Hex(canonicalJson(value, options));\n}\n"],"mappings":";;;;;;;;AAQA,MAAM,IAAuB;CAC3B;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAC5J;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;CAAY;AACtF;;;;;AAmBA,SAAS,WAAW,MAAc,MAAe,KAA0B;CACzE,IAAI,IAAI;CACR,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;EACpC,IAAI,IAAI,KAAK,WAAW,CAAC;EACzB,IAAI,KAAK,SAAU,IAAI,OAAQ;GAC7B,MAAM,MAAM,IAAI,IAAI,KAAK,SAAS,KAAK,WAAW,IAAI,CAAC,IAAI;GAC3D,IAAI,IAAI,SAAU,OAAO,SAAU,MAAM,OAAQ;IAC/C,IAAI,SAAY,IAAI,SAAW,OAAO,MAAM;IAC5C;GACF,OAAO,IAAI,CAAC,MAAM,IAAI;EACxB;EACA,IAAI,IAAI,KAAM;GACZ,IAAI,QAAQ,KAAA,GAAW,IAAI,KAAK;GAChC,KAAK;EACP,OAAO,IAAI,IAAI,MAAO;GACpB,IAAI,QAAQ,KAAA,GAAW;IACrB,IAAI,KAAK,MAAQ,KAAK;IACtB,IAAI,IAAI,KAAK,MAAQ,IAAI;GAC3B;GACA,KAAK;EACP,OA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,KAAK,IAAI,CAAC,GAAG,MAAM,IAAI,UAAU,uCAAuC;EAC5E,KAAK,IAAI,CAAC;EACV,IAAI;EACJ,IAAI,MAAM,QAAQ,CAAC,GAAG,MAAM,IAAI,EAAE,KAAK,MAAe,MAAM,MAAM,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;OACzF;GACH,MAAM,IAAI;GAEV,MAAM,IADO,OAAO,KAAK,CAAC,CAAC,CAAC,QAAQ,MAAM,EAAE,OAAO,KAAA,CAAS,CAAC,CAAC,KACjD,CAAC,CAAC,KAAK,MAAM,GAAG,KAAK,CAAC,EAAE,GAAG,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE;EACtE;EACA,KAAK,OAAO,CAAC;EACb,OAAO;CACT;CACA,OAAO,MAAM,OAAO,EAAE;AACxB;;AAGA,SAAgB,cAAc,OAAgB,SAAoC;CAChF,OAAO,UAAU,cAAc,OAAO,OAAO,CAAC;AAChD"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Random, Source, deriveSeed, fnv1a32, mulberry32, random, randomOf, seedOf, seedsFrom, stablePick } from "./random.js";
|
|
2
2
|
import { DistributionSpec, Param, dependsOnLibm, sampler } from "./distributions.js";
|
|
3
|
+
import { CanonicalOptions, Sha256Options, canonicalHash, canonicalJson, sha256Hex } from "./hash.js";
|
|
3
4
|
import { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, RecordedTable, recordedSpec } from "./recorded.js";
|
|
4
|
-
export { DIAGRAM_BOX_AREA, type DistributionSpec, GIT_BLOB_BYTES, type Param, RECORDED_TABLES, type Random, type RecordedTable, type Source, dependsOnLibm, deriveSeed, fnv1a32, mulberry32, random, randomOf, recordedSpec, sampler, seedOf, seedsFrom, stablePick };
|
|
5
|
+
export { type CanonicalOptions, DIAGRAM_BOX_AREA, type DistributionSpec, GIT_BLOB_BYTES, type Param, RECORDED_TABLES, type Random, type RecordedTable, type Sha256Options, type Source, canonicalHash, canonicalJson, dependsOnLibm, deriveSeed, fnv1a32, mulberry32, random, randomOf, recordedSpec, sampler, seedOf, seedsFrom, sha256Hex, stablePick };
|
package/build/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { dependsOnLibm, sampler } from "./distributions.js";
|
|
2
|
+
import { canonicalHash, canonicalJson, sha256Hex } from "./hash.js";
|
|
2
3
|
import { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, recordedSpec } from "./recorded.js";
|
|
3
4
|
import { deriveSeed, fnv1a32, mulberry32, random, randomOf, seedOf, seedsFrom, stablePick } from "./random.js";
|
|
4
|
-
export { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, dependsOnLibm, deriveSeed, fnv1a32, mulberry32, random, randomOf, recordedSpec, sampler, seedOf, seedsFrom, stablePick };
|
|
5
|
+
export { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, canonicalHash, canonicalJson, dependsOnLibm, deriveSeed, fnv1a32, mulberry32, random, randomOf, recordedSpec, sampler, seedOf, seedsFrom, sha256Hex, stablePick };
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/rigor-generators",
|
|
4
|
-
"version": "0.1.
|
|
5
|
-
"description": "A seeded random number generator (mulberry32) with the draws that tests and benchmarks make: integers, picks, weights, shuffles, samples, normal values and named streams.",
|
|
4
|
+
"version": "0.1.7",
|
|
5
|
+
"description": "A seeded random number generator (mulberry32) with the draws that tests and benchmarks make: integers, picks, weights, shuffles, samples, normal values and named streams. It also gives a content hash: SHA-256 and canonical JSON (RFC 8785).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"random",
|
|
8
8
|
"seed",
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
"prng",
|
|
11
11
|
"mulberry32",
|
|
12
12
|
"fnv1a",
|
|
13
|
+
"sha256",
|
|
14
|
+
"canonical-json",
|
|
15
|
+
"jcs",
|
|
13
16
|
"deterministic",
|
|
14
17
|
"testing",
|
|
15
18
|
"benchmark",
|