Skip to content

Commit 098fd1e

Browse files
Merge branch 'main' into release
2 parents a33bbf1 + f246e16 commit 098fd1e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2240
-374
lines changed

Directory.Build.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<NeutralLanguage>en</NeutralLanguage>
99
<Copyright>Copyright © ONIXLabs 2020</Copyright>
1010
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
11-
<Version>10.0.0</Version>
12-
<PackageVersion>10.0.0</PackageVersion>
13-
<AssemblyVersion>10.0.0</AssemblyVersion>
11+
<Version>11.0.0</Version>
12+
<PackageVersion>11.0.0</PackageVersion>
13+
<AssemblyVersion>11.0.0</AssemblyVersion>
1414
</PropertyGroup>
1515
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 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+
using Xunit;
17+
18+
namespace OnixLabs.Core.UnitTests;
19+
20+
public sealed class DateTimeExtensionTests
21+
{
22+
[Theory(DisplayName = "ToDateOnly should produce the expected result")]
23+
[InlineData(1, 1, 1, 0, 0, 0)]
24+
[InlineData(1999, 12, 12, 12, 30, 31)]
25+
public void ToDateOnlyShouldProduceExpectedResult(int year, int month, int day, int hour, int minute, int second)
26+
{
27+
// When
28+
DateTime value = new(year, month, day, hour, minute, second);
29+
DateOnly expected = DateOnly.FromDateTime(value);
30+
DateOnly actual = value.ToDateOnly();
31+
32+
// Then
33+
Assert.Equal(expected, actual);
34+
}
35+
36+
[Theory(DisplayName = "ToTimeOnly should produce the expected result")]
37+
[InlineData(1, 1, 1, 0, 0, 0)]
38+
[InlineData(1999, 12, 12, 12, 30, 31)]
39+
public void ToTimeOnlyShouldProduceExpectedResult(int year, int month, int day, int hour, int minute, int second)
40+
{
41+
// When
42+
DateTime value = new(year, month, day, hour, minute, second);
43+
TimeOnly expected = TimeOnly.FromDateTime(value);
44+
TimeOnly actual = value.ToTimeOnly();
45+
46+
// Then
47+
Assert.Equal(expected, actual);
48+
}
49+
}

OnixLabs.Core/Extensions.DateTime.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2020-2024 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+
using System.ComponentModel;
17+
18+
namespace OnixLabs.Core;
19+
20+
/// <summary>
21+
/// Provides extension methods for <see cref="DateTime"/> instances.
22+
/// </summary>
23+
[EditorBrowsable(EditorBrowsableState.Never)]
24+
public static class DateTimeExtensions
25+
{
26+
/// <summary>
27+
/// Obtains a <see cref="DateOnly"/> instance from the current <see cref="DateTime"/> instance.
28+
/// </summary>
29+
/// <param name="value">The <see cref="DateTime"/> instance from which to obtain a <see cref="DateOnly"/> instance.</param>
30+
/// <returns>Returns the <see cref="DateOnly"/> instance obtained from the current <see cref="DateTime"/> instance.</returns>
31+
public static DateOnly ToDateOnly(this DateTime value) => DateOnly.FromDateTime(value);
32+
33+
/// <summary>
34+
/// Obtains a <see cref="TimeOnly"/> instance from the current <see cref="TimeOnly"/> instance.
35+
/// </summary>
36+
/// <param name="value">The <see cref="DateTime"/> instance from which to obtain a <see cref="TimeOnly"/> instance.</param>
37+
/// <returns>Returns the <see cref="TimeOnly"/> instance obtained from the current <see cref="DateTime"/> instance.</returns>
38+
public static TimeOnly ToTimeOnly(this DateTime value) => TimeOnly.FromDateTime(value);
39+
}

OnixLabs.Core/Extensions.Object.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace OnixLabs.Core;
2626

2727
/// <summary>
28-
/// Provides extension methods for objects.
28+
/// Provides extension methods for <see cref="object"/> instances.
2929
/// </summary>
3030
[EditorBrowsable(EditorBrowsableState.Never)]
3131
public static class ObjectExtensions

OnixLabs.Core/Extensions.Random.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace OnixLabs.Core;
2020

2121
/// <summary>
22-
/// Provides extension methods for objects.
22+
/// Provides extension methods for <see cref="Random"/> instances.
2323
/// </summary>
2424
[EditorBrowsable(EditorBrowsableState.Never)]
2525
public static class RandomExtensions

OnixLabs.Core/Extensions.ReadOnlySequence.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace OnixLabs.Core;
1919

2020
/// <summary>
21-
/// Provides extension methods for read-only sequences.
21+
/// Provides extension methods for <see cref="ReadOnlySequence{T}"/> instances.
2222
/// </summary>
2323
[EditorBrowsable(EditorBrowsableState.Never)]
2424
public static class ReadOnlySequenceExtensions

OnixLabs.Core/Extensions.String.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace OnixLabs.Core;
2323

2424
/// <summary>
25-
/// Provides extension methods for strings.
25+
/// Provides extension methods for <see cref="string"/> instances.
2626
/// </summary>
2727
[EditorBrowsable(EditorBrowsableState.Never)]
2828
public static class StringExtensions

OnixLabs.Security.Cryptography.UnitTests/EcdhKeyTests.cs

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public void EcdhPkcs8RoundTripDeriveSharedSecretShouldProduceIdenticalSecrets()
4040
// Given
4141
using HashAlgorithm algorithm = SHA256.Create();
4242
PbeParameters parameters = new(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, 10);
43-
byte[] aliceExportedBytes = EcdhPrivateKey.Create().ExportPkcs8PrivateKey("AlicePassword", parameters);
44-
byte[] bobExportedBytes = EcdhPrivateKey.Create().ExportPkcs8PrivateKey("BobPassword", parameters);
43+
byte[] aliceExportedBytes = EcdhPrivateKey.Create().ExportPkcs8("AlicePassword", parameters);
44+
byte[] bobExportedBytes = EcdhPrivateKey.Create().ExportPkcs8("BobPassword", parameters);
4545

46-
IEcdhPrivateKey alice = EcdhPrivateKey.ImportPkcs8PrivateKey(aliceExportedBytes, "AlicePassword");
47-
IEcdhPrivateKey bob = EcdhPrivateKey.ImportPkcs8PrivateKey(bobExportedBytes, "BobPassword");
46+
IEcdhPrivateKey alice = EcdhPrivateKey.ImportPkcs8(aliceExportedBytes, "AlicePassword");
47+
IEcdhPrivateKey bob = EcdhPrivateKey.ImportPkcs8(bobExportedBytes, "BobPassword");
4848

4949
// When
5050
Secret aliceSecret = alice.DeriveSharedSecret(bob.GetPublicKey());
@@ -53,4 +53,118 @@ public void EcdhPkcs8RoundTripDeriveSharedSecretShouldProduceIdenticalSecrets()
5353
// Then
5454
Assert.Equal(aliceSecret, bobSecret);
5555
}
56+
57+
[Fact(DisplayName = "EcdhPrivateKey should be exportable and importable")]
58+
public void EcdhPrivateKeyShouldBeExportableAndImportable()
59+
{
60+
// Given
61+
EcdhPrivateKey expected = EcdhPrivateKey.Create();
62+
63+
// When
64+
byte[] privateKeyData = expected.Export();
65+
EcdhPrivateKey actual = EcdhPrivateKey.Import(privateKeyData);
66+
67+
// Then
68+
Assert.Equal(expected, actual);
69+
}
70+
71+
[Fact(DisplayName = "EcdhPrivateKey should be exportable and importable as PEM")]
72+
public void EcdhPrivateKeyShouldBeExportableAndImportableAsPem()
73+
{
74+
// Given
75+
EcdhPrivateKey expected = EcdhPrivateKey.Create();
76+
77+
// When
78+
string privateKeyData = expected.ExportPem();
79+
EcdhPrivateKey actual = EcdhPrivateKey.ImportPem(privateKeyData);
80+
81+
// Then
82+
Assert.Equal(expected, actual);
83+
}
84+
85+
[Fact(DisplayName = "EcdhPrivateKey should be exportable and importable as PKCS8")]
86+
public void EcdhPrivateKeyShouldBeExportableAndImportableAsPkcs8()
87+
{
88+
// Given
89+
EcdhPrivateKey expected = EcdhPrivateKey.Create();
90+
91+
// When
92+
byte[] privateKeyData = expected.ExportPkcs8();
93+
EcdhPrivateKey actual = EcdhPrivateKey.ImportPkcs8(privateKeyData);
94+
95+
// Then
96+
Assert.Equal(expected, actual);
97+
}
98+
99+
[Fact(DisplayName = "EcdhPrivateKey should be exportable and importable as PKCS8 PEM")]
100+
public void EcdhPrivateKeyShouldBeExportableAndImportableAsPkcs8Pem()
101+
{
102+
// Given
103+
EcdhPrivateKey expected = EcdhPrivateKey.Create();
104+
105+
// When
106+
string privateKeyData = expected.ExportPkcs8Pem();
107+
EcdhPrivateKey actual = EcdhPrivateKey.ImportPem(privateKeyData);
108+
109+
// Then
110+
Assert.Equal(expected, actual);
111+
}
112+
113+
[Fact(DisplayName = "EcdhPrivateKey should be exportable and importable as encrypted PKCS8")]
114+
public void EcdhPrivateKeyShouldBeExportableAndImportableAsEncryptedPkcs8()
115+
{
116+
// Given
117+
EcdhPrivateKey expected = EcdhPrivateKey.Create();
118+
PbeParameters parameters = new(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, 10);
119+
120+
// When
121+
byte[] privateKeyData = expected.ExportPkcs8("Password", parameters);
122+
EcdhPrivateKey actual = EcdhPrivateKey.ImportPkcs8(privateKeyData, "Password");
123+
124+
// Then
125+
Assert.Equal(expected, actual);
126+
}
127+
128+
[Fact(DisplayName = "EcdhPrivateKey should be exportable and importable as encrypted PKCS8 PEM")]
129+
public void EcdhPrivateKeyShouldBeExportableAndImportableAsEncryptedPkcs8Pem()
130+
{
131+
// Given
132+
EcdhPrivateKey expected = EcdhPrivateKey.Create();
133+
PbeParameters parameters = new(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, 10);
134+
135+
// When
136+
string privateKeyData = expected.ExportPkcs8Pem("Password", parameters);
137+
EcdhPrivateKey actual = EcdhPrivateKey.ImportPem(privateKeyData, "Password");
138+
139+
// Then
140+
Assert.Equal(expected, actual);
141+
}
142+
143+
[Fact(DisplayName = "EcdhPublicKey should be exportable and importable")]
144+
public void EcdhPublicKeyShouldBeExportableAndImportable()
145+
{
146+
// Given
147+
EcdhPublicKey expected = EcdhPrivateKey.Create().GetPublicKey();
148+
149+
// When
150+
byte[] privateKeyData = expected.Export();
151+
EcdhPublicKey actual = EcdhPublicKey.Import(privateKeyData);
152+
153+
// Then
154+
Assert.Equal(expected, actual);
155+
}
156+
157+
[Fact(DisplayName = "EcdhPublicKey should be exportable and importable as PEM")]
158+
public void EcdhPublicKeyShouldBeExportableAndImportableAsPem()
159+
{
160+
// Given
161+
EcdhPublicKey expected = EcdhPrivateKey.Create().GetPublicKey();
162+
163+
// When
164+
string privateKeyData = expected.ExportPem();
165+
EcdhPublicKey actual = EcdhPublicKey.ImportPem(privateKeyData);
166+
167+
// Then
168+
Assert.Equal(expected, actual);
169+
}
56170
}

0 commit comments

Comments
 (0)