| 1 |
SipHash implementation for Rust |
| 2 |
=============================== |
| 3 |
|
| 4 |
This crates implements SipHash-2-4 and SipHash-1-3 in Rust. |
| 5 |
|
| 6 |
It is based on the original implementation from rust-core and exposes the |
| 7 |
same API. |
| 8 |
|
| 9 |
It also implements SipHash variants returning 128-bit tags. |
| 10 |
|
| 11 |
The `sip` module implements the standard 64-bit mode, whereas the `sip128` |
| 12 |
module implements the 128-bit mode. |
| 13 |
|
| 14 |
Usage |
| 15 |
----- |
| 16 |
|
| 17 |
In `Cargo.toml`: |
| 18 |
|
| 19 |
```toml |
| 20 |
[dependencies] |
| 21 |
siphasher = "1" |
| 22 |
``` |
| 23 |
|
| 24 |
If you want [serde](https://github.com/serde-rs/serde) support, include the feature like this: |
| 25 |
|
| 26 |
```toml |
| 27 |
[dependencies] |
| 28 |
siphasher = { version = "1", features = ["serde"] } |
| 29 |
``` |
| 30 |
|
| 31 |
64-bit mode: |
| 32 |
|
| 33 |
```rust |
| 34 |
use siphasher::sip::{SipHasher, SipHasher13, SipHasher24}; |
| 35 |
|
| 36 |
// one-shot: |
| 37 |
|
| 38 |
let array: &[u8] = &[1, 2, 3]; |
| 39 |
let key: &[u8; 16] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 40 |
let hasher = SipHasher13::new_with_key(key); |
| 41 |
let h = hasher.hash(array); |
| 42 |
|
| 43 |
// incremental: |
| 44 |
|
| 45 |
use core::hash::Hasher; |
| 46 |
|
| 47 |
let array1: &[u8] = &[1, 2, 3]; |
| 48 |
let array2: &[u8] = &[4, 5, 6]; |
| 49 |
let key: &[u8; 16] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 50 |
let mut hasher = SipHasher13::new_with_key(key); |
| 51 |
hasher.write(array1); |
| 52 |
hasher.write(array2); |
| 53 |
let h = hasher.finish(); |
| 54 |
``` |
| 55 |
|
| 56 |
128-bit mode: |
| 57 |
|
| 58 |
```rust |
| 59 |
use siphasher::sip128::{Hasher128, SipHasher, SipHasher13, SipHasher24}; |
| 60 |
|
| 61 |
// one-shot: |
| 62 |
|
| 63 |
let array: &[u8] = &[1, 2, 3]; |
| 64 |
let key: &[u8; 16] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 65 |
let hasher = SipHasher13::new_with_key(key); |
| 66 |
let h = hasher.hash(array).as_bytes(); |
| 67 |
|
| 68 |
// incremental: |
| 69 |
|
| 70 |
use core::hash::Hasher; |
| 71 |
|
| 72 |
let array1: &[u8] = &[1, 2, 3]; |
| 73 |
let array2: &[u8] = &[4, 5, 6]; |
| 74 |
let key: &[u8; 16] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 75 |
let mut hasher = SipHasher13::new_with_key(key); |
| 76 |
hasher.write(array1); |
| 77 |
hasher.write(array2); |
| 78 |
let h = hasher.finish128().as_bytes(); |
| 79 |
``` |
| 80 |
|
| 81 |
[API documentation](https://docs.rs/siphasher/) |
| 82 |
----------------------------------------------- |
| 83 |
|
| 84 |
Note |
| 85 |
---- |
| 86 |
|
| 87 |
Due to a confusing and not well documented API, methods from the `Hasher` trait of the standard library (`std::hash::Hasher`, `core::hash::Hasher`) produce non-portable results. |
| 88 |
|
| 89 |
This is not specific to SipHash, and affects all hash functions. |
| 90 |
|
| 91 |
The only safe methods in that trait are `write()` and `finish()`. |
| 92 |
|
| 93 |
It is thus recommended to use SipHash (and all other hash functions, actually) as documented above. |