Path: redb/redb.SQLite/Data/SqliteHash.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.SQLite/Data/SqliteHash.cs
----
using System;
namespace redb.SQLite.Data;
///
/// The stored form of a 128-bit hash in SQLite — _objects._hash, _schemes._structure_hash,
/// _users._hash: BLOB(16) in RFC 4122 byte order, i.e. the bytes of the canonical text
/// form read left to right (00112233-4455-6677-8899-aabbccddeeff is 00 11 22 33 44 55 …).
///
///
/// One place for the byte order, because several code paths write it and several read it. Writes go
/// through the Guid's TEXT parameter and the SQL wrapper — unhex() of the
/// text without dashes is by construction the order the text shows; the native extension does the same
/// in its own statements. Reads get a byte[] from the driver and use .
/// The two must agree, and Guid.ToByteArray() without the big-endian flag does not: it puts the
/// first three groups in little-endian order.
///
///
///
/// Only hashes. _values._Guid, _objects._value_guid, _users._code_guid are data and
/// stay TEXT — they are compared with values users write and read as text.
///
///
///
/// A database created before V4 holds the hashes as 36-character TEXT. SQLite keeps the storage class
/// a value was written with regardless of the declared type, so no ALTER is needed: the values
/// are converted in place on open (ApplySchemaUpgradesAsync), and a reader that meets a TEXT
/// value anyway still understands it.
///
///
public static class SqliteHash
{
///
/// SQL that turns a canonical uuid TEXT expression (a $n parameter, a column) into the
/// 16-byte BLOB. NULL stays NULL. unhex() needs SQLite 3.41+; the bundled library is newer.
///
public static string FromText(string sqlExpression) => $"unhex(replace({sqlExpression},'-',''))";
/// The 16 bytes of in RFC 4122 order.
public static byte[] ToBlob(Guid value)
{
// Guid.ToByteArray() is little-endian in the first three groups; put them back in text order.
var b = value.ToByteArray();
return new[]
{
b[3], b[2], b[1], b[0],
b[5], b[4],
b[7], b[6],
b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15],
};
}
/// The Guid whose canonical text form spells .
public static Guid FromBlob(ReadOnlySpan bytes)
{
if (bytes.Length != 16)
throw new FormatException($"A stored hash is 16 bytes, got {bytes.Length}.");
return new Guid(
(bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3],
(short)((bytes[4] << 8) | bytes[5]),
(short)((bytes[6] << 8) | bytes[7]),
bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
}
///
/// A Guid from whatever the driver returned for a uuid-valued column: the BLOB of a hash, the
/// TEXT of a data guid (or of a hash not yet converted), or a Guid already.
///
public static Guid FromDbValue(object value) => value switch
{
Guid g => g,
byte[] b => FromBlob(b),
_ => Guid.Parse(value.ToString()!),
};
}