Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
api-governance / spectral-lint (push) Has been cancelled
oas-ci / oas-validate (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Policy Simulation / policy-simulate (push) Has been cancelled
SDK Publish & Sign / sdk-publish (push) Has been cancelled
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace StellaOps.Cryptography;
|
|
|
|
/// <summary>
|
|
/// Default implementation of <see cref="ICryptoHasher"/> using BCL cryptographic primitives.
|
|
/// </summary>
|
|
public sealed class DefaultCryptoHasher : ICryptoHasher
|
|
{
|
|
public DefaultCryptoHasher(string algorithmId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(algorithmId))
|
|
{
|
|
throw new ArgumentException("Algorithm identifier is required.", nameof(algorithmId));
|
|
}
|
|
|
|
AlgorithmId = algorithmId.ToUpperInvariant();
|
|
}
|
|
|
|
public string AlgorithmId { get; }
|
|
|
|
public byte[] ComputeHash(ReadOnlySpan<byte> data)
|
|
{
|
|
return AlgorithmId switch
|
|
{
|
|
HashAlgorithms.Sha256 => SHA256.HashData(data),
|
|
HashAlgorithms.Sha384 => SHA384.HashData(data),
|
|
HashAlgorithms.Sha512 => SHA512.HashData(data),
|
|
_ => throw new InvalidOperationException($"Unsupported hash algorithm '{AlgorithmId}'.")
|
|
};
|
|
}
|
|
|
|
public string ComputeHashHex(ReadOnlySpan<byte> data)
|
|
{
|
|
var hash = ComputeHash(data);
|
|
return Convert.ToHexStringLower(hash);
|
|
}
|
|
}
|