sqlite-hashes
Use this crate to add various hash functions to SQLite, including MD5, SHA1, SHA224, SHA256, SHA384, SHA512, FNV1a, XXHASH.
This crate uses rusqlite to add user-defined functions using static linking. Eventually it would be good to build dynamically loadable extension binaries usable from other languages (PRs welcome).
Usage
Extension
To use as an extension, load the sqlite_hashes.so
shared library into SQLite.
;
)
Rust library
There are two types of scalar functions, the <hash>(...)
and <hash>_hex(...)
, e.g. sha256(...)
and sha256_hex(...)
. The first one returns a blob, and the second one returns a hex string. All functions can hash text and blob values, but will raise an error on other types like integers and floating point numbers. Functions support any number of arguments, e.g. sha256(a, b, c, ...)
, hashing them in order. All NULL
values are ignored. When calling the built-in SQLite hex(NULL)
, the result is an empty string, so sha256_hex(NULL)
will return an empty string as well to be consistent.
use ;
Aggregate and Window Functions
When aggregate
or window
feature is enabled (default), there are functions to compute combined hash over a set of values like a column in a table, e.g. sha256_concat
and sha256_concat_hex
. Just like scalar functions, multiple arguments are also supported, so you can compute a hash over a set of columns, e.g. sha256_concat(col1, col2, col3)
. Note that the window functionality is not supported in the loadable extension.
IMPORTANT NOTE: ORDERING
SQLite does NOT guarantee the order of rows when executing aggregate functions. A query SELECT group_concat(v) FROM tbl ORDER BY v;
will NOT concatenate values in sorted order, but will use some internal storage order instead. Other databases like PostgreSQL support SELECT string_agg(v ORDER BY v) FROM tbl;
, but SQLite does not.
One common workaround is to use a subquery, e.g. SELECT group_concat(v) FROM (SELECT v FROM tbl ORDER BY v);
. This is NOT guaranteed to work in future versions of SQLite. See discussion for more details.
In order to guarantee the ordering, you must use a window function.
SELECT sha256_concat_hex(v)
OVER (ORDER BY v ROWS
BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM tbl
LIMIT 1;
The hashing window functions will only work if the starting point of the window is not moving (UNBOUNDED PRECEDING
). To force a non-NULL value, use COALESCE:
SELECT coalesce(
(SELECT sha256_concat_hex(v)
OVER (ORDER BY v ROWS
BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM tbl
LIMIT 1),
sha256_hex('')
);
Note that window functions are only available in SQLite 3.25 and later, so a bundled SQLite version must be used, at least for now.
use ;
Crate features
By default, this crate will compile with all features. You can enable just the ones you need to reduce compile time and binary size.
[]
= { = "0.6", = false, = ["hex", "window", "sha256"] }
- trace - enable tracing support, logging all function calls and their arguments
- hex - enable hex string functions like
*_hex()
and*_concat_hex()
(ifaggregate
is enabled) - aggregate - enable aggregate functions like
*_concat()
and*_concat_hex()
(ifhex
is enabled) - window - enable window functions support (implies
aggregate
) - md5 - enable MD5 hash support
- sha1 - enable SHA1 hash support
- sha224 - enable SHA224 hash support
- sha256 - enable SHA256 hash support
- sha384 - enable SHA384 hash support
- sha512 - enable SHA512 hash support
- fnv - enable fnv1a hash support
- xxhash - enable xxh32, xxh64, xxh3_64, xxh3_128 hash support
Development
- This project is easier to develop with just, a modern alternative to
make
. Install it withcargo 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, includingcargo fmt
,cargo clippy
, andcargo test
. Usegit push --no-verify
to skip these checks.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
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.