ps-hash 0.1.0-25

Generates 77-character Base32 or 64-character base64url hashes with 128 bits of security.
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use crate::{Hash, HASH_SIZE_BIN, HASH_SIZE_COMPACT, HASH_SIZE_CROCKFORD, PARITY_SIZE};

/// Replaces the character at `index` with a different one that is valid in
/// both alphabets, so that corruption tests stay inside the encoded character
/// set.
fn corrupt(bytes: &mut [u8], index: usize) {
    bytes[index] = if bytes[index] == b'A' { b'B' } else { b'A' };
}

// ============================================================================
// Basic Hash Creation and Validation
// ============================================================================

#[test]
fn test_hash_creation_empty_data() {
    let result = Hash::hash([]);
    assert!(result.is_ok());
    let hash = result.unwrap();
    assert_eq!(hash.to_string().len(), HASH_SIZE_CROCKFORD);
}

#[test]
fn test_hash_creation_non_empty_data() {
    let data = b"test data";
    let result = Hash::hash(data);
    assert!(result.is_ok());
    let hash = result.unwrap();
    assert_eq!(hash.to_string().len(), HASH_SIZE_CROCKFORD);
}

#[test]
fn test_hash_creation_large_data() {
    let data = vec![0u8; 1_000_000];
    let result = Hash::hash(&data);
    assert!(result.is_ok());
}

#[test]
fn test_hash_deterministic() {
    let data = b"deterministic test";
    let hash1 = Hash::hash(data).unwrap();
    let hash2 = Hash::hash(data).unwrap();
    assert_eq!(hash1, hash2);
}

#[test]
fn test_hash_different_inputs_different_outputs() {
    let hash1 = Hash::hash(b"data1").unwrap();
    let hash2 = Hash::hash(b"data2").unwrap();
    assert_ne!(hash1, hash2);
}

// ============================================================================
// Hash Validation
// ============================================================================

#[test]
fn test_validate_uncorrupted_hash() {
    let original = Hash::hash(b"validation test").unwrap();
    let validated = Hash::validate(original.to_string()).unwrap();
    assert_eq!(original, validated);
}

#[test]
fn test_validate_single_character_corruption() {
    let original = Hash::hash(b"corruption test").unwrap();
    let mut corrupted = original.to_string().into_bytes();

    corrupt(&mut corrupted, 5);

    let corrupted_str = String::from_utf8(corrupted).unwrap();
    let result = Hash::validate(&corrupted_str);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), original);
}

#[test]
fn test_validate_multi_character_corruption_recoverable() {
    let original = Hash::hash(b"char corruption").unwrap();
    let mut corrupted = original.to_string().into_bytes();

    // Four character replacements stay within the seven-byte correction budget.
    for index in [5, 17, 33, 61] {
        corrupt(&mut corrupted, index);
    }

    let corrupted_str = String::from_utf8(corrupted).unwrap();
    let result = Hash::validate(&corrupted_str);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), original);
}

#[test]
fn test_validate_unrecoverable_corruption() {
    let original = Hash::hash(b"unrecoverable").unwrap();
    let mut corrupted = original.to_string().into_bytes();

    for index in 0..30 {
        corrupt(&mut corrupted, index);
    }

    let corrupted_str = String::from_utf8(corrupted).unwrap();
    let result = Hash::validate(&corrupted_str);

    assert!(result.is_err());
}

#[test]
fn test_validate_bin_uncorrupted() {
    let original = Hash::hash(b"binary validation").unwrap();
    let compact = original.compact().to_vec();
    let mut binary = compact;
    binary.resize(HASH_SIZE_BIN, 0);
    let result = Hash::validate(&mut binary);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), original);
}

#[test]
fn test_validate_bin_corrupted() {
    let original = Hash::hash(b"binary corruption").unwrap();
    let mut binary = original.compact().to_vec();

    // Corrupt one byte; together with the six truncated parity bytes this
    // stays within the seven-byte correction budget.
    binary[0] ^= 0xFF;

    let result = Hash::validate(&binary);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), original);
}

// ============================================================================
// Internal Representation and Accessors
// ============================================================================

#[test]
fn test_compact_returns_correct_size() {
    let hash = Hash::hash(b"compact test").unwrap();
    assert_eq!(hash.compact().len(), HASH_SIZE_COMPACT);
}

#[test]
fn test_compact_round_trips_via_validate_bin_vec() {
    let original = Hash::hash(b"compact slice").unwrap();
    let mut vec = original.compact().to_vec();
    let recovered = Hash::validate_bin_vec(&mut vec).unwrap();
    assert_eq!(original, recovered);
}

#[test]
fn test_length_accessor() {
    let data = b"length test";
    let hash = Hash::hash(data).unwrap();
    assert_eq!(hash.data_max_len().to_usize(), data.len());
}

#[test]
fn test_length_accessor_zero() {
    let hash = Hash::hash(b"").unwrap();
    assert_eq!(hash.data_max_len().to_usize(), 0);
}

#[test]
fn test_digest_accessor() {
    let hash = Hash::hash(b"digest test").unwrap();
    let digest = hash.digest();
    // Digest should be first 32 bytes of inner
    assert_eq!(digest.len(), 32);
}

#[test]
fn test_parity_accessor() {
    let hash = Hash::hash(b"parity test").unwrap();
    let parity = hash.parity();
    assert_eq!(parity.len(), PARITY_SIZE);
}

#[test]
fn test_to_vec_consistency() {
    let hash = Hash::hash(b"to_vec test").unwrap();
    let vec = Vec::from(hash);
    assert_eq!(vec, hash.to_string().into_bytes());
}

#[test]
fn test_data_max_len_small() {
    let data = b"test";
    let hash = Hash::hash(data).unwrap();
    let max_len = hash.data_max_len().to_usize();
    assert_eq!(max_len, data.len());
}

#[test]
fn test_data_max_len_large() {
    let data = vec![42u8; 65536];
    let hash = Hash::hash(&data).unwrap();
    let max_len = hash.data_max_len().to_usize();
    assert_eq!(max_len, data.len());
}

// ============================================================================
// Trait Implementations
// ============================================================================

#[test]
fn test_display_trait() {
    let hash = Hash::hash(b"display").unwrap();
    let displayed = format!("{hash}");
    assert_eq!(displayed, hash.to_string());
}

#[test]
fn test_debug_trait() {
    let hash = Hash::hash(b"debug").unwrap();
    let debug_str = format!("{hash:?}");
    assert!(!debug_str.is_empty());
}

#[test]
fn test_hash_trait_consistency() {
    use std::collections::HashSet;
    let hash1 = Hash::hash(b"hash trait").unwrap();
    let hash2 = Hash::hash(b"hash trait").unwrap();

    let mut set = HashSet::new();
    set.insert(hash1);
    assert!(set.contains(&hash2));
}

#[test]
fn test_partial_eq_same() {
    let hash1 = Hash::hash(b"eq test").unwrap();
    let hash2 = Hash::hash(b"eq test").unwrap();
    assert_eq!(hash1, hash2);
}

#[test]
fn test_partial_eq_different() {
    let hash1 = Hash::hash(b"eq1").unwrap();
    let hash2 = Hash::hash(b"eq2").unwrap();
    assert_ne!(hash1, hash2);
}

#[test]
fn test_partial_eq_corrupted_recoverable() {
    let original = Hash::hash(b"eq corrupted").unwrap();
    let mut corrupted = original.to_string().into_bytes();

    corrupt(&mut corrupted, 3);

    let corrupted_str = String::from_utf8(corrupted).unwrap();
    let recovered = Hash::validate(&corrupted_str).unwrap();
    assert_eq!(original, recovered);
}

#[test]
fn test_ord_trait() {
    let hash1 = Hash::hash(b"ord1").unwrap();
    let hash2 = Hash::hash(b"ord2").unwrap();
    let _ = std::cmp::Ordering::Less;
    let _ = hash1.cmp(&hash2);
}

#[test]
fn test_ord_trait_symmetry() {
    let hash = Hash::hash(b"symmetry").unwrap();
    assert_eq!(hash.cmp(&hash), std::cmp::Ordering::Equal);
}

// ============================================================================
// Type Conversions
// ============================================================================

#[test]
fn test_from_hash_to_array() {
    let hash = Hash::hash(b"to array").unwrap();
    let array: [u8; HASH_SIZE_CROCKFORD] = hash.into();
    assert_eq!(array.len(), HASH_SIZE_CROCKFORD);
}

#[test]
fn test_from_hash_ref_to_string() {
    let hash = Hash::hash(b"to string").unwrap();
    let string: String = (&hash).into();
    assert_eq!(string, hash.to_string());
}

#[test]
fn test_from_hash_ref_to_vec() {
    let hash = Hash::hash(b"to vec").unwrap();
    let vec: Vec<u8> = (&hash).into();
    assert_eq!(vec, hash.to_string().into_bytes());
}

#[test]
fn test_try_from_valid_str() {
    let original = Hash::hash(b"from str").unwrap();
    let string = original.to_string();
    let hash = Hash::try_from(string.as_str()).unwrap();
    assert_eq!(hash, original);
}

#[test]
fn test_try_from_valid_slice() {
    let original = Hash::hash(b"from slice").unwrap();
    let bytes = original.to_string().into_bytes();
    let hash = Hash::try_from(bytes.as_slice()).unwrap();
    assert_eq!(hash, original);
}

#[test]
fn test_try_from_invalid_str() {
    let result = Hash::try_from("invalid_hash");
    assert!(result.is_err());
}

#[test]
fn test_try_from_too_short() {
    let result = Hash::try_from("short");
    assert!(result.is_err());
}

#[test]
fn test_try_from_invalid_encoding() {
    // Length 50 falls between the binary and base64url ranges, so no
    // representation accepts it.
    let invalid = "!@#$%^&*()!@#$%^&*()!@#$%^&*()!@#$%^&*()!@#$%^&*()";
    let result = Hash::try_from(invalid);
    assert!(result.is_err());
}

// ============================================================================
// Edge Cases and Error Handling
// ============================================================================

#[test]
fn test_clone_copy_semantics() {
    let hash1 = Hash::hash(b"clone").unwrap();
    let hash2 = hash1;
    assert_eq!(hash1, hash2);
}

#[test]
fn test_copy_after_use() {
    let hash1 = Hash::hash(b"copy").unwrap();
    let _str = hash1.to_string();
    let hash2 = hash1; // Should work due to Copy
    assert_eq!(hash1, hash2);
}

#[test]
fn test_data_max_len_boundary() {
    for len in [0, 1, 255, 256, 65536, 0x2f000] {
        let data = vec![0u8; len];
        let hash = Hash::hash(&data).unwrap();
        let max_len = hash.data_max_len().to_usize();
        assert_eq!(max_len, len);
    }
}

#[test]
fn test_validate_bin_vec() {
    let original = Hash::hash(b"bin vec").unwrap();
    let compact = original.compact().to_vec();
    let mut binary = compact;
    binary.resize(HASH_SIZE_BIN, 0);
    let result = Hash::validate_bin_vec(&mut binary);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), original);
}

#[test]
fn test_consecutive_validations() {
    let data = b"consecutive";
    let original = Hash::hash(data).unwrap();

    for _ in 0..10 {
        let validated = Hash::validate(original.to_string()).unwrap();
        assert_eq!(validated, original);
    }
}

// ============================================================================
// Compact Representation Consistency
// ============================================================================

#[test]
fn test_compact_round_trip() {
    let original = Hash::hash(b"compact round trip").unwrap();
    let compact = original.compact().to_vec();
    let mut binary = compact;
    binary.resize(HASH_SIZE_BIN, 0);
    let recovered = Hash::validate(&mut binary).unwrap();
    assert_eq!(original, recovered);
}

#[test]
fn test_compact_preserves_data() {
    let data = b"compact preserves";
    let hash = Hash::hash(data).unwrap();
    let max_len_before = hash.data_max_len();

    let compact = hash.compact().to_vec();
    let mut binary = compact;
    binary.resize(HASH_SIZE_BIN, 0);
    let recovered = Hash::validate(&mut binary).unwrap();
    let max_len_after = recovered.data_max_len();

    assert_eq!(max_len_before, max_len_after);
}

// ============================================================================
// Mixed Validation Scenarios
// ============================================================================

#[test]
fn test_str_then_bin_validation() {
    let original = Hash::hash(b"mixed validation").unwrap();
    let string = original.to_string();
    let validated_str = Hash::validate(&string).unwrap();

    let mut binary = original.compact().to_vec();
    binary.resize(HASH_SIZE_BIN, 0);
    let validated_bin = Hash::validate(&mut binary).unwrap();

    assert_eq!(validated_str, validated_bin);
}

#[test]
fn test_inline_hash_function() {
    let data = b"inline hash";
    let hash1 = crate::hash(data).unwrap();
    let hash2 = Hash::hash(data).unwrap();
    assert_eq!(hash1, hash2);
}