sqlite_hashes/
md5.rs

1use md5::Md5;
2
3use crate::rusqlite::{Connection, Result};
4use crate::scalar::create_hash_fn;
5
6/// Register the `md5` SQL function with the given `SQLite` connection.
7/// The function takes a single argument and returns the [MD5 hash](https://en.wikipedia.org/wiki/MD5) (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_md5_functions;
16/// # fn main() -> Result<()> {
17/// let db = Connection::open_in_memory()?;
18/// register_md5_functions(&db)?;
19/// let hash: Vec<u8> = db.query_row("SELECT md5('hello')", [], |r| r.get(0))?;
20/// let expected = b"\x5d\x41\x40\x2a\xbc\x4b\x2a\x76\xb9\x71\x9d\x91\x10\x17\xc5\x92";
21/// assert_eq!(hash, expected);
22/// # Ok(())
23/// # }
24/// ```
25pub fn register_md5_functions(conn: &Connection) -> Result<()> {
26    create_hash_fn::<Md5>(conn, "md5")
27}