DKNet

SHA content hashing

Part of DKNet.Svc.Encryption. IShaHashing computes and verifies SHA-256/512 digests over UTF-8 input, returning hexadecimal strings.

using DKNet.Svc.Encryption.Hashing;

IShaHashing shaHashing = new ShaHashing();
var hash = shaHashing.ComputeSha256("hello world");
var matches = shaHashing.VerifySha256("hello world", hash);
Console.WriteLine(matches); // True

Output is lower-case hex unless upperCase is set. Verification is hex-only (expectedHex), constant-time, and returns false — instead of throwing — when the expected value isn’t valid hex. Empty string input is allowed; null throws ArgumentNullException.

VerifySha256 recomputes the hash and compares the decoded bytes with FixedTimeEquals; equal bytes return true, unequal bytes or invalid hex return false without throwing.

The diagram cannot show ComputeSha256/ComputeSha512 used on their own (without verification) — that path is the first half of the diagram: UTF-8 bytes in, hex string out, no comparison involved.

⚙️ Configuration reference

Member Parameter Default Effect
ComputeSha256/ComputeSha512 upperCase false true returns upper-case hex.
VerifySha256/VerifySha512 ignoreCase true No effect — comparison runs on decoded bytes, which are already case-insensitive.

IShaHashing has no DI-level configuration: AddEncryptionServices() registers it as transient with no arguments.

⚠️ Gotchas & limits