This repository was archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeyUtilities.cs
More file actions
59 lines (56 loc) · 2.14 KB
/
KeyUtilities.cs
File metadata and controls
59 lines (56 loc) · 2.14 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OtpSharp
{
/// <summary>
/// Some helper methods to perform common key functions
/// </summary>
internal class KeyUtilities
{
/// <summary>
/// Overwrite potentially sensitive data with random junk
/// </summary>
/// <remarks>
/// Warning!
///
/// This isn't foolproof by any means. The garbage collector could have moved the actual
/// location in memory to another location during a collection cycle and left the old data in place
/// simply marking it as available. We can't control this or even detect it.
/// This method is simply a good faith effort to limit the exposure of sensitive data in memory as much as possible
/// </remarks>
internal static void Destroy(byte[] sensitiveData)
{
if (sensitiveData == null)
throw new ArgumentNullException("sensitiveData");
new Random().NextBytes(sensitiveData);
}
/// <summary>
/// converts a long into a big endian byte array.
/// </summary>
/// <remarks>
/// RFC 4226 specifies big endian as the method for converting the counter to data to hash.
/// </remarks>
static internal byte[] GetBigEndianBytes(long input)
{
// Since .net uses little endian numbers, we need to reverse the byte order to get big endian.
var data = BitConverter.GetBytes(input);
Array.Reverse(data);
return data;
}
/// <summary>
/// converts an int into a big endian byte array.
/// </summary>
/// <remarks>
/// RFC 4226 specifies big endian as the method for converting the counter to data to hash.
/// </remarks>
static internal byte[] GetBigEndianBytes(int input)
{
// Since .net uses little endian numbers, we need to reverse the byte order to get big endian.
var data = BitConverter.GetBytes(input);
Array.Reverse(data);
return data;
}
}
}