sqlite_hashes/sha256.rs
1use sha2::Sha256;
2
3use crate::rusqlite::{Connection, Result};
4
5/// Register the `sha256` SQL function with the given `SQLite` connection.
6/// The function takes a single argument and returns the [SHA256 hash](https://en.wikipedia.org/wiki/SHA-2) (blob) of that argument.
7/// The argument can be either a string or a blob.
8/// If the argument is `NULL`, the result is `NULL`.
9///
10/// # Example
11///
12/// ```
13/// # use sqlite_hashes::rusqlite::{Connection, Result};
14/// # use sqlite_hashes::register_sha256_functions;
15/// # fn main() -> Result<()> {
16/// let db = Connection::open_in_memory()?;
17/// register_sha256_functions(&db)?;
18/// let hash: Vec<u8> = db.query_row("SELECT sha256('hello')", [], |r| r.get(0))?;
19/// let expected = b"\x2c\xf2\x4d\xba\x5f\xb0\xa3\x0e\x26\xe8\x3b\x2a\xc5\xb9\xe2\x9e\x1b\x16\x1e\x5c\x1f\xa7\x42\x5e\x73\x04\x33\x62\x93\x8b\x98\x24";
20/// assert_eq!(hash, expected);
21/// # Ok(())
22/// # }
23/// ```
24pub fn register_sha256_functions(conn: &Connection) -> Result<()> {
25 crate::scalar::create_hash_fn::<Sha256>(conn, "sha256")
26}