You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-9Lines changed: 11 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,18 +53,20 @@ map.insert(56, 78);
53
53
The aHash package has the following flags:
54
54
*`std`: This enables features which require the standard library. (On by default) This includes providing the utility classes `AHashMap` and `AHashSet`.
55
55
*`serde`: Enables `serde` support for the utility classes `AHashMap` and `AHashSet`.
56
-
*`compile-time-rng`: Whenever possible aHash will seed hashers with random numbers using the [getrandom](https://github.com/rust-random/getrandom) crate.
57
-
This is possible for OS targets which provide a source of randomness. (see the [full list](https://docs.rs/getrandom/0.2.0/getrandom/#supported-targets).)
58
-
For OS targets without access to a random number generator, `compile-time-rng` provides an alternative.
56
+
*`runtime-rng`: To obtain a seed for Hashers will obtain randomness from the operating system. (On by default)
57
+
This is done using the [getrandom](https://github.com/rust-random/getrandom) crate.
58
+
*`compile-time-rng`: For OS targets without access to a random number generator, `compile-time-rng` provides an alternative.
59
59
If `getrandom` is unavailable and `compile-time-rng` is enabled, aHash will generate random numbers at compile time and embed them in the binary.
60
60
This allows for DOS resistance even if there is no random number generator available at runtime (assuming the compiled binary is not public).
61
-
This makes the binary non-deterministic, unless `getrandom` is available for the target in which case the flag does nothing.
62
-
(If non-determinism is a problem see [constrandom's documentation](https://github.com/tkaitchuck/constrandom#deterministic-builds))
61
+
This makes the binary non-deterministic. (If non-determinism is a problem see [constrandom's documentation](https://github.com/tkaitchuck/constrandom#deterministic-builds))
63
62
64
-
**NOTE:** If `getrandom` is unavailable and `compile-time-rng` is disabled aHash will fall back on using the numeric
65
-
value of memory addresses as a source of randomness. This is somewhat strong if ALSR is turned on (it is by default)
66
-
but for embedded platforms this will result in weak keys. As a result, it is recommended to use `compile-time-rng` anytime
67
-
random numbers will not be available at runtime.
63
+
If both `runtime-rng` and `compile-time-rng` are enabled the `runtime-rng` will take precedence and `compile-time-rng` will do nothing.
64
+
65
+
**NOTE:** If both `runtime-rng` and `compile-time-rng` a source of randomness may be provided by the application on startup
66
+
using the [ahash::random_state::set_random_source](https://docs.rs/ahash/latest/ahash/random_state/fn.set_random_source.html) method.
67
+
If neither flag is set and this is not done, aHash will fall back on using the numeric value of memory addresses as a source of randomness.
68
+
This is somewhat strong if ALSR is turned on (it is by default) but for embedded platforms this will result in weak keys.
69
+
As a result, it is recommended to use `compile-time-rng` anytime random numbers will not be available at runtime.
Copy file name to clipboardExpand all lines: src/lib.rs
+47-20Lines changed: 47 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,24 @@
1
-
//! AHash is a hashing algorithm is intended to be a high performance, (hardware specific), keyed hash function.
2
-
//! This can be seen as a DOS resistant alternative to `FxHash`, or a fast equivalent to `SipHash`.
3
-
//! It provides a high speed hash algorithm, but where the result is not predictable without knowing a Key.
4
-
//! This allows it to be used in a `HashMap` without allowing for the possibility that an malicious user can
1
+
//! AHash is a high performance keyed hash function.
2
+
//!
3
+
//! It is a DOS resistant alternative to `FxHash` or a faster alternative to `SipHash`.
4
+
//!
5
+
//! It quickly provides a high quality hash where the result is not predictable without knowing the Key.
6
+
//! AHash works with `HashMap` to hash keys, but without allowing for the possibility that an malicious user can
5
7
//! induce a collision.
6
8
//!
7
9
//! # How aHash works
8
10
//!
9
-
//! aHash uses the hardware AES instruction on x86 processors to provide a keyed hash function.
10
-
//! aHash is not a cryptographically secure hash.
11
+
//! When it is available aHash uses the hardware AES instructions to provide a keyed hash function.
12
+
//! When it is not, aHash falls back on a slightly slower alternative algorithm.
11
13
//!
14
+
//! AHash does not have a fixed standard for its output. This allows it to improve over time.
15
+
//! But this also means that different computers or computers using different versions of ahash will observe different
16
+
//! hash values.
12
17
#![cfg_attr(
13
18
feature = "std",
14
19
doc = r##"
15
-
# Example
20
+
# Usage
21
+
AHash is a drop in replacement for the default implementation of the Hasher trait. To construct a HashMap using aHash as its hasher do the following:
16
22
```
17
23
use ahash::{AHasher, RandomState};
18
24
use std::collections::HashMap;
@@ -25,25 +31,46 @@ map.insert(12, 34);
25
31
#![cfg_attr(
26
32
feature = "std",
27
33
doc = r##"
28
-
For convenience, both new-type wrappers and type aliases are provided. The new type wrappers are called called `AHashMap` and `AHashSet`. These do the same thing with slightly less typing.
29
-
The type aliases are called `ahash::HashMap`, `ahash::HashSet` are also provided and alias the
30
-
std::[HashMap] and std::[HashSet]. Why are there two options? The wrappers are convenient but
31
-
can't be used where a generic `std::collection::HashMap<K, V, S>` is required.
34
+
For convenience, both new-type wrappers and type aliases are provided.
32
35
36
+
The new type wrappers are called called `AHashMap` and `AHashSet`.
37
+
These do the same thing with slightly less typing. (For convience `From`, `Into`, and `Deref` are provided).
33
38
```
34
39
use ahash::AHashMap;
35
40
36
-
let mut map: AHashMap<i32, i32> = AHashMap::with_capacity(4);
41
+
let mut map: AHashMap<i32, i32> = AHashMap::new();
37
42
map.insert(12, 34);
38
-
map.insert(56, 78);
39
-
// There are also type aliases provieded together with some extension traits to make
40
-
// it more of a drop in replacement for the std::HashMap/HashSet
41
-
use ahash::{HashMapExt, HashSetExt}; // Used to get with_capacity()
42
-
let mut map = ahash::HashMap::with_capacity(10);
43
+
```
44
+
45
+
For even less typing and better interop with existing libraries which require a `std::collection::HashMap` (such as rayon),
46
+
the type aliases [HashMap], [HashSet] are provided. These alias the `std::HashMap` and `std::HashSet` using aHash as the hasher.
47
+
48
+
```
49
+
use ahash::{HashMap, HashMapExt};
50
+
51
+
let mut map: HashMap<i32, i32> = HashMap::new();
43
52
map.insert(12, 34);
44
-
let mut set = ahash::HashSet::with_capacity(10);
45
-
set.insert(10);
46
53
```
54
+
Note the import of [HashMapExt]. This is needed for the constructor.
55
+
56
+
# Directly hashing
57
+
58
+
Hashers can also be instantiated with `RandomState`. For example:
59
+
```
60
+
use std::hash::BuildHasher;
61
+
use ahash::RandomState;
62
+
63
+
let hash_builder = RandomState::with_seed(42);
64
+
let hash = hash_builder.hash_one("Some Data");
65
+
```
66
+
### Randomness
67
+
68
+
To ensure that each map has a unique set of keys aHash needs a source of randomness.
69
+
Normally this is just obtained from the OS. (Or via the `compile-time-rng` flag)
70
+
71
+
If for some reason (such as fuzzing) an application wishes to supply all random seeds manually, this can be done via:
Copy file name to clipboardExpand all lines: src/random_state.rs
+106-8Lines changed: 106 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -118,6 +118,12 @@ cfg_if::cfg_if! {
118
118
}
119
119
/// A supplier of Randomness used for different hashers.
120
120
/// See [set_random_source].
121
+
///
122
+
/// If [set_random_source] aHash will default to the best available source of randomness.
123
+
/// In order this is:
124
+
/// 1. OS provided random number generator (available if the `runtime-rng` flag is enabled which it is by default)
125
+
/// 2. Strong compile time random numbers used to permute a static "counter". (available if `compile-time-rng` is enabled. __Enabling this is recommended if `runtime-rng` is not possible__)
126
+
/// 3. A static counter that adds the memory address of each [RandomState] created permuted with fixed constants. (Similar to above but with fixed keys)
121
127
pubtraitRandomSource{
122
128
fngen_hasher_seed(&self) -> usize;
123
129
}
@@ -195,6 +201,16 @@ cfg_if::cfg_if! {
195
201
/// [Hasher]: std::hash::Hasher
196
202
/// [BuildHasher]: std::hash::BuildHasher
197
203
/// [HashMap]: std::collections::HashMap
204
+
///
205
+
/// There are multiple constructors each is documented in more detail below:
0 commit comments