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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! A stable hash of a structured value.
//!
//! Given a value — a map, an array, a string, a number, nested in any
//! combination — this produces a short digest that is the same every time for
//! values that are equal. Not merely equal within one run of a program: equal
//! across runs, across processes, and across machines.
//!
//! # Why `Hash` is not enough
//!
//! `std::hash::Hash` looks like the right tool and is not. The default hasher,
//! `RandomState`, is seeded per process, so the same value hashes differently
//! every time the program starts. That is exactly right for a `HashMap`, where
//! the seed is what stops an attacker choosing collisions, and exactly wrong for
//! anything that has to be written down or compared across processes. The other
//! half of the problem is that a `HashMap` iterates in an unspecified order, so
//! even with a fixed hasher two equal maps can hash differently.
//!
//! So the value has to be normalised first: written into a canonical form that
//! depends only on what the value is. Then the canonical bytes are hashed. This
//! crate does both.
//!
//! # What it is for
//!
//! - **Cache keys.** A key built from the request parameters has to hit the same
//! entry for the same request.
//! - **ETags.** An entity tag should change when the content changes and not
//! when a field is reordered.
//! - **Idempotency keys.** Recognise a repeated request as the same action.
//! - **Change detection and deduplication.** Compare two digests instead of two
//! documents.
//!
//! # Example
//!
//! ```
//! use object_hash::{Value, hash};
//!
//! let one = Value::Map(vec![
//! ("name".into(), Value::String("cipansor".into())),
//! ("year".into(), Value::Number(2026.0)),
//! ]);
//! // The same entries, inserted the other way round.
//! let two = Value::Map(vec![
//! ("year".into(), Value::Number(2026.0)),
//! ("name".into(), Value::String("cipansor".into())),
//! ]);
//!
//! assert_eq!(hash(&one), hash(&two), "key order does not matter for a map");
//!
//! let three = Value::Map(vec![
//! ("name".into(), Value::String("cipansor".into())),
//! ("year".into(), Value::Number(2027.0)),
//! ]);
//! assert_ne!(hash(&one), hash(&three), "a changed value changes the digest");
//! ```
//!
//! # Compatibility
//!
//! The canonical form is the one the `object-hash` npm package produces, byte
//! for byte, and the digest is SHA-1 in lowercase hexadecimal, which is its
//! default. A digest computed here equals one computed there for the same value,
//! so a Rust service and a Node service can agree on a cache key without either
//! knowing about the other. That is also why a map is written as an array of
//! pairs and an integral float has no point: the reference writes them that way,
//! and agreeing with it is worth more than tidier output.
//!
//! The one thing that does not carry over is the reference's handling of
//! JavaScript objects. An object there is a thing with a prototype and a
//! constructor, and it mixes those into the hash; a `Value::Map` here is just
//! its entries. That is a deliberate difference: a Rust caller has no prototype
//! to respect, and mixing one in would make the digest depend on the library
//! rather than on the value.
//!
//! # What it is not
//!
//! SHA-1 is not used here as a security primitive. The digest names a value; it
//! does not protect it. Do not treat a digest as a signature or as proof that a
//! value came from somewhere. If a stronger digest is wanted, hash the canonical
//! bytes — [`canonical`] returns them — with any other function.
/// The README, compiled as a doctest so its example cannot drift from the API.
;
use fmt;
/// A value that can be hashed.
///
/// This is the set of things the reference can hash, minus the ones that only
/// exist in JavaScript: there is no case for a function, a symbol, or a `Date`,
/// because a Rust caller would not pass one. A `Map` here stands for the
/// reference's plain object and its `Map`, which it treats the same way.
/// The canonical form of a value, as text.
///
/// Two values that are equal have the same canonical form. It is returned rather
/// than hashed so a caller can use a different digest, or none.
/// The digest of a value, as lowercase hexadecimal SHA-1.
///
/// The digest is stable across runs and machines for equal values. See the
/// crate documentation for what equal means here.
///
/// # Example
///
/// ```
/// use object_hash::{hash, Value};
///
/// let left = Value::Map(vec![
/// ("b".into(), Value::number(2)),
/// ("a".into(), Value::string("one")),
/// ]);
/// // A map's key order does not change the digest.
/// let right = Value::Map(vec![
/// ("a".into(), Value::string("one")),
/// ("b".into(), Value::number(2)),
/// ]);
/// assert_eq!(hash(&left), hash(&right));
///
/// // An array's order does.
/// assert_ne!(
/// hash(&Value::Array(vec![Value::number(1), Value::number(2)])),
/// hash(&Value::Array(vec![Value::number(2), Value::number(1)])),
/// );
/// ```