using System.Security.Cryptography;
namespace ZB.MOM.WW.Secrets.Abstractions;
///
/// One consistent reading of a master key (KEK): the 32 key bytes together with the
/// that identifies those bytes. Obtained from
/// once per cipher operation, so the id stamped on a
/// row and the key that wraps it can never come from two different reads of a source that changed
/// in between (a key file replaced mid-operation).
///
///
/// The snapshot owns its key buffer: zeroes it with
/// . Always dispose a snapshot
/// (using var snapshot = provider.GetSnapshot();) and never retain past it.
///
public sealed class MasterKeySnapshot : IDisposable
{
private readonly byte[] _key;
///
/// Creates a snapshot that takes ownership of (it is zeroed on
/// ; pass a buffer nothing else uses).
///
/// The non-secret id of .
/// The key bytes; ownership transfers to the snapshot.
public MasterKeySnapshot(string kekId, byte[] key)
{
KekId = kekId ?? throw new ArgumentNullException(nameof(kekId));
_key = key ?? throw new ArgumentNullException(nameof(key));
}
/// The non-secret identifier of .
public string KekId { get; }
/// The key bytes. Valid only until , which zeroes them.
public ReadOnlyMemory Key => _key;
/// Zeroes the key buffer.
public void Dispose() => CryptographicOperations.ZeroMemory(_key);
}