Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: base64 and byte array #131

Merged
merged 2 commits into from
Sep 30, 2024
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
27 changes: 27 additions & 0 deletions src/BB84.Extensions/ByteExtensions.FromBase64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace BB84.Extensions;

public static partial class ByteExtensions
{
/// <summary>
/// Converts the specified <paramref name="value"/>, which encodes binary data as base-64 digits,
/// to an equivalent 8-bit unsigned integer array.
/// </summary>
/// <param name="value">The string to convert.</param>
/// <returns>An array of 8-bit unsigned integers that is equivalent to <paramref name="value"/>.</returns>
/// <exception cref="ArgumentException"></exception>
public static byte[] FromBase64(this string value)
{
if (value.IsNullOrEmpty())
return [];

value = value.Trim();
bool isValidBase64 = (value.Length % 4 == 0) && Base64Regex.IsMatch(value);

if (isValidBase64.Equals(false))
throw new ArgumentException($"{value} is not valid base64");

byte[] bytes = Convert.FromBase64String(value);

return bytes;
}
}
13 changes: 13 additions & 0 deletions src/BB84.Extensions/ByteExtensions.ToBase64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BB84.Extensions;

public static partial class ByteExtensions
{
/// <summary>
/// Converts an array of 8-bit unsigned integers to its equivalent string representation
/// that is encoded with base-64 digits.
/// </summary>
/// <param name="byteArray">An array of 8-bit unsigned integers.</param>
/// <returns>The string representation, in base 64, of the contents of <paramref name="byteArray"/>.</returns>
public static string ToBase64(this byte[] byteArray)
=> byteArray.Length.Equals(0) ? string.Empty : Convert.ToBase64String(byteArray);
}
15 changes: 13 additions & 2 deletions src/BB84.Extensions/ByteExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
namespace BB84.Extensions;
using System.Text.RegularExpressions;

namespace BB84.Extensions;

/// <summary>
/// The byte extensions class.
/// </summary>
public static partial class ByteExtensions
{ }
{
#if NET7_0_OR_GREATER
private static readonly Regex Base64Regex = GeneratedBase64Regex();

[GeneratedRegex(@"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None)]
private static partial Regex GeneratedBase64Regex();
#else
private static readonly Regex Base64Regex = new(@"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
#endif
}
17 changes: 3 additions & 14 deletions src/BB84.Extensions/StringExtensions.FromBase64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,13 @@ public static partial class StringExtensions
/// <summary>
/// Decodes the provided string from a base64 string.
/// </summary>
/// <param name="stringValue">The input string to work with.</param>
/// <param name="value">The input string to work with.</param>
/// <param name="encoding">The encoding to use.</param>
/// <returns>The decoded string.</returns>
/// <exception cref="ArgumentException">The provided string is not a valid base64 string.</exception>
public static string FromBase64(this string stringValue, Encoding? encoding = null)
public static string FromBase64(this string value, Encoding encoding)
{
if (stringValue.IsNullOrEmpty())
return string.Empty;

// Check for valid base64
stringValue = stringValue.Trim();
bool isValidBase64 = (stringValue.Length % 4 == 0) && Base64Regex.IsMatch(stringValue);

if (isValidBase64.Equals(false))
throw new ArgumentException($"{stringValue} is not valid base64");

byte[] buffer = Convert.FromBase64String(stringValue);
encoding ??= Encoding.Default;
byte[] buffer = value.FromBase64();
return encoding.GetString(buffer);
}
}
5 changes: 2 additions & 3 deletions src/BB84.Extensions/StringExtensions.ToBase64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ public static partial class StringExtensions
/// <param name="stringValue">The input string to work with.</param>
/// <param name="encoding">The encoding to use.</param>
/// <returns>The encoded string.</returns>
public static string ToBase64(this string stringValue, Encoding? encoding = null)
public static string ToBase64(this string stringValue, Encoding encoding)
{
if (stringValue.IsNullOrWhiteSpace())
return string.Empty;

encoding ??= Encoding.Default;
byte[] buffer = encoding.GetBytes(stringValue);
return Convert.ToBase64String(buffer);
return buffer.ToBase64();
}
}
5 changes: 0 additions & 5 deletions src/BB84.Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ namespace BB84.Extensions;
public static partial class StringExtensions
{
#if NET7_0_OR_GREATER
private static readonly Regex Base64Regex = GeneratedBase64Regex();
private static readonly Regex WhitespaceRegex = GeneratedWhitespaceRegex();
private static readonly Regex LinebreakRegex = GeneratedLinebreakRegex();

[GeneratedRegex(@"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None)]
private static partial Regex GeneratedBase64Regex();

[GeneratedRegex(@"\s+", RegexOptions.None)]
private static partial Regex GeneratedWhitespaceRegex();

[GeneratedRegex(@"(\r\n|\r|\n)", RegexOptions.None)]
private static partial Regex GeneratedLinebreakRegex();
#else
private static readonly Regex Base64Regex = new(@"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
private static readonly Regex WhitespaceRegex = new(@"\s+", RegexOptions.None);
private static readonly Regex LinebreakRegex = new(@"(\r\n|\r|\n)", RegexOptions.None);
#endif
Expand Down
12 changes: 12 additions & 0 deletions tests/BB84.ExtensionsTests/ByteExtensionsTests.FromBase64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BB84.Extensions;

namespace BB84.ExtensionsTests;

public sealed partial class ByteExtensionsTests
{
[DataTestMethod]
[DataRow(new byte[] { 85, 110, 105, 116, 84, 101, 115, 116 }, "VW5pdFRlc3Q=")]
[DataRow(new byte[0], "")]
public void FromBase64Test(byte[] expected, string value)
=> Assert.IsTrue(expected.SequenceEqual(value.FromBase64()));
}
12 changes: 12 additions & 0 deletions tests/BB84.ExtensionsTests/ByteExtensionsTests.ToBase64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BB84.Extensions;

namespace BB84.ExtensionsTests;

public sealed partial class ByteExtensionsTests
{
[DataTestMethod]
[DataRow("VW5pdFRlc3Q=", new byte[] { 85, 110, 105, 116, 84, 101, 115, 116 })]
[DataRow("", new byte[0])]
public void ToBase64Test(string expected, byte[] bytes)
=> Assert.AreEqual(expected, bytes.ToBase64());
}
44 changes: 15 additions & 29 deletions tests/BB84.ExtensionsTests/StringExtensionsTests.FromBase64.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
using BB84.Extensions;
using System.Text;

using BB84.Extensions;

namespace BB84.ExtensionsTests;

public sealed partial class StringExtensionsTests
{
[TestMethod]
public void FromBase64Test()
{
string value = "VW5pdFRlc3Q=";

string result = value.FromBase64();

Assert.AreNotEqual(value, result);
Assert.AreEqual("UnitTest", result);
}

[TestMethod]
public void FromBase64EmptyTest()
{
string value = string.Empty;

string result = value.FromBase64();

Assert.AreEqual(value, result);
}

[TestMethod]
[DataTestMethod]
[DataRow("UnitTest", "VW5pdFRlc3Q=")]
[DataRow("", "")]
public void FromBase64Test(string expected, string value)
=> Assert.AreEqual(expected, value.FromBase64(Encoding.UTF8));

[DataTestMethod]
[DataRow("%")]
[DataRow("$")]
[DataRow("#")]
[ExpectedException(typeof(ArgumentException))]
public void FromBase64ExceptionTest()
{
string value = "%$#";

_ = value.FromBase64();
}
public void FromBase64ExceptionTest(string value)
=> value.FromBase64();
}
29 changes: 8 additions & 21 deletions tests/BB84.ExtensionsTests/StringExtensionsTests.ToBase64.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
using BB84.Extensions;
using System.Text;

using BB84.Extensions;

namespace BB84.ExtensionsTests;

public sealed partial class StringExtensionsTests
{
[TestMethod]
public void ToBase64Test()
{
string value = "UnitTest";

string result = value.ToBase64();

Assert.AreNotEqual(value, result);
Assert.AreEqual("VW5pdFRlc3Q=", result);
}

[TestMethod]
public void ToBase64EmptyTest()
{
string value = string.Empty;

string result = value.ToBase64();

Assert.AreEqual(value, result);
}
[DataTestMethod]
[DataRow("VW5pdFRlc3Q=", "UnitTest")]
[DataRow("", "")]
public void ToBase64Test(string expected, string value)
=> Assert.AreEqual(expected, value.ToBase64(Encoding.UTF8));
}