-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FNV and XXHASH hashing algorithms (#8)
fixes #6
- Loading branch information
Showing
10 changed files
with
186 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "sqlite-hashes" | ||
version = "0.6.0" # This value is also used in the README.md | ||
description = "Hashing functions for SQLite with aggregation support: MD5, SHA1, SHA256, SHA512" | ||
description = "Hashing functions for SQLite with aggregation support: MD5, SHA1, SHA256, SHA512, fnv1a, xxhash" | ||
authors = ["Yuri Astrakhan <[email protected]>"] | ||
repository = "https://github.com/nyurik/sqlite-hashes" | ||
edition = "2021" | ||
|
@@ -21,11 +21,11 @@ crate-type = ["cdylib"] | |
required-features = ["loadable_extension"] | ||
|
||
[features] | ||
default = ["trace", "aggregate", "window", "hex", "md5", "sha1", "sha224", "sha256", "sha384", "sha512"] | ||
default = ["trace", "aggregate", "window", "hex", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "fnv", "xxhash"] | ||
# Use this feature to build loadable extension. | ||
# Assumes --no-default-features. | ||
# Does not support windowing functionality yet. | ||
default_loadable_extension = ["loadable_extension", "aggregate", "hex", "md5", "sha1", "sha224", "sha256", "sha384", "sha512"] | ||
default_loadable_extension = ["loadable_extension", "aggregate", "hex", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "fnv", "xxhash"] | ||
# | ||
# Enable Trace Logging | ||
trace = ["dep:hex", "dep:log"] | ||
|
@@ -52,6 +52,8 @@ sha224 = ["dep:sha2"] | |
sha256 = ["dep:sha2"] | ||
sha384 = ["dep:sha2"] | ||
sha512 = ["dep:sha2"] | ||
fnv = ["dep:noncrypto-digests", "noncrypto-digests?/fnv"] | ||
xxhash = ["dep:noncrypto-digests", "noncrypto-digests?/xxh3", "noncrypto-digests?/xxh32", "noncrypto-digests?/xxh64"] | ||
|
||
[dependencies] | ||
hex = { version = "0.4", optional = true } | ||
|
@@ -63,6 +65,7 @@ digest = "0.10.7" | |
md-5 = { version = "0.10.6", optional = true } | ||
sha1 = { version = "0.10.6", optional = true } | ||
sha2 = { version = "0.10.8", optional = true } | ||
noncrypto-digests = { version = "0.3.0", optional = true } | ||
|
||
[dev-dependencies] | ||
cargo-husky = { version = "1", features = ["user-hooks"], default-features = false } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use noncrypto_digests::Fnv; | ||
|
||
use crate::rusqlite::{Connection, Result}; | ||
|
||
/// Register the `fnv1a` SQL function with the given `SQLite` connection. | ||
/// The `fnv1a` function uses [Fowler–Noll–Vo hash function](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash) to compute the hash of the argument(s). | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// # use sqlite_hashes::rusqlite::{Connection, Result}; | ||
/// # use sqlite_hashes::register_fnv_functions; | ||
/// # fn main() -> Result<()> { | ||
/// let db = Connection::open_in_memory()?; | ||
/// register_fnv_functions(&db)?; | ||
/// let hash: Vec<u8> = db.query_row("SELECT fnv1a('hello')", [], |r| r.get(0))?; | ||
/// let expected = b"\xA4\x30\xD8\x46\x80\xAA\xBD\x0B"; | ||
/// assert_eq!(hash, expected); | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn register_fnv_functions(conn: &Connection) -> Result<()> { | ||
crate::scalar::create_hash_fn::<Fnv>(conn, "fnv1a") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use noncrypto_digests::{Xxh32, Xxh3_128, Xxh3_64, Xxh64}; | ||
|
||
use crate::rusqlite::{Connection, Result}; | ||
|
||
/// Register `xxh32`, `xxh64`, `xxh3_64`, `xxh3_128`, `xxh3_64` SQL functions with the given `SQLite` connection. | ||
/// The functions use [Rust xxhash implementation](https://github.com/DoumanAsh/xxhash-rust) to compute the hash of the argument(s) using zero as the seed value. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// # // Use Python to convert: | ||
/// # // print('"\\x' + '\\x'.join([f"{v:02X}" for v in [251, 0, 119, 249]])+'"') | ||
/// # use sqlite_hashes::rusqlite::{Connection, Result}; | ||
/// # use sqlite_hashes::register_xxhash_functions; | ||
/// # fn main() -> Result<()> { | ||
/// let db = Connection::open_in_memory()?; | ||
/// register_xxhash_functions(&db)?; | ||
/// let hash: Vec<u8> = db.query_row("SELECT xxh32('hello')", [], |r| r.get(0))?; | ||
/// let expected = b"\xFB\x00\x77\xF9"; | ||
/// assert_eq!(hash, expected); | ||
/// let hash: Vec<u8> = db.query_row("SELECT xxh64('hello')", [], |r| r.get(0))?; | ||
/// let expected = b"\x26\xC7\x82\x7D\x88\x9F\x6D\xA3"; | ||
/// assert_eq!(hash, expected); | ||
/// let hash: Vec<u8> = db.query_row("SELECT xxh3_64('hello')", [], |r| r.get(0))?; | ||
/// let expected = b"\x95\x55\xE8\x55\x5C\x62\xDC\xFD"; | ||
/// assert_eq!(hash, expected); | ||
/// let hash: Vec<u8> = db.query_row("SELECT xxh3_128('hello')", [], |r| r.get(0))?; | ||
/// let expected = b"\xb5\xe9\xc1\xad\x07\x1b\x3e\x7f\xc7\x79\xcf\xaa\x5e\x52\x38\x18"; | ||
/// assert_eq!(hash, expected); | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn register_xxhash_functions(conn: &Connection) -> Result<()> { | ||
crate::scalar::create_hash_fn::<Xxh32>(conn, "xxh32")?; | ||
crate::scalar::create_hash_fn::<Xxh64>(conn, "xxh64")?; | ||
crate::scalar::create_hash_fn::<Xxh3_64>(conn, "xxh3_64")?; | ||
crate::scalar::create_hash_fn::<Xxh3_128>(conn, "xxh3_128") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters