├── .github └── workflows │ └── rust.yml ├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md └── lib.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - name: Build 18 | run: cargo build --verbose 19 | - name: Run tests 20 | run: cargo test --verbose 21 | 22 | msrv: 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: actions/checkout@v4 26 | - name: Read crate metadata 27 | id: metadata 28 | run: echo "rust-version=$(sed -ne 's/rust-version *= *\"\(.*\)\"/\1/p' Cargo.toml)" >> $GITHUB_OUTPUT 29 | - name: Install Rust 30 | uses: dtolnay/rust-toolchain@stable 31 | with: 32 | toolchain: ${{ steps.metadata.outputs.rust-version }} 33 | - run: cargo test --verbose 34 | 35 | fmt: 36 | runs-on: ubuntu-latest 37 | steps: 38 | - uses: actions/checkout@v4 39 | - name: Build 40 | run: cargo fmt --check 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - nightly 4 | - beta 5 | - stable 6 | script: 7 | - cargo test --verbose --all # default features 8 | - cargo test --verbose --all --no-default-features # excludes std 9 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "fnv" 3 | version = "1.0.7" 4 | authors = ["Alex Crichton "] 5 | description = "Fowler–Noll–Vo hash function" 6 | license = "Apache-2.0 OR MIT" 7 | readme = "README.md" 8 | repository = "https://github.com/servo/rust-fnv" 9 | documentation = "https://doc.servo.org/fnv/" 10 | categories = ["algorithms", "data-structures"] 11 | edition = "2021" 12 | rust-version = "1.56.1" 13 | 14 | [lib] 15 | name = "fnv" 16 | path = "lib.rs" 17 | 18 | [features] 19 | default = ["std"] 20 | std = [] 21 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Contributors 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rust-fnv 2 | 3 | An implementation of the [Fowler–Noll–Vo hash function][chongo]. 4 | 5 | ### [Read the documentation](https://doc.servo.org/fnv/) 6 | 7 | 8 | ## About 9 | 10 | The FNV hash function is a custom `Hasher` implementation that is more 11 | efficient for smaller hash keys. 12 | 13 | [The Rust Standard Library documentation][docs] states that while the default 14 | `Hasher` implementation, SipHash, is good in many cases, it is notably slower 15 | than other algorithms with short keys, such as when you have a map of integers 16 | to other values. In cases like these, [FNV is demonstrably faster][graphs]. 17 | 18 | Its disadvantages are that it performs badly on larger inputs, and 19 | provides no protection against collision attacks, where a malicious user 20 | can craft specific keys designed to slow a hasher down. Thus, it is 21 | important to profile your program to ensure that you are using small hash 22 | keys, and be certain that your program could not be exposed to malicious 23 | inputs (including being a networked server). 24 | 25 | The Rust compiler itself uses FNV, as it is not worried about 26 | denial-of-service attacks, and can assume that its inputs are going to be 27 | small—a perfect use case for FNV. 28 | 29 | 30 | ## Usage 31 | 32 | To include this crate in your program, add the following to your `Cargo.toml`: 33 | 34 | ```toml 35 | [dependencies] 36 | fnv = "1.0.3" 37 | ``` 38 | 39 | 40 | ## Using FNV in a HashMap 41 | 42 | The `FnvHashMap` type alias is the easiest way to use the standard library’s 43 | `HashMap` with FNV. 44 | 45 | ```rust 46 | use fnv::FnvHashMap; 47 | 48 | let mut map = FnvHashMap::default(); 49 | map.insert(1, "one"); 50 | map.insert(2, "two"); 51 | 52 | map = FnvHashMap::with_capacity_and_hasher(10, Default::default()); 53 | map.insert(1, "one"); 54 | map.insert(2, "two"); 55 | ``` 56 | 57 | Note, the standard library’s `HashMap::new` and `HashMap::with_capacity` 58 | are only implemented for the `RandomState` hasher, so using `Default` to 59 | get the hasher is the next best option. 60 | 61 | 62 | ## Using FNV in a HashSet 63 | 64 | Similarly, `FnvHashSet` is a type alias for the standard library’s `HashSet` 65 | with FNV. 66 | 67 | ```rust 68 | use fnv::FnvHashSet; 69 | 70 | let mut set = FnvHashSet::default(); 71 | set.insert(1); 72 | set.insert(2); 73 | 74 | set = FnvHashSet::with_capacity_and_hasher(10, Default::default()); 75 | set.insert(1); 76 | set.insert(2); 77 | ``` 78 | 79 | [chongo]: http://www.isthe.com/chongo/tech/comp/fnv/index.html 80 | [docs]: https://doc.rust-lang.org/std/collections/struct.HashMap.html 81 | [graphs]: https://cglab.ca/~abeinges/blah/hash-rs/ 82 | -------------------------------------------------------------------------------- /lib.rs: -------------------------------------------------------------------------------- 1 | //! An implementation of the [Fowler–Noll–Vo hash function][chongo]. 2 | //! 3 | //! ## About 4 | //! 5 | //! The FNV hash function is a custom `Hasher` implementation that is more 6 | //! efficient for smaller hash keys. 7 | //! 8 | //! [The Rust Standard Library documentation][docs] states that while the default 9 | //! `Hasher` implementation, SipHash, is good in many cases, it is notably slower 10 | //! than other algorithms with short keys, such as when you have a map of integers 11 | //! to other values. In cases like these, [FNV is demonstrably faster][graphs]. 12 | //! 13 | //! Its disadvantages are that it performs badly on larger inputs, and 14 | //! provides no protection against collision attacks, where a malicious user 15 | //! can craft specific keys designed to slow a hasher down. Thus, it is 16 | //! important to profile your program to ensure that you are using small hash 17 | //! keys, and be certain that your program could not be exposed to malicious 18 | //! inputs (including being a networked server). 19 | //! 20 | //! The Rust compiler itself uses FNV, as it is not worried about 21 | //! denial-of-service attacks, and can assume that its inputs are going to be 22 | //! small—a perfect use case for FNV. 23 | //! 24 | #![cfg_attr( 25 | feature = "std", 26 | doc = r#" 27 | 28 | ## Using FNV in a `HashMap` 29 | 30 | The `FnvHashMap` type alias is the easiest way to use the standard library’s 31 | `HashMap` with FNV. 32 | 33 | ```rust 34 | use fnv::FnvHashMap; 35 | 36 | let mut map = FnvHashMap::default(); 37 | map.insert(1, "one"); 38 | map.insert(2, "two"); 39 | 40 | map = FnvHashMap::with_capacity_and_hasher(10, Default::default()); 41 | map.insert(1, "one"); 42 | map.insert(2, "two"); 43 | ``` 44 | 45 | Note, the standard library’s `HashMap::new` and `HashMap::with_capacity` 46 | are only implemented for the `RandomState` hasher, so using `Default` to 47 | get the hasher is the next best option. 48 | 49 | ## Using FNV in a `HashSet` 50 | 51 | Similarly, `FnvHashSet` is a type alias for the standard library’s `HashSet` 52 | with FNV. 53 | 54 | ```rust 55 | use fnv::FnvHashSet; 56 | 57 | let mut set = FnvHashSet::default(); 58 | set.insert(1); 59 | set.insert(2); 60 | 61 | set = FnvHashSet::with_capacity_and_hasher(10, Default::default()); 62 | set.insert(1); 63 | set.insert(2); 64 | ``` 65 | "# 66 | )] 67 | //! 68 | //! [chongo]: http://www.isthe.com/chongo/tech/comp/fnv/index.html 69 | //! [docs]: https://doc.rust-lang.org/std/collections/struct.HashMap.html 70 | //! [graphs]: https://cglab.ca/~abeinges/blah/hash-rs/ 71 | 72 | #![cfg_attr(not(feature = "std"), no_std)] 73 | 74 | #[cfg(all(not(feature = "std"), test))] 75 | extern crate alloc; 76 | 77 | #[cfg(not(feature = "std"))] 78 | use core::default::Default; 79 | #[cfg(not(feature = "std"))] 80 | use core::hash::{BuildHasherDefault, Hasher}; 81 | #[cfg(feature = "std")] 82 | use std::collections::{HashMap, HashSet}; 83 | #[cfg(feature = "std")] 84 | use std::default::Default; 85 | #[cfg(feature = "std")] 86 | use std::hash::{BuildHasherDefault, Hasher}; 87 | 88 | const INITIAL_STATE: u64 = 0xcbf2_9ce4_8422_2325; 89 | const PRIME: u64 = 0x0100_0000_01b3; 90 | 91 | /// An implementation of the Fowler–Noll–Vo hash function. 92 | /// 93 | /// See the [crate documentation](index.html) for more details. 94 | #[allow(missing_copy_implementations)] 95 | #[derive(Clone)] 96 | pub struct FnvHasher(u64); 97 | 98 | impl Default for FnvHasher { 99 | #[inline] 100 | fn default() -> FnvHasher { 101 | FnvHasher(INITIAL_STATE) 102 | } 103 | } 104 | 105 | impl FnvHasher { 106 | /// Create an FNV hasher starting with a state corresponding 107 | /// to the hash `key`. 108 | #[inline] 109 | #[must_use] 110 | pub fn with_key(key: u64) -> FnvHasher { 111 | FnvHasher(key) 112 | } 113 | } 114 | 115 | impl Hasher for FnvHasher { 116 | #[inline] 117 | fn finish(&self) -> u64 { 118 | self.0 119 | } 120 | 121 | #[inline] 122 | fn write(&mut self, bytes: &[u8]) { 123 | let FnvHasher(mut hash) = *self; 124 | 125 | for byte in bytes { 126 | hash ^= u64::from(*byte); 127 | hash = hash.wrapping_mul(PRIME); 128 | } 129 | 130 | *self = FnvHasher(hash); 131 | } 132 | } 133 | 134 | /// A builder for default FNV hashers. 135 | pub type FnvBuildHasher = BuildHasherDefault; 136 | 137 | /// A `HashMap` using a default FNV hasher. 138 | #[cfg(feature = "std")] 139 | pub type FnvHashMap = HashMap; 140 | 141 | /// A `HashSet` using a default FNV hasher. 142 | #[cfg(feature = "std")] 143 | pub type FnvHashSet = HashSet; 144 | 145 | /// Const version of FNV hash. 146 | #[inline] 147 | #[must_use] 148 | pub const fn fnv_hash(bytes: &[u8]) -> u64 { 149 | let mut hash = INITIAL_STATE; 150 | let mut i = 0; 151 | while i < bytes.len() { 152 | hash ^= bytes[i] as u64; 153 | hash = hash.wrapping_mul(PRIME); 154 | i += 1; 155 | } 156 | hash 157 | } 158 | 159 | #[cfg(test)] 160 | mod test { 161 | use super::*; 162 | #[cfg(not(feature = "std"))] 163 | use alloc::vec::Vec; 164 | #[cfg(feature = "std")] 165 | use std::hash::Hasher; 166 | 167 | fn fnv1a(bytes: &[u8]) -> u64 { 168 | let mut hasher = FnvHasher::default(); 169 | hasher.write(bytes); 170 | hasher.finish() 171 | } 172 | 173 | fn repeat_10(bytes: &[u8]) -> Vec { 174 | (0..10).flat_map(|_| bytes.iter().copied()).collect() 175 | } 176 | 177 | fn repeat_500(bytes: &[u8]) -> Vec { 178 | (0..500).flat_map(|_| bytes.iter().copied()).collect() 179 | } 180 | 181 | #[test] 182 | #[allow(clippy::unreadable_literal, clippy::too_many_lines)] 183 | fn basic_tests() { 184 | assert_eq!(fnv1a(b""), 0xcbf29ce484222325); 185 | assert_eq!(fnv1a(b"a"), 0xaf63dc4c8601ec8c); 186 | assert_eq!(fnv1a(b"b"), 0xaf63df4c8601f1a5); 187 | assert_eq!(fnv1a(b"c"), 0xaf63de4c8601eff2); 188 | assert_eq!(fnv1a(b"d"), 0xaf63d94c8601e773); 189 | assert_eq!(fnv1a(b"e"), 0xaf63d84c8601e5c0); 190 | assert_eq!(fnv1a(b"f"), 0xaf63db4c8601ead9); 191 | assert_eq!(fnv1a(b"fo"), 0x08985907b541d342); 192 | assert_eq!(fnv1a(b"foo"), 0xdcb27518fed9d577); 193 | assert_eq!(fnv1a(b"foob"), 0xdd120e790c2512af); 194 | assert_eq!(fnv1a(b"fooba"), 0xcac165afa2fef40a); 195 | assert_eq!(fnv1a(b"foobar"), 0x85944171f73967e8); 196 | assert_eq!(fnv1a(b"\0"), 0xaf63bd4c8601b7df); 197 | assert_eq!(fnv1a(b"a\0"), 0x089be207b544f1e4); 198 | assert_eq!(fnv1a(b"b\0"), 0x08a61407b54d9b5f); 199 | assert_eq!(fnv1a(b"c\0"), 0x08a2ae07b54ab836); 200 | assert_eq!(fnv1a(b"d\0"), 0x0891b007b53c4869); 201 | assert_eq!(fnv1a(b"e\0"), 0x088e4a07b5396540); 202 | assert_eq!(fnv1a(b"f\0"), 0x08987c07b5420ebb); 203 | assert_eq!(fnv1a(b"fo\0"), 0xdcb28a18fed9f926); 204 | assert_eq!(fnv1a(b"foo\0"), 0xdd1270790c25b935); 205 | assert_eq!(fnv1a(b"foob\0"), 0xcac146afa2febf5d); 206 | assert_eq!(fnv1a(b"fooba\0"), 0x8593d371f738acfe); 207 | assert_eq!(fnv1a(b"foobar\0"), 0x34531ca7168b8f38); 208 | assert_eq!(fnv1a(b"ch"), 0x08a25607b54a22ae); 209 | assert_eq!(fnv1a(b"cho"), 0xf5faf0190cf90df3); 210 | assert_eq!(fnv1a(b"chon"), 0xf27397910b3221c7); 211 | assert_eq!(fnv1a(b"chong"), 0x2c8c2b76062f22e0); 212 | assert_eq!(fnv1a(b"chongo"), 0xe150688c8217b8fd); 213 | assert_eq!(fnv1a(b"chongo "), 0xf35a83c10e4f1f87); 214 | assert_eq!(fnv1a(b"chongo w"), 0xd1edd10b507344d0); 215 | assert_eq!(fnv1a(b"chongo wa"), 0x2a5ee739b3ddb8c3); 216 | assert_eq!(fnv1a(b"chongo was"), 0xdcfb970ca1c0d310); 217 | assert_eq!(fnv1a(b"chongo was "), 0x4054da76daa6da90); 218 | assert_eq!(fnv1a(b"chongo was h"), 0xf70a2ff589861368); 219 | assert_eq!(fnv1a(b"chongo was he"), 0x4c628b38aed25f17); 220 | assert_eq!(fnv1a(b"chongo was her"), 0x9dd1f6510f78189f); 221 | assert_eq!(fnv1a(b"chongo was here"), 0xa3de85bd491270ce); 222 | assert_eq!(fnv1a(b"chongo was here!"), 0x858e2fa32a55e61d); 223 | assert_eq!(fnv1a(b"chongo was here!\n"), 0x46810940eff5f915); 224 | assert_eq!(fnv1a(b"ch\0"), 0xf5fadd190cf8edaa); 225 | assert_eq!(fnv1a(b"cho\0"), 0xf273ed910b32b3e9); 226 | assert_eq!(fnv1a(b"chon\0"), 0x2c8c5276062f6525); 227 | assert_eq!(fnv1a(b"chong\0"), 0xe150b98c821842a0); 228 | assert_eq!(fnv1a(b"chongo\0"), 0xf35aa3c10e4f55e7); 229 | assert_eq!(fnv1a(b"chongo \0"), 0xd1ed680b50729265); 230 | assert_eq!(fnv1a(b"chongo w\0"), 0x2a5f0639b3dded70); 231 | assert_eq!(fnv1a(b"chongo wa\0"), 0xdcfbaa0ca1c0f359); 232 | assert_eq!(fnv1a(b"chongo was\0"), 0x4054ba76daa6a430); 233 | assert_eq!(fnv1a(b"chongo was \0"), 0xf709c7f5898562b0); 234 | assert_eq!(fnv1a(b"chongo was h\0"), 0x4c62e638aed2f9b8); 235 | assert_eq!(fnv1a(b"chongo was he\0"), 0x9dd1a8510f779415); 236 | assert_eq!(fnv1a(b"chongo was her\0"), 0xa3de2abd4911d62d); 237 | assert_eq!(fnv1a(b"chongo was here\0"), 0x858e0ea32a55ae0a); 238 | assert_eq!(fnv1a(b"chongo was here!\0"), 0x46810f40eff60347); 239 | assert_eq!(fnv1a(b"chongo was here!\n\0"), 0xc33bce57bef63eaf); 240 | assert_eq!(fnv1a(b"cu"), 0x08a24307b54a0265); 241 | assert_eq!(fnv1a(b"cur"), 0xf5b9fd190cc18d15); 242 | assert_eq!(fnv1a(b"curd"), 0x4c968290ace35703); 243 | assert_eq!(fnv1a(b"curds"), 0x07174bd5c64d9350); 244 | assert_eq!(fnv1a(b"curds "), 0x5a294c3ff5d18750); 245 | assert_eq!(fnv1a(b"curds a"), 0x05b3c1aeb308b843); 246 | assert_eq!(fnv1a(b"curds an"), 0xb92a48da37d0f477); 247 | assert_eq!(fnv1a(b"curds and"), 0x73cdddccd80ebc49); 248 | assert_eq!(fnv1a(b"curds and "), 0xd58c4c13210a266b); 249 | assert_eq!(fnv1a(b"curds and w"), 0xe78b6081243ec194); 250 | assert_eq!(fnv1a(b"curds and wh"), 0xb096f77096a39f34); 251 | assert_eq!(fnv1a(b"curds and whe"), 0xb425c54ff807b6a3); 252 | assert_eq!(fnv1a(b"curds and whey"), 0x23e520e2751bb46e); 253 | assert_eq!(fnv1a(b"curds and whey\n"), 0x1a0b44ccfe1385ec); 254 | assert_eq!(fnv1a(b"cu\0"), 0xf5ba4b190cc2119f); 255 | assert_eq!(fnv1a(b"cur\0"), 0x4c962690ace2baaf); 256 | assert_eq!(fnv1a(b"curd\0"), 0x0716ded5c64cda19); 257 | assert_eq!(fnv1a(b"curds\0"), 0x5a292c3ff5d150f0); 258 | assert_eq!(fnv1a(b"curds \0"), 0x05b3e0aeb308ecf0); 259 | assert_eq!(fnv1a(b"curds a\0"), 0xb92a5eda37d119d9); 260 | assert_eq!(fnv1a(b"curds an\0"), 0x73ce41ccd80f6635); 261 | assert_eq!(fnv1a(b"curds and\0"), 0xd58c2c132109f00b); 262 | assert_eq!(fnv1a(b"curds and \0"), 0xe78baf81243f47d1); 263 | assert_eq!(fnv1a(b"curds and w\0"), 0xb0968f7096a2ee7c); 264 | assert_eq!(fnv1a(b"curds and wh\0"), 0xb425a84ff807855c); 265 | assert_eq!(fnv1a(b"curds and whe\0"), 0x23e4e9e2751b56f9); 266 | assert_eq!(fnv1a(b"curds and whey\0"), 0x1a0b4eccfe1396ea); 267 | assert_eq!(fnv1a(b"curds and whey\n\0"), 0x54abd453bb2c9004); 268 | assert_eq!(fnv1a(b"hi"), 0x08ba5f07b55ec3da); 269 | assert_eq!(fnv1a(b"hi\0"), 0x337354193006cb6e); 270 | assert_eq!(fnv1a(b"hello"), 0xa430d84680aabd0b); 271 | assert_eq!(fnv1a(b"hello\0"), 0xa9bc8acca21f39b1); 272 | assert_eq!(fnv1a(b"\xff\x00\x00\x01"), 0x6961196491cc682d); 273 | assert_eq!(fnv1a(b"\x01\x00\x00\xff"), 0xad2bb1774799dfe9); 274 | assert_eq!(fnv1a(b"\xff\x00\x00\x02"), 0x6961166491cc6314); 275 | assert_eq!(fnv1a(b"\x02\x00\x00\xff"), 0x8d1bb3904a3b1236); 276 | assert_eq!(fnv1a(b"\xff\x00\x00\x03"), 0x6961176491cc64c7); 277 | assert_eq!(fnv1a(b"\x03\x00\x00\xff"), 0xed205d87f40434c7); 278 | assert_eq!(fnv1a(b"\xff\x00\x00\x04"), 0x6961146491cc5fae); 279 | assert_eq!(fnv1a(b"\x04\x00\x00\xff"), 0xcd3baf5e44f8ad9c); 280 | assert_eq!(fnv1a(b"\x40\x51\x4e\x44"), 0xe3b36596127cd6d8); 281 | assert_eq!(fnv1a(b"\x44\x4e\x51\x40"), 0xf77f1072c8e8a646); 282 | assert_eq!(fnv1a(b"\x40\x51\x4e\x4a"), 0xe3b36396127cd372); 283 | assert_eq!(fnv1a(b"\x4a\x4e\x51\x40"), 0x6067dce9932ad458); 284 | assert_eq!(fnv1a(b"\x40\x51\x4e\x54"), 0xe3b37596127cf208); 285 | assert_eq!(fnv1a(b"\x54\x4e\x51\x40"), 0x4b7b10fa9fe83936); 286 | assert_eq!(fnv1a(b"127.0.0.1"), 0xaabafe7104d914be); 287 | assert_eq!(fnv1a(b"127.0.0.1\0"), 0xf4d3180b3cde3eda); 288 | assert_eq!(fnv1a(b"127.0.0.2"), 0xaabafd7104d9130b); 289 | assert_eq!(fnv1a(b"127.0.0.2\0"), 0xf4cfb20b3cdb5bb1); 290 | assert_eq!(fnv1a(b"127.0.0.3"), 0xaabafc7104d91158); 291 | assert_eq!(fnv1a(b"127.0.0.3\0"), 0xf4cc4c0b3cd87888); 292 | assert_eq!(fnv1a(b"64.81.78.68"), 0xe729bac5d2a8d3a7); 293 | assert_eq!(fnv1a(b"64.81.78.68\0"), 0x74bc0524f4dfa4c5); 294 | assert_eq!(fnv1a(b"64.81.78.74"), 0xe72630c5d2a5b352); 295 | assert_eq!(fnv1a(b"64.81.78.74\0"), 0x6b983224ef8fb456); 296 | assert_eq!(fnv1a(b"64.81.78.84"), 0xe73042c5d2ae266d); 297 | assert_eq!(fnv1a(b"64.81.78.84\0"), 0x8527e324fdeb4b37); 298 | assert_eq!(fnv1a(b"feedface"), 0x0a83c86fee952abc); 299 | assert_eq!(fnv1a(b"feedface\0"), 0x7318523267779d74); 300 | assert_eq!(fnv1a(b"feedfacedaffdeed"), 0x3e66d3d56b8caca1); 301 | assert_eq!(fnv1a(b"feedfacedaffdeed\0"), 0x956694a5c0095593); 302 | assert_eq!(fnv1a(b"feedfacedeadbeef"), 0xcac54572bb1a6fc8); 303 | assert_eq!(fnv1a(b"feedfacedeadbeef\0"), 0xa7a4c9f3edebf0d8); 304 | assert_eq!(fnv1a(b"line 1\nline 2\nline 3"), 0x7829851fac17b143); 305 | assert_eq!( 306 | fnv1a(b"chongo /\\../\\"), 307 | 0x2c8f4c9af81bcf06 308 | ); 309 | assert_eq!( 310 | fnv1a(b"chongo /\\../\\\0"), 311 | 0xd34e31539740c732 312 | ); 313 | assert_eq!( 314 | fnv1a(b"chongo (Landon Curt Noll) /\\../\\"), 315 | 0x3605a2ac253d2db1 316 | ); 317 | assert_eq!( 318 | fnv1a(b"chongo (Landon Curt Noll) /\\../\\\0"), 319 | 0x08c11b8346f4a3c3 320 | ); 321 | assert_eq!( 322 | fnv1a(b"http://antwrp.gsfc.nasa.gov/apod/astropix.html"), 323 | 0x6be396289ce8a6da 324 | ); 325 | assert_eq!( 326 | fnv1a(b"http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash"), 327 | 0xd9b957fb7fe794c5 328 | ); 329 | assert_eq!(fnv1a(b"http://epod.usra.edu/"), 0x05be33da04560a93); 330 | assert_eq!(fnv1a(b"http://exoplanet.eu/"), 0x0957f1577ba9747c); 331 | assert_eq!(fnv1a(b"http://hvo.wr.usgs.gov/cam3/"), 0xda2cc3acc24fba57); 332 | assert_eq!( 333 | fnv1a(b"http://hvo.wr.usgs.gov/cams/HMcam/"), 334 | 0x74136f185b29e7f0 335 | ); 336 | assert_eq!( 337 | fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/deformation.html"), 338 | 0xb2f2b4590edb93b2 339 | ); 340 | assert_eq!( 341 | fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/images.html"), 342 | 0xb3608fce8b86ae04 343 | ); 344 | assert_eq!( 345 | fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/maps.html"), 346 | 0x4a3a865079359063 347 | ); 348 | assert_eq!( 349 | fnv1a(b"http://hvo.wr.usgs.gov/volcanowatch/current_issue.html"), 350 | 0x5b3a7ef496880a50 351 | ); 352 | assert_eq!(fnv1a(b"http://neo.jpl.nasa.gov/risk/"), 0x48fae3163854c23b); 353 | assert_eq!(fnv1a(b"http://norvig.com/21-days.html"), 0x07aaa640476e0b9a); 354 | assert_eq!( 355 | fnv1a(b"http://primes.utm.edu/curios/home.php"), 356 | 0x2f653656383a687d 357 | ); 358 | assert_eq!(fnv1a(b"http://slashdot.org/"), 0xa1031f8e7599d79c); 359 | assert_eq!( 360 | fnv1a(b"http://tux.wr.usgs.gov/Maps/155.25-19.5.html"), 361 | 0xa31908178ff92477 362 | ); 363 | assert_eq!( 364 | fnv1a(b"http://volcano.wr.usgs.gov/kilaueastatus.php"), 365 | 0x097edf3c14c3fb83 366 | ); 367 | assert_eq!( 368 | fnv1a(b"http://www.avo.alaska.edu/activity/Redoubt.php"), 369 | 0xb51ca83feaa0971b 370 | ); 371 | assert_eq!(fnv1a(b"http://www.dilbert.com/fast/"), 0xdd3c0d96d784f2e9); 372 | assert_eq!( 373 | fnv1a(b"http://www.fourmilab.ch/gravitation/orbits/"), 374 | 0x86cd26a9ea767d78 375 | ); 376 | assert_eq!(fnv1a(b"http://www.fpoa.net/"), 0xe6b215ff54a30c18); 377 | assert_eq!( 378 | fnv1a(b"http://www.ioccc.org/index.html"), 379 | 0xec5b06a1c5531093 380 | ); 381 | assert_eq!( 382 | fnv1a(b"http://www.isthe.com/cgi-bin/number.cgi"), 383 | 0x45665a929f9ec5e5 384 | ); 385 | assert_eq!( 386 | fnv1a(b"http://www.isthe.com/chongo/bio.html"), 387 | 0x8c7609b4a9f10907 388 | ); 389 | assert_eq!( 390 | fnv1a(b"http://www.isthe.com/chongo/index.html"), 391 | 0x89aac3a491f0d729 392 | ); 393 | assert_eq!( 394 | fnv1a(b"http://www.isthe.com/chongo/src/calc/lucas-calc"), 395 | 0x32ce6b26e0f4a403 396 | ); 397 | assert_eq!( 398 | fnv1a(b"http://www.isthe.com/chongo/tech/astro/venus2004.html"), 399 | 0x614ab44e02b53e01 400 | ); 401 | assert_eq!( 402 | fnv1a(b"http://www.isthe.com/chongo/tech/astro/vita.html"), 403 | 0xfa6472eb6eef3290 404 | ); 405 | assert_eq!( 406 | fnv1a(b"http://www.isthe.com/chongo/tech/comp/c/expert.html"), 407 | 0x9e5d75eb1948eb6a 408 | ); 409 | assert_eq!( 410 | fnv1a(b"http://www.isthe.com/chongo/tech/comp/calc/index.html"), 411 | 0xb6d12ad4a8671852 412 | ); 413 | assert_eq!( 414 | fnv1a(b"http://www.isthe.com/chongo/tech/comp/fnv/index.html"), 415 | 0x88826f56eba07af1 416 | ); 417 | assert_eq!( 418 | fnv1a(b"http://www.isthe.com/chongo/tech/math/number/howhigh.html"), 419 | 0x44535bf2645bc0fd 420 | ); 421 | assert_eq!( 422 | fnv1a(b"http://www.isthe.com/chongo/tech/math/number/number.html"), 423 | 0x169388ffc21e3728 424 | ); 425 | assert_eq!( 426 | fnv1a(b"http://www.isthe.com/chongo/tech/math/prime/mersenne.html"), 427 | 0xf68aac9e396d8224 428 | ); 429 | assert_eq!( 430 | fnv1a(b"http://www.isthe.com/chongo/tech/math/prime/mersenne.html#largest"), 431 | 0x8e87d7e7472b3883 432 | ); 433 | assert_eq!( 434 | fnv1a(b"http://www.lavarnd.org/cgi-bin/corpspeak.cgi"), 435 | 0x295c26caa8b423de 436 | ); 437 | assert_eq!( 438 | fnv1a(b"http://www.lavarnd.org/cgi-bin/haiku.cgi"), 439 | 0x322c814292e72176 440 | ); 441 | assert_eq!( 442 | fnv1a(b"http://www.lavarnd.org/cgi-bin/rand-none.cgi"), 443 | 0x8a06550eb8af7268 444 | ); 445 | assert_eq!( 446 | fnv1a(b"http://www.lavarnd.org/cgi-bin/randdist.cgi"), 447 | 0xef86d60e661bcf71 448 | ); 449 | assert_eq!( 450 | fnv1a(b"http://www.lavarnd.org/index.html"), 451 | 0x9e5426c87f30ee54 452 | ); 453 | assert_eq!( 454 | fnv1a(b"http://www.lavarnd.org/what/nist-test.html"), 455 | 0xf1ea8aa826fd047e 456 | ); 457 | assert_eq!(fnv1a(b"http://www.macosxhints.com/"), 0x0babaf9a642cb769); 458 | assert_eq!(fnv1a(b"http://www.mellis.com/"), 0x4b3341d4068d012e); 459 | assert_eq!( 460 | fnv1a(b"http://www.nature.nps.gov/air/webcams/parks/havoso2alert/havoalert.cfm"), 461 | 0xd15605cbc30a335c 462 | ); 463 | assert_eq!( 464 | fnv1a(b"http://www.nature.nps.gov/air/webcams/parks/havoso2alert/timelines_24.cfm"), 465 | 0x5b21060aed8412e5 466 | ); 467 | assert_eq!(fnv1a(b"http://www.paulnoll.com/"), 0x45e2cda1ce6f4227); 468 | assert_eq!(fnv1a(b"http://www.pepysdiary.com/"), 0x50ae3745033ad7d4); 469 | assert_eq!( 470 | fnv1a(b"http://www.sciencenews.org/index/home/activity/view"), 471 | 0xaa4588ced46bf414 472 | ); 473 | assert_eq!( 474 | fnv1a(b"http://www.skyandtelescope.com/"), 475 | 0xc1b0056c4a95467e 476 | ); 477 | assert_eq!( 478 | fnv1a(b"http://www.sput.nl/~rob/sirius.html"), 479 | 0x56576a71de8b4089 480 | ); 481 | assert_eq!(fnv1a(b"http://www.systemexperts.com/"), 0xbf20965fa6dc927e); 482 | assert_eq!( 483 | fnv1a(b"http://www.tq-international.com/phpBB3/index.php"), 484 | 0x569f8383c2040882 485 | ); 486 | assert_eq!( 487 | fnv1a(b"http://www.travelquesttours.com/index.htm"), 488 | 0xe1e772fba08feca0 489 | ); 490 | assert_eq!( 491 | fnv1a(b"http://www.wunderground.com/global/stations/89606.html"), 492 | 0x4ced94af97138ac4 493 | ); 494 | assert_eq!(fnv1a(&repeat_10(b"21701")), 0xc4112ffb337a82fb); 495 | assert_eq!(fnv1a(&repeat_10(b"M21701")), 0xd64a4fd41de38b7d); 496 | assert_eq!(fnv1a(&repeat_10(b"2^21701-1")), 0x4cfc32329edebcbb); 497 | assert_eq!(fnv1a(&repeat_10(b"\x54\xc5")), 0x0803564445050395); 498 | assert_eq!(fnv1a(&repeat_10(b"\xc5\x54")), 0xaa1574ecf4642ffd); 499 | assert_eq!(fnv1a(&repeat_10(b"23209")), 0x694bc4e54cc315f9); 500 | assert_eq!(fnv1a(&repeat_10(b"M23209")), 0xa3d7cb273b011721); 501 | assert_eq!(fnv1a(&repeat_10(b"2^23209-1")), 0x577c2f8b6115bfa5); 502 | assert_eq!(fnv1a(&repeat_10(b"\x5a\xa9")), 0xb7ec8c1a769fb4c1); 503 | assert_eq!(fnv1a(&repeat_10(b"\xa9\x5a")), 0x5d5cfce63359ab19); 504 | assert_eq!(fnv1a(&repeat_10(b"391581216093")), 0x33b96c3cd65b5f71); 505 | assert_eq!(fnv1a(&repeat_10(b"391581*2^216093-1")), 0xd845097780602bb9); 506 | assert_eq!( 507 | fnv1a(&repeat_10(b"\x05\xf9\x9d\x03\x4c\x81")), 508 | 0x84d47645d02da3d5 509 | ); 510 | assert_eq!(fnv1a(&repeat_10(b"FEDCBA9876543210")), 0x83544f33b58773a5); 511 | assert_eq!( 512 | fnv1a(&repeat_10(b"\xfe\xdc\xba\x98\x76\x54\x32\x10")), 513 | 0x9175cbb2160836c5 514 | ); 515 | assert_eq!(fnv1a(&repeat_10(b"EFCDAB8967452301")), 0xc71b3bc175e72bc5); 516 | assert_eq!( 517 | fnv1a(&repeat_10(b"\xef\xcd\xab\x89\x67\x45\x23\x01")), 518 | 0x636806ac222ec985 519 | ); 520 | assert_eq!(fnv1a(&repeat_10(b"0123456789ABCDEF")), 0xb6ef0e6950f52ed5); 521 | assert_eq!( 522 | fnv1a(&repeat_10(b"\x01\x23\x45\x67\x89\xab\xcd\xef")), 523 | 0xead3d8a0f3dfdaa5 524 | ); 525 | assert_eq!(fnv1a(&repeat_10(b"1032547698BADCFE")), 0x922908fe9a861ba5); 526 | assert_eq!( 527 | fnv1a(&repeat_10(b"\x10\x32\x54\x76\x98\xba\xdc\xfe")), 528 | 0x6d4821de275fd5c5 529 | ); 530 | assert_eq!(fnv1a(&repeat_500(b"\x00")), 0x1fe3fce62bd816b5); 531 | assert_eq!(fnv1a(&repeat_500(b"\x07")), 0xc23e9fccd6f70591); 532 | assert_eq!(fnv1a(&repeat_500(b"~")), 0xc1af12bdfe16b5b5); 533 | assert_eq!(fnv1a(&repeat_500(b"\x7f")), 0x39e9f18f2f85e221); 534 | } 535 | 536 | #[test] 537 | fn fnv_hash_standalone() { 538 | assert_eq!(fnv_hash(b"hello world"), fnv1a(b"hello world")); 539 | } 540 | } 541 | --------------------------------------------------------------------------------