sqlite_hashes/lib.rs
1#![cfg_attr(feature = "default", doc = include_str!("../README.md"))]
2//
3// Unsafe code is required for cdylib, so only use it for this crate
4#![forbid(unsafe_code)]
5
6#[cfg(not(any(
7 feature = "md5",
8 feature = "sha1",
9 feature = "sha224",
10 feature = "sha256",
11 feature = "sha384",
12 feature = "sha512",
13 feature = "fnv",
14 feature = "xxhash",
15)))]
16compile_error!(
17 "At least one of these features must be enabled: md5,sha1,sha224,sha256,sha384,sha512,fnv,xxhash"
18);
19
20/// Re-export of the [`rusqlite`](https://crates.io/crates/rusqlite) crate to avoid version conflicts.
21pub use rusqlite;
22
23use crate::rusqlite::{Connection, Result};
24
25mod aggregate;
26
27mod scalar;
28pub use crate::scalar::NamedDigest;
29
30mod state;
31pub use crate::state::HashState;
32
33#[cfg(feature = "md5")]
34mod md5;
35
36#[cfg(feature = "md5")]
37pub use crate::md5::register_md5_functions;
38
39#[cfg(feature = "sha1")]
40mod sha1;
41
42#[cfg(feature = "sha1")]
43pub use crate::sha1::register_sha1_functions;
44
45#[cfg(feature = "sha224")]
46mod sha224;
47
48#[cfg(feature = "sha224")]
49pub use crate::sha224::register_sha224_functions;
50
51#[cfg(feature = "sha256")]
52mod sha256;
53
54#[cfg(feature = "sha256")]
55pub use crate::sha256::register_sha256_functions;
56
57#[cfg(feature = "sha384")]
58mod sha384;
59
60#[cfg(feature = "sha384")]
61pub use crate::sha384::register_sha384_functions;
62
63#[cfg(feature = "sha512")]
64mod sha512;
65
66#[cfg(feature = "sha512")]
67pub use crate::sha512::register_sha512_functions;
68
69#[cfg(feature = "fnv")]
70mod fnv;
71
72#[cfg(feature = "fnv")]
73pub use crate::fnv::register_fnv_functions;
74
75#[cfg(feature = "xxhash")]
76mod xxhash;
77
78#[cfg(feature = "xxhash")]
79pub use crate::xxhash::register_xxhash_functions;
80
81/// Register all hashing functions for the given `SQLite` connection.
82/// This is a convenience function that calls all of the `register_*_function` functions.
83/// Features must be enabled for the corresponding functions to be registered.
84///
85/// # Example
86///
87/// ```
88/// # use sqlite_hashes::rusqlite::{Connection, Result};
89/// # use sqlite_hashes::register_hash_functions;
90/// # fn main() -> Result<()> {
91/// let db = Connection::open_in_memory()?;
92/// register_hash_functions(&db)?;
93/// # if cfg!(all(feature = "hex", feature = "md5")) {
94/// let hash: String = db.query_row("SELECT md5_hex('hello')", [], |r| r.get(0))?;
95/// assert_eq!(&hash, "5D41402ABC4B2A76B9719D911017C592");
96/// # }
97/// # if cfg!(all(feature = "hex", feature = "sha1")) {
98/// let hash: String = db.query_row("SELECT sha1_hex('hello')", [], |r| r.get(0))?;
99/// assert_eq!(hash, "AAF4C61DDCC5E8A2DABEDE0F3B482CD9AEA9434D");
100/// # }
101/// # if cfg!(all(feature = "hex", feature = "sha224")) {
102/// let hash: String = db.query_row("SELECT sha224_hex('hello')", [], |r| r.get(0))?;
103/// assert_eq!(&hash, "EA09AE9CC6768C50FCEE903ED054556E5BFC8347907F12598AA24193");
104/// # }
105/// # if cfg!(all(feature = "hex", feature = "sha256")) {
106/// let hash: String = db.query_row("SELECT sha256_hex('hello')", [], |r| r.get(0))?;
107/// assert_eq!(&hash, "2CF24DBA5FB0A30E26E83B2AC5B9E29E1B161E5C1FA7425E73043362938B9824");
108/// # }
109/// # if cfg!(all(feature = "hex", feature = "sha384")) {
110/// let hash: String = db.query_row("SELECT sha384_hex('hello')", [], |r| r.get(0))?;
111/// assert_eq!(&hash, "59E1748777448C69DE6B800D7A33BBFB9FF1B463E44354C3553BCDB9C666FA90125A3C79F90397BDF5F6A13DE828684F");
112/// # }
113/// # if cfg!(all(feature = "hex", feature = "sha512")) {
114/// let hash: String = db.query_row("SELECT sha512_hex('hello')", [], |r| r.get(0))?;
115/// assert_eq!(hash, "9B71D224BD62F3785D96D46AD3EA3D73319BFBC2890CAADAE2DFF72519673CA72323C3D99BA5C11D7C7ACC6E14B8C5DA0C4663475C2E5C3ADEF46F73BCDEC043");
116/// # }
117/// # if cfg!(all(feature = "hex", feature = "fnv")) {
118/// let hash: String = db.query_row("SELECT fnv1a_hex('hello')", [], |r| r.get(0))?;
119/// assert_eq!(hash, "A430D84680AABD0B");
120/// # }
121/// # if cfg!(all(feature = "hex", feature = "xxhash")) {
122/// let hash: String = db.query_row("SELECT xxh32_hex('hello')", [], |r| r.get(0))?;
123/// assert_eq!(hash, "FB0077F9");
124/// let hash: String = db.query_row("SELECT xxh64_hex('hello')", [], |r| r.get(0))?;
125/// assert_eq!(hash, "26C7827D889F6DA3");
126/// let hash: String = db.query_row("SELECT xxh3_64_hex('hello')", [], |r| r.get(0))?;
127/// assert_eq!(hash, "9555E8555C62DCFD");
128/// let hash: String = db.query_row("SELECT xxh3_128_hex('hello')", [], |r| r.get(0))?;
129/// assert_eq!(hash, "B5E9C1AD071B3E7FC779CFAA5E523818");
130/// # }
131/// # Ok(())
132/// # }
133/// ```
134pub fn register_hash_functions(conn: &Connection) -> Result<()> {
135 #[cfg(feature = "md5")]
136 register_md5_functions(conn)?;
137 #[cfg(feature = "sha1")]
138 register_sha1_functions(conn)?;
139 #[cfg(feature = "sha224")]
140 register_sha224_functions(conn)?;
141 #[cfg(feature = "sha256")]
142 register_sha256_functions(conn)?;
143 #[cfg(feature = "sha384")]
144 register_sha384_functions(conn)?;
145 #[cfg(feature = "sha512")]
146 register_sha512_functions(conn)?;
147 #[cfg(feature = "fnv")]
148 register_fnv_functions(conn)?;
149 #[cfg(feature = "xxhash")]
150 register_xxhash_functions(conn)?;
151
152 Ok(())
153}