ctb_formats_checksum/
xxhash.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Thin wrapper around the xxHash fast non-cryptographic hash algorithms.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace crate prelude"
26)]
27pub(crate) use ctb_utilities::*;
28
29use twox_hash::{XxHash3_64, XxHash3_128, XxHash32, XxHash64};
30
31/// Thin wrapper for xxhash32. Returns the 4-byte hash.
32pub fn xxhash32(data: &[u8]) -> [u8; 4] {
33    XxHash32::oneshot(0, data).to_be_bytes()
34}
35
36/// Thin wrapper for xxhash64. Returns the 8-byte hash.
37pub fn xxhash64(data: &[u8]) -> [u8; 8] {
38    XxHash64::oneshot(0, data).to_be_bytes()
39}
40
41/// Thin wrapper for `xxhash3_64`. Returns the 8-byte hash.
42pub fn xxhash3_64(data: &[u8]) -> [u8; 8] {
43    XxHash3_64::oneshot(data).to_be_bytes()
44}
45
46/// Thin wrapper for `xxhash3_128`. Returns the 16-byte hash.
47pub fn xxhash3_128(data: &[u8]) -> [u8; 16] {
48    XxHash3_128::oneshot(data).to_be_bytes()
49}
50
51#[cfg(test)]
52#[allow(
53    clippy::panic,
54    clippy::expect_used,
55    clippy::unwrap_used,
56    clippy::unwrap_in_result,
57    clippy::panic_in_result_fn,
58    clippy::indexing_slicing,
59    clippy::arithmetic_side_effects,
60    reason = "Standard repository test boilerplate"
61)]
62mod tests {
63    use super::*;
64
65    #[crate::ctb_test]
66    fn test_xxhash32() {
67        assert_eq!(xxhash32(&[]), [0x02, 0xcc, 0x5d, 0x05]);
68        assert_eq!(xxhash32(b"hello world"), [0xce, 0xbb, 0x66, 0x22]);
69    }
70
71    #[crate::ctb_test]
72    fn test_xxhash64() {
73        assert_eq!(
74            xxhash64(&[]),
75            [0xef, 0x46, 0xdb, 0x37, 0x51, 0xd8, 0xe9, 0x99]
76        );
77        assert_eq!(
78            xxhash64(b"hello world"),
79            [0x45, 0xab, 0x67, 0x34, 0xb2, 0x1e, 0x69, 0x68]
80        );
81    }
82
83    #[crate::ctb_test]
84    fn test_xxhash3_64() {
85        assert_eq!(
86            xxhash3_64(&[]),
87            [0x2d, 0x06, 0x80, 0x05, 0x38, 0xd3, 0x94, 0xc2]
88        );
89        assert_eq!(
90            xxhash3_64(b"hello world"),
91            [0xd4, 0x47, 0xb1, 0xea, 0x40, 0xe6, 0x98, 0x8b]
92        );
93    }
94
95    #[crate::ctb_test]
96    fn test_xxhash3_128() {
97        assert_eq!(
98            xxhash3_128(&[]),
99            [
100                0x99, 0xaa, 0x06, 0xd3, 0x01, 0x47, 0x98, 0xd8, 0x60, 0x01,
101                0xc3, 0x24, 0x46, 0x8d, 0x49, 0x7f
102            ]
103        );
104        assert_eq!(
105            xxhash3_128(b"hello world"),
106            [
107                0xdf, 0x8d, 0x09, 0xe9, 0x3f, 0x87, 0x49, 0x00, 0xa9, 0x9b,
108                0x87, 0x75, 0xcc, 0x15, 0xb6, 0xc7
109            ]
110        );
111    }
112}