using System; using System.Security.Cryptography; namespace Hazel.Crypto { public static class SpanCryptoExtensions { /// /// Clear a span's contents to zero /// public static void SecureClear(this ByteSpan span) { if (span.Length > 0) { Array.Clear(span.GetUnderlyingArray(), span.Offset, span.Length); } } /// /// Fill a byte span with random data /// /// Entropy source public static void FillWithRandom(this ByteSpan span, RandomNumberGenerator random) { if (span.Offset == 0 && span.Length == span.GetUnderlyingArray().Length) { random.GetBytes(span.GetUnderlyingArray()); return; } byte[] temp = new byte[span.Length]; random.GetBytes(temp); new ByteSpan(temp).CopyTo(span); } } }