| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.digest.murmurhash; |
| 4 |
|
| 5 |
// MurmurHash3!32, MurmurHash3!(128, 32) and MurmurHash3!(128, 64) implement |
| 6 |
// the std.digest Template API. |
| 7 |
static assert(isDigest!(MurmurHash3!32)); |
| 8 |
// The convenient digest template allows for quick hashing of any data. |
| 9 |
ubyte[4] hashed = digest!(MurmurHash3!32)([1, 2, 3, 4]); |
| 10 |
assert(hashed == [0, 173, 69, 68]); |
| 11 |
} |
| 12 |
|
| 13 |
@safe unittest |
| 14 |
{ |
| 15 |
import std.digest.murmurhash; |
| 16 |
|
| 17 |
// One can also hash ubyte data piecewise by instanciating a hasher and call |
| 18 |
// the 'put' method. |
| 19 |
const(ubyte)[] data1 = [1, 2, 3]; |
| 20 |
const(ubyte)[] data2 = [4, 5, 6, 7]; |
| 21 |
// The incoming data will be buffered and hashed element by element. |
| 22 |
MurmurHash3!32 hasher; |
| 23 |
hasher.put(data1); |
| 24 |
hasher.put(data2); |
| 25 |
// The call to 'finish' ensures: |
| 26 |
// - the remaining bits are processed |
| 27 |
// - the hash gets finalized |
| 28 |
auto hashed = hasher.finish(); |
| 29 |
assert(hashed == [181, 151, 88, 252]); |
| 30 |
} |
| 31 |
|
| 32 |
@safe unittest |
| 33 |
{ |
| 34 |
import std.digest.murmurhash; |
| 35 |
|
| 36 |
// Using `putElements`, `putRemainder` and `finalize` you gain full |
| 37 |
// control over which part of the algorithm to run. |
| 38 |
// This allows for maximum throughput but needs extra care. |
| 39 |
|
| 40 |
// Data type must be the same as the hasher's element type: |
| 41 |
// - uint for MurmurHash3!32 |
| 42 |
// - uint[4] for MurmurHash3!(128, 32) |
| 43 |
// - ulong[2] for MurmurHash3!(128, 64) |
| 44 |
const(uint)[] data = [1, 2, 3, 4]; |
| 45 |
// Note the hasher starts with 'Fast'. |
| 46 |
MurmurHash3!32 hasher; |
| 47 |
// Push as many array of elements as you need. The less calls the better. |
| 48 |
hasher.putElements(data); |
| 49 |
// Put remainder bytes if needed. This method can be called only once. |
| 50 |
hasher.putRemainder(ubyte(1), ubyte(1), ubyte(1)); |
| 51 |
// Call finalize to incorporate data length in the hash. |
| 52 |
hasher.finalize(); |
| 53 |
// Finally get the hashed value. |
| 54 |
auto hashed = hasher.getBytes(); |
| 55 |
assert(hashed == [188, 165, 108, 2]); |
| 56 |
} |
| 57 |
|
| 58 |
@safe unittest |
| 59 |
{ |
| 60 |
import std.digest.murmurhash; |
| 61 |
|
| 62 |
ubyte[4] hashed = digest!(MurmurHash3!32)([1, 2, 3, 4]); |
| 63 |
assert(hashed == [0, 173, 69, 68]); |
| 64 |
} |
| 65 |
|
| 66 |
@safe unittest |
| 67 |
{ |
| 68 |
import std.digest.murmurhash; |
| 69 |
|
| 70 |
const(ubyte)[] data1 = [1, 2, 3]; |
| 71 |
const(ubyte)[] data2 = [4, 5, 6, 7]; |
| 72 |
// The incoming data will be buffered and hashed element by element. |
| 73 |
MurmurHash3!32 hasher; |
| 74 |
hasher.put(data1); |
| 75 |
hasher.put(data2); |
| 76 |
// The call to 'finish' ensures: |
| 77 |
// - the remaining bits are processed |
| 78 |
// - the hash gets finalized |
| 79 |
auto hashed = hasher.finish(); |
| 80 |
assert(hashed == [181, 151, 88, 252]); |
| 81 |
} |
| 82 |
|