sqlite_hashes/
blake3.rs

1use blake3::Hasher;
2
3use crate::rusqlite::{Connection, Result};
4use crate::scalar::create_hash_fn;
5
6/// Register the `blake3` SQL function with the given `SQLite` connection.
7/// The function takes a single argument and returns the [BLAKE3 hash](https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3) (blob) of that argument.
8/// The argument can be either a string or a blob.
9/// If the argument is `NULL`, the result is `NULL`.
10///
11/// # Example
12///
13/// ```
14/// # use sqlite_hashes::rusqlite::{Connection, Result};
15/// # use sqlite_hashes::register_blake3_functions;
16/// # fn main() -> Result<()> {
17/// let db = Connection::open_in_memory()?;
18/// register_blake3_functions(&db)?;
19/// let hash: Vec<u8> = db.query_row("SELECT blake3('hello')", [], |r| r.get(0))?;
20/// let expected = b"\xea\x8f\x16\x3d\xb3\x86\x82\x92\x5e\x44\x91\xc5\xe5\x8d\x4b\xb3\x50\x6e\xf8\xc1\x4e\xb7\x8a\x86\xe9\x08\xc5\x62\x4a\x67\x20\x0f";
21/// assert_eq!(hash, expected);
22/// # Ok(())
23/// # }
24/// ```
25pub fn register_blake3_functions(conn: &Connection) -> Result<()> {
26    create_hash_fn::<Hasher>(conn, "blake3")
27}