forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcdsaKey.BclImpl.cs
More file actions
84 lines (70 loc) · 2.38 KB
/
EcdsaKey.BclImpl.cs
File metadata and controls
84 lines (70 loc) · 2.38 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
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
#if !NET462
#nullable enable
using System;
using System.Security.Cryptography;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security
{
public partial class EcdsaKey : Key, IDisposable
{
private sealed class BclImpl : Impl
{
private readonly HashAlgorithmName _hashAlgorithmName;
public BclImpl(string curve_oid, int cord_size, byte[] qx, byte[] qy, byte[]? privatekey)
{
var curve = ECCurve.CreateFromValue(curve_oid);
var parameter = new ECParameters
{
Curve = curve
};
parameter.Q.X = qx;
parameter.Q.Y = qy;
if (privatekey != null)
{
parameter.D = privatekey.TrimLeadingZeros().Pad(cord_size);
PrivateKey = parameter.D;
}
Ecdsa = ECDsa.Create(parameter);
_hashAlgorithmName = KeyLength switch
{
<= 256 => HashAlgorithmName.SHA256,
<= 384 => HashAlgorithmName.SHA384,
_ => HashAlgorithmName.SHA512,
};
}
public override byte[]? PrivateKey { get; }
public override ECDsa Ecdsa { get; }
public override int KeyLength
{
get
{
return Ecdsa.KeySize;
}
}
public override byte[] Sign(byte[] input)
{
return Ecdsa.SignData(input, _hashAlgorithmName);
}
public override bool Verify(byte[] input, byte[] signature)
{
return Ecdsa.VerifyData(input, signature, _hashAlgorithmName);
}
public override void Export(out byte[] qx, out byte[] qy)
{
var parameter = Ecdsa.ExportParameters(includePrivateParameters: false);
#pragma warning disable IDE0370 // Remove unnecessary suppression
qx = parameter.Q.X!;
qy = parameter.Q.Y!;
#pragma warning restore IDE0370 // Remove unnecessary suppression
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Ecdsa.Dispose();
}
}
}
}
}
#endif