Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/crypto-extensions #90

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions OnixLabs.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using OnixLabs.Security.Cryptography;

namespace OnixLabs.Playground;

internal static class Program
{
private static void Main()
{
string value = "SHA256:043a718774c572bd8a25adbeb1bfcd5c0256ae11cecf9f9c3f925d0e52beaf89";

NamedHash hash = NamedHash.Parse(value);

Console.WriteLine(hash);
}
}
28 changes: 28 additions & 0 deletions OnixLabs.Security.Cryptography.UnitTests/NamedHashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Security.Cryptography;
using Xunit;

Expand All @@ -33,4 +34,31 @@ public void NamedHashToStringShouldProduceExpectedResult()
// Then
Assert.Equal(expected, actual);
}

[Fact(DisplayName = "NamedHash.Parse should produce the expected result")]
public void NamedHashParseShouldProduceExpectedResult()
{
// Given
const string expected = "SHA256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";

// When
NamedHash hash = NamedHash.Parse(expected);
string actual = hash.ToString();

// Then
Assert.Equal(expected, actual);
}

[Fact(DisplayName = "NamedHash.Parse should throw FormatException")]
public void NamedHashParseShouldThrowFormatException()
{
// Given
const string expected = "SHA256:InvalidData";

// When
Exception exception = Assert.Throws<FormatException>(() => NamedHash.Parse(expected));

// Then
Assert.Equal("The input string 'SHA256:InvalidData' was not in a correct format.", exception.Message);
}
}
16 changes: 16 additions & 0 deletions OnixLabs.Security.Cryptography.UnitTests/PublicKeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Security.Cryptography;
using OnixLabs.Security.Cryptography.UnitTests.Data;
using Xunit;

Expand Down Expand Up @@ -120,4 +121,19 @@ public void DifferentPublicKeyValuesShouldProduceDifferentPublicKeyCodes()
// Then
Assert.NotEqual(leftHashCode, rightHashCode);
}

[Fact(DisplayName = "PublicKey.GetHash should produce the expected result")]
public void PublicKeyGetHashShouldProduceTheExpectedResult()
{
// Given
const string expected = "9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a";
PublicKey publicKey = new TestPublicKey([1, 2, 3, 4]);

// When
Hash hash = publicKey.GetHash(SHA256.Create());
string actual = hash.ToString();

// then
Assert.Equal(expected, actual);
}
}
51 changes: 51 additions & 0 deletions OnixLabs.Security.Cryptography/NamedHash.Parse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020 ONIXLabs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Diagnostics.CodeAnalysis;

namespace OnixLabs.Security.Cryptography;

public readonly partial record struct NamedHash
{
public static NamedHash Parse(string value, IFormatProvider? provider = null) => Parse(value.AsSpan(), provider);

Check warning on line 22 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.Parse(string, IFormatProvider?)'

Check warning on line 22 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.Parse(string, IFormatProvider?)'

Check warning on line 22 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.Parse(string, IFormatProvider?)'

public static NamedHash Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)

Check warning on line 24 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.Parse(ReadOnlySpan<char>, IFormatProvider?)'

Check warning on line 24 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.Parse(ReadOnlySpan<char>, IFormatProvider?)'

Check warning on line 24 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.Parse(ReadOnlySpan<char>, IFormatProvider?)'
{
if (TryParse(value, provider, out NamedHash result)) return result;
throw new FormatException($"The input string '{value}' was not in a correct format.");
}

public static bool TryParse([NotNullWhen(true)] string? value, IFormatProvider? provider, out NamedHash result) => TryParse(value.AsSpan(), provider, out result);

Check warning on line 30 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.TryParse(string?, IFormatProvider?, out NamedHash)'

Check warning on line 30 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.TryParse(string?, IFormatProvider?, out NamedHash)'

public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out NamedHash result)

Check warning on line 32 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.TryParse(ReadOnlySpan<char>, IFormatProvider?, out NamedHash)'

Check warning on line 32 in OnixLabs.Security.Cryptography/NamedHash.Parse.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NamedHash.TryParse(ReadOnlySpan<char>, IFormatProvider?, out NamedHash)'
{
result = default;

int index = value.IndexOf(Separator);

if (index < 0) return false;

ReadOnlySpan<char> name = value[..index];
ReadOnlySpan<char> data = value[(index + 1)..];

if (name.IsEmpty || data.IsEmpty) return false;

bool isDecoded = Hash.TryParse(data, provider, out Hash hash);

result = new NamedHash(hash, name.ToString());

return isDecoded;
}
}
4 changes: 2 additions & 2 deletions OnixLabs.Security.Cryptography/NamedHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Security.Cryptography;
using OnixLabs.Core;

namespace OnixLabs.Security.Cryptography;

/// <summary>
/// Represents a named cryptographic hash.
/// </summary>
public readonly partial record struct NamedHash : ICryptoPrimitive<NamedHash>, ISpanBinaryConvertible
public readonly partial record struct NamedHash : ICryptoPrimitive<NamedHash>, ISpanParsable<NamedHash>
{
private const string Separator = ":";
private const string HashAlgorithmNameNullOrWhiteSpace = "Hash algorithm name must not be null or whitespace.";
Expand Down
4 changes: 1 addition & 3 deletions OnixLabs.Security.Cryptography/NamedPublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using OnixLabs.Core;

namespace OnixLabs.Security.Cryptography;

/// <summary>
/// Represents a named cryptographic public key.
/// </summary>
public readonly partial record struct NamedPublicKey : ICryptoPrimitive<NamedPublicKey>, ISpanBinaryConvertible
public readonly partial record struct NamedPublicKey : ICryptoPrimitive<NamedPublicKey>
{
private const string Separator = ":";
private const string KeyAlgorithmNameNullOrWhiteSpace = "Key algorithm name must not be null or whitespace.";
Expand Down
27 changes: 27 additions & 0 deletions OnixLabs.Security.Cryptography/PublicKey.Get.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020 ONIXLabs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Security.Cryptography;

namespace OnixLabs.Security.Cryptography;

public abstract partial class PublicKey
{
/// <summary>
/// Gets a <see cref="Hash"/> representation of the current <see cref="PublicKey"/> instance.
/// </summary>
/// <param name="algorithm">The <see cref="HashAlgorithm"/> that will be used to hash the public key data.</param>
/// <returns>Returns a <see cref="Hash"/> representation of the current <see cref="PublicKey"/> instance.</returns>
public Hash GetHash(HashAlgorithm algorithm) => Hash.Compute(algorithm, KeyData);
}
Loading