/
KeyPair.cs
96 lines (87 loc) · 2.43 KB
/
KeyPair.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
namespace Noise
{
/// <summary>
/// A Diffie-Hellman private/public key pair.
/// </summary>
public sealed class KeyPair : IDisposable
{
private static readonly Curve25519 dh = new Curve25519();
private readonly byte[] privateKey;
private readonly byte[] publicKey;
private bool disposed;
/// <summary>
/// Initializes a new instance of the <see cref="KeyPair"/> class.
/// </summary>
/// <param name="privateKey">The private key.</param>
/// <param name="publicKey">The public key.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the <paramref name="privateKey"/> or the <paramref name="publicKey"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// Thrown if the lengths of the <paramref name="privateKey"/> or the <paramref name="publicKey"/> are invalid.
/// </exception>
internal KeyPair(byte[] privateKey, byte[] publicKey)
{
Exceptions.ThrowIfNull(privateKey, nameof(privateKey));
Exceptions.ThrowIfNull(publicKey, nameof(publicKey));
if (privateKey.Length != 32)
{
throw new ArgumentException("Private key must have length of 32 bytes.", nameof(privateKey));
}
if (publicKey.Length != 32)
{
throw new ArgumentException("Public key must have length of 32 bytes.", nameof(publicKey));
}
this.privateKey = privateKey;
this.publicKey = publicKey;
}
/// <summary>
/// Generates a new Diffie-Hellman key pair.
/// </summary>
/// <returns>A randomly generated private key and its corresponding public key.</returns>
public static KeyPair Generate()
{
return dh.GenerateKeyPair();
}
/// <summary>
/// Gets the private key.
/// </summary>
/// <exception cref="ObjectDisposedException">
/// Thrown if the current instance has already been disposed.
/// </exception>
public byte[] PrivateKey
{
get
{
Exceptions.ThrowIfDisposed(disposed, nameof(KeyPair));
return privateKey;
}
}
/// <summary>
/// Gets the public key.
/// </summary>
/// <exception cref="ObjectDisposedException">
/// Thrown if the current instance has already been disposed.
/// </exception>
public byte[] PublicKey
{
get
{
Exceptions.ThrowIfDisposed(disposed, nameof(KeyPair));
return publicKey;
}
}
/// <summary>
/// Erases the key pair from the memory.
/// </summary>
public void Dispose()
{
if (!disposed)
{
Utilities.ZeroMemory(privateKey);
disposed = true;
}
}
}
}