blob: 03164ec37a62ec66c08069f821e338d8672efb2d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
using System;
using System.Security.Cryptography;
namespace Hazel.Crypto
{
public static class SpanCryptoExtensions
{
/// <summary>
/// Clear a span's contents to zero
/// </summary>
public static void SecureClear(this ByteSpan span)
{
if (span.Length > 0)
{
Array.Clear(span.GetUnderlyingArray(), span.Offset, span.Length);
}
}
/// <summary>
/// Fill a byte span with random data
/// </summary>
/// <param name="random">Entropy source</param>
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);
}
}
}
|