Skip to content

Commit 23740c6

Browse files
Merge branch 'main' into release
2 parents 0b63d69 + 4aaad0c commit 23740c6

File tree

5 files changed

+73
-11
lines changed

5 files changed

+73
-11
lines changed

OnixLabs.Core/OnixLabs.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
<Title>OnixLabs.Core</Title>
99
<Authors>ONIXLabs</Authors>
1010
<Description>ONIXLabs Core API for .NET</Description>
11-
<AssemblyVersion>3.0.0</AssemblyVersion>
11+
<AssemblyVersion>4.0.0</AssemblyVersion>
1212
<NeutralLanguage>en</NeutralLanguage>
1313
<Copyright>Copyright © ONIXLabs 2020-2021</Copyright>
1414
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
15-
<PackageVersion>3.1.0</PackageVersion>
15+
<PackageVersion>4.0.0</PackageVersion>
1616
</PropertyGroup>
1717

1818
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">

OnixLabs.Security.Cryptography/KeyPair.Create.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static KeyPair CreateEcdsaKeyPair(HashAlgorithmType type)
3030
PrivateKey privateKey = new EcdsaPrivateKey(asymmetricAlgorithm.ExportECPrivateKey(), type);
3131
PublicKey publicKey = new EcdsaPublicKey(asymmetricAlgorithm.ExportSubjectPublicKeyInfo(), type);
3232

33-
return new KeyPair(privateKey, publicKey);
33+
return new KeyPair(privateKey, publicKey, type);
3434
}
3535

3636
/// <summary>
@@ -46,7 +46,7 @@ public static KeyPair CreateEcdsaKeyPair(HashAlgorithmType type, ECCurve curve)
4646
PrivateKey privateKey = new EcdsaPrivateKey(asymmetricAlgorithm.ExportECPrivateKey(), type);
4747
PublicKey publicKey = new EcdsaPublicKey(asymmetricAlgorithm.ExportSubjectPublicKeyInfo(), type);
4848

49-
return new KeyPair(privateKey, publicKey);
49+
return new KeyPair(privateKey, publicKey, type);
5050
}
5151

5252
/// <summary>
@@ -62,7 +62,7 @@ public static KeyPair CreateEcdsaKeyPair(HashAlgorithmType type, ECParameters pa
6262
PrivateKey privateKey = new EcdsaPrivateKey(asymmetricAlgorithm.ExportECPrivateKey(), type);
6363
PublicKey publicKey = new EcdsaPublicKey(asymmetricAlgorithm.ExportSubjectPublicKeyInfo(), type);
6464

65-
return new KeyPair(privateKey, publicKey);
65+
return new KeyPair(privateKey, publicKey, type);
6666
}
6767

6868
/// <summary>
@@ -78,7 +78,7 @@ public static KeyPair CreateRsaKeyPair(HashAlgorithmType type, RSASignaturePaddi
7878
PrivateKey privateKey = new RsaPrivateKey(asymmetricAlgorithm.ExportRSAPrivateKey(), type, padding);
7979
PublicKey publicKey = new RsaPublicKey(asymmetricAlgorithm.ExportRSAPublicKey(), type, padding);
8080

81-
return new KeyPair(privateKey, publicKey);
81+
return new KeyPair(privateKey, publicKey, type);
8282
}
8383

8484
/// <summary>
@@ -95,7 +95,7 @@ public static KeyPair CreateRsaKeyPair(HashAlgorithmType type, RSASignaturePaddi
9595
PrivateKey privateKey = new RsaPrivateKey(asymmetricAlgorithm.ExportRSAPrivateKey(), type, padding);
9696
PublicKey publicKey = new RsaPublicKey(asymmetricAlgorithm.ExportRSAPublicKey(), type, padding);
9797

98-
return new KeyPair(privateKey, publicKey);
98+
return new KeyPair(privateKey, publicKey, type);
9999
}
100100

101101
/// <summary>
@@ -115,7 +115,7 @@ public static KeyPair CreateRsaKeyPair(
115115
PrivateKey privateKey = new RsaPrivateKey(asymmetricAlgorithm.ExportRSAPrivateKey(), type, padding);
116116
PublicKey publicKey = new RsaPublicKey(asymmetricAlgorithm.ExportRSAPublicKey(), type, padding);
117117

118-
return new KeyPair(privateKey, publicKey);
118+
return new KeyPair(privateKey, publicKey, type);
119119
}
120120
}
121121
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2020-2021 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
17+
namespace OnixLabs.Security.Cryptography
18+
{
19+
public sealed partial class KeyPair
20+
{
21+
/// <summary>
22+
/// Creates a key pair from the specified public and private key components.
23+
/// </summary>
24+
/// <param name="privateKey">The private key component of the key pair.</param>
25+
/// <param name="publicKey">The public key component of the key pair.</param>
26+
/// <param name="type">The hash algorithm type of the key pair.</param>
27+
/// <returns>Returns a new <see cref="KeyPair"/> instance from the key components.</returns>
28+
/// <exception cref="ArgumentException">If the private key hash algorithm type is mismatched.</exception>
29+
/// <exception cref="ArgumentException">If the public key hash algorithm type is mismatched.</exception>
30+
/// <exception cref="ArgumentException">If the private and public key components are mismatched.</exception>
31+
public static KeyPair FromKeyComponents(PrivateKey privateKey, PublicKey publicKey, HashAlgorithmType type)
32+
{
33+
if (privateKey.AlgorithmType != type)
34+
{
35+
throw new ArgumentException($"Private key hash algorithm type is mismatched with '{type}'.");
36+
}
37+
38+
if (publicKey.AlgorithmType != type)
39+
{
40+
throw new ArgumentException($"Public key hash algorithm type is mismatched with '{type}'.");
41+
}
42+
43+
byte[] random = Guid.NewGuid().ToByteArray();
44+
Hash hash = Hash.ComputeSha2Hash256(random);
45+
DigitalSignature signature = privateKey.SignHash(hash);
46+
47+
if (!signature.IsHashValid(hash, publicKey))
48+
{
49+
throw new ArgumentException("Invalid key pair. The specified public and private keys are mismatched.");
50+
}
51+
52+
return new KeyPair(privateKey, publicKey, type);
53+
}
54+
}
55+
}

OnixLabs.Security.Cryptography/KeyPair.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ public sealed partial class KeyPair
2424
/// </summary>
2525
/// <param name="privateKey">The private key component of this key pair.</param>
2626
/// <param name="publicKey">The public key component of this key pair.</param>
27-
private KeyPair(PrivateKey privateKey, PublicKey publicKey)
27+
/// <param name="algorithmType">The hash algorithm type of this key pair.</param>
28+
private KeyPair(PrivateKey privateKey, PublicKey publicKey, HashAlgorithmType algorithmType)
2829
{
2930
PrivateKey = privateKey;
3031
PublicKey = publicKey;
32+
AlgorithmType = algorithmType;
3133
}
3234

3335
/// <summary>
@@ -39,5 +41,10 @@ private KeyPair(PrivateKey privateKey, PublicKey publicKey)
3941
/// Gets the public key component of this key pair.
4042
/// </summary>
4143
public PublicKey PublicKey { get; }
44+
45+
/// <summary>
46+
/// Gets the hash algorithm type of this key pair.
47+
/// </summary>
48+
public HashAlgorithmType AlgorithmType { get; }
4249
}
4350
}

OnixLabs.Security.Cryptography/OnixLabs.Security.Cryptography.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
<Title>OnixLabs.Security.Cryptography</Title>
66
<Authors>ONIXLabs</Authors>
77
<Description>ONIXLabs Cryptography API for .NET</Description>
8-
<AssemblyVersion>3.0.0</AssemblyVersion>
8+
<AssemblyVersion>4.0.0</AssemblyVersion>
99
<NeutralLanguage>en</NeutralLanguage>
1010
<Nullable>enable</Nullable>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1212
<Copyright>Copyright © ONIXLabs 2020-2021</Copyright>
1313
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
14-
<PackageVersion>3.1.0</PackageVersion>
14+
<PackageVersion>4.0.0</PackageVersion>
1515
</PropertyGroup>
1616

1717
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">

0 commit comments

Comments
 (0)