Skip to content

Commit a712c70

Browse files
update/cryptography
Updated cryptography APIs to include cleaner constructors, fewer allocations, better parsing, and cleaner documentation.
1 parent a4c8604 commit a712c70

File tree

100 files changed

+891
-2413
lines changed

Some content is hidden

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

100 files changed

+891
-2413
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 System.ComponentModel;
17+
18+
namespace OnixLabs.Core;
19+
20+
/// <summary>
21+
/// Provides extension methods for <see cref="ISpanParsable{TSelf}"/> instances.
22+
/// </summary>
23+
[EditorBrowsable(EditorBrowsableState.Never)]
24+
public static class ISpanParsableExtensions
25+
{
26+
/// <summary>
27+
/// Provides extension methods for <see cref="ISpanParsable{TSelf}"/> instances.
28+
/// </summary>
29+
/// <typeparam name="T">The underlying <see cref="ISpanParsable{TSelf}"/> type.</typeparam>
30+
extension<T>(ISpanParsable<T>) where T : ISpanParsable<T>
31+
{
32+
/// <summary>
33+
/// Tries to parse the specified <paramref name="value"/>, or throws a <see cref="FormatException"/> if the value cannot be parsed.
34+
/// </summary>
35+
/// <param name="value">The value to parse.</param>
36+
/// <param name="provider">An object that provides culture-specific formatting information about the value.</param>
37+
/// <returns>Returns a new instance of <typeparamref name="T"/> parsed from the specified value.</returns>
38+
/// <exception cref="FormatException">If the value cannot be parsed.</exception>
39+
public static T ParseOrThrow(ReadOnlySpan<char> value, IFormatProvider? provider = null)
40+
{
41+
// ReSharper disable once ConvertIfStatementToReturnStatement
42+
if (T.TryParse(value, provider, out T? result)) return result;
43+
throw new FormatException($"The input string '{value}' was not in a correct format.");
44+
}
45+
}
46+
}

OnixLabs.Core/Text/Base16.Parse.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ namespace OnixLabs.Core.Text;
1919
public readonly partial struct Base16
2020
{
2121
/// <inheritdoc/>
22-
public static Base16 Parse(string value, IFormatProvider? provider = null) => Parse(value.AsSpan(), provider);
22+
public static Base16 Parse(string value, IFormatProvider? provider = null) =>
23+
Parse(value.AsSpan(), provider);
2324

2425
/// <inheritdoc/>
25-
public static Base16 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) => new(IBaseCodec.Base16.Decode(value, provider));
26+
public static Base16 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) =>
27+
new(IBaseCodec.Base16.Decode(value, provider));
2628

2729
/// <inheritdoc/>
28-
public static bool TryParse(string? value, IFormatProvider? provider, out Base16 result) => TryParse(value.AsSpan(), provider, out result);
30+
public static bool TryParse(string? value, IFormatProvider? provider, out Base16 result) =>
31+
TryParse(value.AsSpan(), provider, out result);
2932

3033
/// <inheritdoc/>
3134
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out Base16 result)

OnixLabs.Core/Text/Base32.Parse.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ namespace OnixLabs.Core.Text;
1919
public readonly partial struct Base32
2020
{
2121
/// <inheritdoc/>
22-
public static Base32 Parse(string value, IFormatProvider? provider = null) => Parse(value.AsSpan(), provider);
22+
public static Base32 Parse(string value, IFormatProvider? provider = null) =>
23+
Parse(value.AsSpan(), provider);
2324

2425
/// <inheritdoc/>
25-
public static Base32 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) => new(IBaseCodec.Base32.Decode(value, provider));
26+
public static Base32 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) =>
27+
new(IBaseCodec.Base32.Decode(value, provider));
2628

2729
/// <inheritdoc/>
28-
public static bool TryParse(string? value, IFormatProvider? provider, out Base32 result) => TryParse(value.AsSpan(), provider, out result);
30+
public static bool TryParse(string? value, IFormatProvider? provider, out Base32 result) =>
31+
TryParse(value.AsSpan(), provider, out result);
2932

3033
/// <inheritdoc/>
3134
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out Base32 result)

OnixLabs.Core/Text/Base58.Parse.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ namespace OnixLabs.Core.Text;
1919
public readonly partial struct Base58
2020
{
2121
/// <inheritdoc/>
22-
public static Base58 Parse(string value, IFormatProvider? provider = null) => Parse(value.AsSpan(), provider);
22+
public static Base58 Parse(string value, IFormatProvider? provider = null) =>
23+
Parse(value.AsSpan(), provider);
2324

2425
/// <inheritdoc/>
25-
public static Base58 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) => new(IBaseCodec.Base58.Decode(value, provider));
26+
public static Base58 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) =>
27+
new(IBaseCodec.Base58.Decode(value, provider));
2628

2729
/// <inheritdoc/>
28-
public static bool TryParse(string? value, IFormatProvider? provider, out Base58 result) => TryParse(value.AsSpan(), provider, out result);
30+
public static bool TryParse(string? value, IFormatProvider? provider, out Base58 result) =>
31+
TryParse(value.AsSpan(), provider, out result);
2932

3033
/// <inheritdoc/>
3134
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out Base58 result)

OnixLabs.Core/Text/Base64.Parse.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ namespace OnixLabs.Core.Text;
1919
public readonly partial struct Base64
2020
{
2121
/// <inheritdoc/>
22-
public static Base64 Parse(string value, IFormatProvider? provider = null) => Parse(value.AsSpan(), provider);
22+
public static Base64 Parse(string value, IFormatProvider? provider = null) =>
23+
Parse(value.AsSpan(), provider);
2324

2425
/// <inheritdoc/>
25-
public static Base64 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) => new(IBaseCodec.Base64.Decode(value, provider));
26+
public static Base64 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) =>
27+
new(IBaseCodec.Base64.Decode(value, provider));
2628

2729
/// <inheritdoc/>
28-
public static bool TryParse(string? value, IFormatProvider? provider, out Base64 result) => TryParse(value.AsSpan(), provider, out result);
30+
public static bool TryParse(string? value, IFormatProvider? provider, out Base64 result) =>
31+
TryParse(value.AsSpan(), provider, out result);
2932

3033
/// <inheritdoc/>
3134
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out Base64 result)

OnixLabs.Core/Text/Extensions.Encoding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace OnixLabs.Core.Text;
2222
/// Provides extension methods <see cref="Encoding"/> instances.
2323
/// </summary>
2424
[EditorBrowsable(EditorBrowsableState.Never)]
25-
internal static class EncodingExtensions
25+
public static class EncodingExtensions
2626
{
2727
/// <summary>
2828
/// Provides extension methods <see cref="Encoding"/> instances.

OnixLabs.Security.Cryptography/DigitalSignature.Convertible.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,28 @@ namespace OnixLabs.Security.Cryptography;
1919

2020
public readonly partial struct DigitalSignature
2121
{
22-
/// <summary>
23-
/// Gets the underlying <see cref="byte"/> array representation of the current <see cref="DigitalSignature"/> instance as a new <see cref="ReadOnlyMemory{T}"/> instance.
24-
/// </summary>
25-
/// <returns>Return the underlying <see cref="byte"/> array representation of the current <see cref="DigitalSignature"/> instance as a new <see cref="ReadOnlyMemory{T}"/> instance.</returns>
22+
/// <inheritdoc/>
2623
public ReadOnlyMemory<byte> AsReadOnlyMemory() => value;
2724

28-
/// <summary>
29-
/// Gets the underlying <see cref="byte"/> array representation of the current <see cref="DigitalSignature"/> instance as a new <see cref="ReadOnlySpan{T}"/> instance.
30-
/// </summary>
31-
/// <returns>Return the underlying <see cref="byte"/> array representation of the current <see cref="DigitalSignature"/> instance as a new <see cref="ReadOnlySpan{T}"/> instance.</returns>
25+
/// <inheritdoc/>
3226
public ReadOnlySpan<byte> AsReadOnlySpan() => value;
3327

3428
/// <summary>
35-
/// Create a new <see cref="DigitalSignature"/> instance from the specified <see cref="byte"/> array.
29+
/// Creates a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlySpan{T}"/> value.
3630
/// </summary>
3731
/// <param name="value">The value from which to create a new <see cref="DigitalSignature"/> instance.</param>
38-
/// <returns>Returns a new <see cref="DigitalSignature"/> instance from the specified <see cref="byte"/> array.</returns>
39-
public static implicit operator DigitalSignature(byte[] value) => new(value);
32+
/// <returns>Returns a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlySpan{T}"/> value.</returns>
33+
public static implicit operator DigitalSignature(ReadOnlySpan<byte> value) => new(value);
4034

4135
/// <summary>
42-
/// Create a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlySpan{T}"/> value.
36+
/// Creates a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlyMemory{T}"/> value.
4337
/// </summary>
4438
/// <param name="value">The value from which to create a new <see cref="DigitalSignature"/> instance.</param>
45-
/// <returns>Returns a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlySpan{T}"/> value.</returns>
46-
public static implicit operator DigitalSignature(ReadOnlySpan<byte> value) => new(value);
39+
/// <returns>Returns a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlyMemory{T}"/> value.</returns>
40+
public static implicit operator DigitalSignature(ReadOnlyMemory<byte> value) => new(value);
4741

4842
/// <summary>
49-
/// Create a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlySequence{T}"/> value.
43+
/// Creates a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlySequence{T}"/> value.
5044
/// </summary>
5145
/// <param name="value">The value from which to create a new <see cref="DigitalSignature"/> instance.</param>
5246
/// <returns>Returns a new <see cref="DigitalSignature"/> instance from the specified <see cref="ReadOnlySequence{T}"/> value.</returns>

OnixLabs.Security.Cryptography/DigitalSignature.Equatable.cs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,24 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using System.Collections.Generic;
1615
using OnixLabs.Core.Linq;
1716

1817
namespace OnixLabs.Security.Cryptography;
1918

2019
public readonly partial struct DigitalSignature
2120
{
22-
/// <summary>
23-
/// Checks whether the current object is equal to another object of the same type.
24-
/// </summary>
25-
/// <param name="other">An object to compare with the current object.</param>
26-
/// <returns>Returns <see langword="true"/> if the current object is equal to the other parameter; otherwise, <see langword="false"/>.</returns>
21+
/// <inheritdoc/>
2722
public bool Equals(DigitalSignature other) => value.SequenceEqualOrNull(other.value);
2823

29-
/// <summary>
30-
/// Checks for equality between the current instance and another object.
31-
/// </summary>
32-
/// <param name="obj">The object to check for equality.</param>
33-
/// <returns>Returns <see langword="true"/> if the object is equal to the current instance; otherwise, <see langword="false"/>.</returns>
24+
/// <inheritdoc/>
3425
public override bool Equals(object? obj) => obj is DigitalSignature other && Equals(other);
3526

36-
/// <summary>
37-
/// Serves as a hash code function for the current instance.
38-
/// </summary>
39-
/// <returns>Returns a hash code for the current instance.</returns>
27+
/// <inheritdoc/>
4028
public override int GetHashCode() => value.GetContentHashCode();
4129

42-
/// <summary>
43-
/// Performs an equality comparison between two object instances.
44-
/// </summary>
45-
/// <param name="left">The left-hand instance to compare.</param>
46-
/// <param name="right">The right-hand instance to compare.</param>
47-
/// <returns>Returns <see langword="true"/> if the left-hand instance is equal to the right-hand instance; otherwise, <see langword="false"/>.</returns>
48-
public static bool operator ==(DigitalSignature left, DigitalSignature right) => EqualityComparer<DigitalSignature>.Default.Equals(left, right);
30+
/// <inheritdoc/>
31+
public static bool operator ==(DigitalSignature left, DigitalSignature right) => left.Equals(right);
4932

50-
/// <summary>
51-
/// Performs an inequality comparison between two object instances.
52-
/// </summary>
53-
/// <param name="left">The left-hand instance to compare.</param>
54-
/// <param name="right">The right-hand instance to compare.</param>
55-
/// <returns>Returns <see langword="true"/> if the left-hand instance is not equal to the right-hand instance; otherwise, <see langword="false"/>.</returns>
56-
public static bool operator !=(DigitalSignature left, DigitalSignature right) => !EqualityComparer<DigitalSignature>.Default.Equals(left, right);
33+
/// <inheritdoc/>
34+
public static bool operator !=(DigitalSignature left, DigitalSignature right) => !left.Equals(right);
5735
}

OnixLabs.Security.Cryptography/DigitalSignature.Parse.cs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,26 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using OnixLabs.Core;
1617
using OnixLabs.Core.Text;
1718

1819
namespace OnixLabs.Security.Cryptography;
1920

2021
public readonly partial struct DigitalSignature
2122
{
22-
/// <summary>
23-
/// Parses the specified <see cref="String"/> value into a <see cref="DigitalSignature"/> value.
24-
/// </summary>
25-
/// <param name="value">The value to parse.</param>
26-
/// <param name="provider">The format provider that will be used to decode the specified value.</param>
27-
/// <returns>Returns a new <see cref="DigitalSignature"/> instance, parsed from the specified <see cref="String"/> value.</returns>
28-
public static DigitalSignature Parse(string value, IFormatProvider? provider = null) => Parse(value.AsSpan(), provider);
23+
/// <inheritdoc/>
24+
public static DigitalSignature Parse(string value, IFormatProvider? provider = null) =>
25+
Parse(value.AsSpan(), provider);
2926

30-
/// <summary>
31-
/// Parses the specified <see cref="ReadOnlySpan{T}"/> value into a <see cref="DigitalSignature"/> value.
32-
/// </summary>
33-
/// <param name="value">The value to parse.</param>
34-
/// <param name="provider">The format provider that will be used to decode the specified value.</param>
35-
/// <returns>Returns a new <see cref="DigitalSignature"/> instance, parsed from the specified <see cref="ReadOnlySpan{T}"/> value.</returns>
36-
public static DigitalSignature Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)
37-
{
38-
if (TryParse(value, provider, out DigitalSignature result)) return result;
39-
throw new FormatException($"The input string '{value}' was not in a correct format.");
40-
}
27+
/// <inheritdoc/>
28+
public static DigitalSignature Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null) =>
29+
DigitalSignature.ParseOrThrow(value, provider);
4130

42-
/// <summary>
43-
/// Tries to parse the specified <see cref="String"/> value into a <see cref="DigitalSignature"/> value.
44-
/// </summary>
45-
/// <param name="value">The value to parse.</param>
46-
/// <param name="provider">The format provider that will be used to decode the specified value.</param>
47-
/// <param name="result">
48-
/// A new <see cref="DigitalSignature"/> instance, parsed from the specified <see cref="String"/> value, or the default
49-
/// <see cref="DigitalSignature"/> value if the specified <see cref="String"/> could not be parsed.
50-
/// </param>
51-
/// <returns>Returns <see langword="true"/> if the specified value was decoded successfully; otherwise, <see langword="false"/>.</returns>
52-
public static bool TryParse(string? value, IFormatProvider? provider, out DigitalSignature result) => TryParse(value.AsSpan(), provider, out result);
31+
/// <inheritdoc/>
32+
public static bool TryParse(string? value, IFormatProvider? provider, out DigitalSignature result) =>
33+
TryParse(value.AsSpan(), provider, out result);
5334

54-
/// <summary>
55-
/// Tries to parse the specified <see cref="ReadOnlySpan{T}"/> value into a <see cref="DigitalSignature"/> value.
56-
/// </summary>
57-
/// <param name="value">The value to parse.</param>
58-
/// <param name="provider">The format provider that will be used to decode the specified value.</param>
59-
/// <param name="result">
60-
/// A new <see cref="DigitalSignature"/> instance, parsed from the specified <see cref="ReadOnlySpan{T}"/> value, or the default
61-
/// <see cref="DigitalSignature"/> value if the specified <see cref="ReadOnlySpan{T}"/> could not be parsed.
62-
/// </param>
63-
/// <returns>Returns <see langword="true"/> if the specified value was decoded successfully; otherwise, <see langword="false"/>.</returns>
35+
/// <inheritdoc/>
6436
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out DigitalSignature result)
6537
{
6638
bool isDecoded = IBaseCodec.TryGetBytes(value, provider ?? Base16FormatProvider.Invariant, out byte[] bytes);

OnixLabs.Security.Cryptography/DigitalSignature.To.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,9 @@ namespace OnixLabs.Security.Cryptography;
1919

2020
public readonly partial struct DigitalSignature
2121
{
22-
/// <summary>
23-
/// Returns a <see cref="string"/> that represents the current object.
24-
/// </summary>
25-
/// <param name="provider">The format provider that will be used to determine the format of the string.</param>
26-
/// <returns>Returns a <see cref="string"/> that represents the current object.</returns>
22+
/// <inheritdoc/>
2723
public string ToString(IFormatProvider provider) => IBaseCodec.GetString(AsReadOnlySpan(), provider);
2824

29-
/// <summary>
30-
/// Returns a <see cref="string"/> that represents the current object.
31-
/// </summary>
32-
/// <returns>Returns a <see cref="string"/> that represents the current object.</returns>
25+
/// <inheritdoc/>
3326
public override string ToString() => ToString(Base16FormatProvider.Invariant);
3427
}

0 commit comments

Comments
 (0)