using System; using System.Security.Cryptography; using System.Text; namespace StellaOps.Notify.Connectors.Shared; /// /// Common hashing helpers for Notify connector metadata. /// public static class ConnectorHashing { /// /// Computes a lowercase hex SHA-256 hash and truncates it to the requested number of bytes. /// public static string ComputeSha256Hash(string value, int lengthBytes = 8) { if (string.IsNullOrWhiteSpace(value)) { throw new ArgumentException("Value must not be null or whitespace.", nameof(value)); } if (lengthBytes <= 0 || lengthBytes > 32) { throw new ArgumentOutOfRangeException(nameof(lengthBytes), "Length must be between 1 and 32 bytes."); } var bytes = Encoding.UTF8.GetBytes(value.Trim()); var hash = SHA256.HashData(bytes); return Convert.ToHexString(hash.AsSpan(0, lengthBytes)).ToLowerInvariant(); } }