Crate sqlite_hashes

source ·
Expand description

sqlite-hashes

GitHub crates.io version docs.rs docs crates.io version CI build

Use this crate to add various hash functions to SQLite, including MD5, SHA1, SHA256, and SHA512. All functions support text and blob values. There is also an aggregate functions that can be used on a set of values.

This crate uses rusqlite to add user-defined scalar functions using static linking. Eventually it would be good to build dynamically loadable extension binaries usable from other languages (PRs welcome).

Usage

use sqlite_hashes::{register_sha256_function, rusqlite::Connection};

fn main() {
  let db = Connection::open_in_memory().unwrap();
  register_sha256_function(&db).unwrap();

  let sql = "SELECT hex(sha256('password'))";
  let hash: String = db.query_row_and_then(&sql, [], |r| r.get(0)).unwrap();
  assert_eq!(hash, "5E884898DA28047151D0E56F8DC6292773603D0D6AABBDD62A11EF721D1542D8");

  // Iterate over a set of values and hash them.
  // Make sure the value order is consistent.
  // This example creates a sequence of ints from 1 to 9.
    let sql = "WITH RECURSIVE seq(value) AS (
    SELECT 1 UNION ALL SELECT value + 1 FROM seq LIMIT 9
  )
  SELECT hex(sha256_concat(cast(value as text)))
  FROM seq
  ORDER BY value";
  let hash: String = db.query_row_and_then(&sql, [], |r| r.get(0)).unwrap();
  assert_eq!(hash, "15E2B0D3C33891EBB0F1EF609EC419420C20E320CE94C65FBC8C3312448EB225");
  
  // The above is equivalent to a single value:
  let sql = "SELECT hex(sha256('123456789'))";
  let hash: String = db.query_row_and_then(&sql, [], |r| r.get(0)).unwrap();
  assert_eq!(hash, "15E2B0D3C33891EBB0F1EF609EC419420C20E320CE94C65FBC8C3312448EB225");
}

Features

By default, this crate will compile with all hash functions. You can enable just the ones you need to reduce compile time.

[dependencies]
sqlite-hashes = { version = "0.1", default-features = false, features = ["sha256"] }

Development

  • This project is easier to develop with just, a modern alternative to make. Install it with cargo install just.
  • To get a list of available commands, run just.
  • To run tests, use just test.
  • On git push, it will run a few validations, including cargo fmt, cargo clippy, and cargo test. Use git push --no-verify to skip these checks.

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Re-exports

Functions

  • Register the md5 SQL function with the given SQLite connection. The function takes a single argument and returns the MD5 hash (blob) of that argument. The argument can be either a string or a blob. If the argument is NULL, the result is NULL.
  • Register the sha1 SQL function with the given SQLite connection. The function takes a single argument and returns the SHA1 hash (blob) of that argument. The argument can be either a string or a blob. If the argument is NULL, the result is NULL.
  • Register the sha256 SQL function with the given SQLite connection. The function takes a single argument and returns the SHA256 hash (blob) of that argument. The argument can be either a string or a blob. If the argument is NULL, the result is NULL.
  • Register the sha512 SQL function with the given SQLite connection. The function takes a single argument and returns the SHA512 hash (blob) of that argument. The argument can be either a string or a blob. If the argument is NULL, the result is NULL.