Path: redb/redb.Core/Utils/RedbMd5.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.Core/Utils/RedbMd5.cs
----
using System;
using System.Security.Cryptography;
namespace redb.Core.Utils
{
///
/// MD5 entry point for REDB hashing.
///
///
/// REDB stores hashes as (exactly 16 bytes) in the database, so the algorithm
/// itself cannot change without invalidating every existing database. It stays MD5 — this type only
/// decides WHICH implementation computes it.
///
///
///
/// On server platforms nothing changes: the call goes straight to , exactly
/// as before. The managed fallback exists for browser-wasm (Blazor WebAssembly), where the
/// runtime ships no MD5 provider and throws
/// CryptographicException: Cryptography_UnknownHashAlgorithm, MD5 — which used to break
/// SyncScheme and Save outright.
///
///
///
/// The fallback is bit-for-bit identical to the system implementation (RFC 1321); this is asserted by
/// unit tests comparing both over RFC test vectors, block boundaries and random buffers. Hashes
/// written by a browser and by a server are therefore interchangeable.
///
///
public static class RedbMd5
{
///
/// Compute the MD5 digest of (16 bytes).
/// Uses the platform implementation where it exists, the managed one where it does not.
///
public static byte[] ComputeHash(byte[] data)
{
if (UseManagedImplementation)
return ComputeHashManaged(data);
using var md5 = MD5.Create();
return md5.ComputeHash(data);
}
///
/// True when the platform has no usable MD5 provider and the managed implementation must be used.
/// Today that is browser-wasm only; every other target keeps the previous code path.
///
public static bool UseManagedImplementation { get; } = OperatingSystem.IsBrowser();
// ===== Managed RFC 1321 implementation =====
// Public (not private) on purpose: tests must be able to compare it against the system
// implementation on a normal desktop run, where UseManagedImplementation is false and the
// fallback would otherwise never execute.
///
/// Managed MD5 per RFC 1321. Produces the same 16 bytes as the platform implementation.
/// Intended for platforms without an MD5 provider, and for tests asserting that equivalence.
///
public static byte[] ComputeHashManaged(byte[] data)
{
if (data is null) throw new ArgumentNullException(nameof(data));
uint a0 = 0x67452301, b0 = 0xefcdab89, c0 = 0x98badcfe, d0 = 0x10325476;
// Padded length: message + 0x80 + zero padding + 8-byte little-endian bit count,
// rounded up to a whole number of 64-byte blocks.
int paddedLength = ((data.Length + 8) / 64 + 1) * 64;
var buffer = new byte[paddedLength];
Buffer.BlockCopy(data, 0, buffer, 0, data.Length);
buffer[data.Length] = 0x80;
ulong bitLength = (ulong)data.Length * 8;
for (int i = 0; i < 8; i++)
buffer[paddedLength - 8 + i] = (byte)(bitLength >> (8 * i));
var m = new uint[16];
for (int offset = 0; offset < paddedLength; offset += 64)
{
for (int j = 0; j < 16; j++)
{
int k = offset + j * 4;
m[j] = (uint)(buffer[k] | (buffer[k + 1] << 8) | (buffer[k + 2] << 16) | (buffer[k + 3] << 24));
}
uint a = a0, b = b0, c = c0, d = d0;
for (int i = 0; i < 64; i++)
{
uint f;
int g;
if (i < 16)
{
f = (b & c) | (~b & d);
g = i;
}
else if (i < 32)
{
f = (d & b) | (~d & c);
g = (5 * i + 1) % 16;
}
else if (i < 48)
{
f = b ^ c ^ d;
g = (3 * i + 5) % 16;
}
else
{
f = c ^ (b | ~d);
g = (7 * i) % 16;
}
f = unchecked(f + a + K[i] + m[g]);
a = d;
d = c;
c = b;
b = unchecked(b + RotateLeft(f, S[i]));
}
a0 = unchecked(a0 + a);
b0 = unchecked(b0 + b);
c0 = unchecked(c0 + c);
d0 = unchecked(d0 + d);
}
var digest = new byte[16];
WriteLittleEndian(digest, 0, a0);
WriteLittleEndian(digest, 4, b0);
WriteLittleEndian(digest, 8, c0);
WriteLittleEndian(digest, 12, d0);
return digest;
}
private static uint RotateLeft(uint value, int count)
=> (value << count) | (value >> (32 - count));
private static void WriteLittleEndian(byte[] target, int offset, uint value)
{
target[offset] = (byte)value;
target[offset + 1] = (byte)(value >> 8);
target[offset + 2] = (byte)(value >> 16);
target[offset + 3] = (byte)(value >> 24);
}
/// Per-round left-rotation amounts (RFC 1321, section 3.4).
private static readonly int[] S =
{
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
};
/// Sine-derived constants: K[i] = floor(2^32 * abs(sin(i + 1))).
private static readonly uint[] K =
{
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
};
}
}