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
//! This library provides some algorithms to calculate cryptographically secure digests of JSON documents.
//! Since JSON is an ambiguous serialization format, we also had to define a canonical deterministic subset
//! of all allowed documents. Order of keys in an object and Unicode normalization are well-defined in this
//! subset, making it suitable for hashing.
use *;
use *;
/// Converts any error that can be converted into a string into a JavaScript error string
/// usable by wasm_bindgen
/// An extension trait on [`Result`] that helps easy conversion of Rust errors to JavaScript
/// error strings usable by wasm_bindgen
/// Returns a canonical string representation of a JSON document, in which any sub-objects not explicitly listed in the
/// second argument are collapsed to their digest. The format of the second argument is inspired by
/// [JQ basic filters](https://stedolan.github.io/jq/manual/#Basicfilters) and these are some examples:
///
/// ```json
/// {
/// "a": {
/// "1": "apple",
/// "2": "banana"
/// },
/// "b": ["some", "array", 0xf, "values"],
/// "c": 42
/// }
/// ```
///
/// - "" -> Same as calling {@link digestJson}
/// - ".a" -> Keep property "a" untouched, the rest will be replaced with their digest. Note that most likely the scalar number "c"
/// does not have enough entropy to avoid a brute-force attack for its digest.
/// - ".b, .c" -> Keeps both properties "b" and "c" unaltered, but "a" will be replaced with the digest of that sub-object.
///
/// You should protect scalar values and easy-to-guess lists by replacing them with an object that has an extra "nonce" property, which
/// has enough entropy. @see wrapJsonWithNonce
/// Calculates the digest of a JSON document. Since this digest is calculated by recursively replacing sub-objects with their digest,
/// it is possible to selectively reveal parts of the document using {@link selectiveDigestJson}
/// This function provides a canonical string for any JSON document. Order of the keys in objects, whitespace
/// and unicode normalization are all taken care of, so document that belongs to a single digest is not malleable.
///
/// This is a drop-in replacement for `JSON.stringify(data)`
/// You should protect scalar values and easy-to-guess lists by replacing them with an object that has an extra "nonce" property, which
/// has enough entropy. List of all countries, cities in a country, streets in a city are all easy to enumerate for a brute-fore
/// attack.
///
/// For example if you have a string that is a country, you can call this function like `wrapJsonWithNonce("Germany")` and get an
/// object like the following:
///
/// ```json
/// {
/// "nonce": "ukhFsI4a6vIZEDUOBRxJmLroPEQ8FQCjJwbI-Z7bEocGo",
/// "value": "Germany"
/// }
/// ```