using System;
using System.Security.Cryptography;
namespace StellaOps.Cryptography;
///
/// Default implementation of using BCL cryptographic primitives.
///
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 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 data)
{
var hash = ComputeHash(data);
return Convert.ToHexStringLower(hash);
}
}