Skip to content

Commit

Permalink
♻️ Changed function names
Browse files Browse the repository at this point in the history
  • Loading branch information
Marthijn van den Heuvel committed Oct 4, 2024
1 parent 33f58a7 commit 7029073
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 31 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# Sidio.Text.Base32
Base32 encoding and decoding in .NET
A simple .NET package that converts a byte array to a Base32 string and vice versa. Because the latest framework
features are used, it requires .NET 8.0 or higher.

[![build](https://github.com/marthijn/Sidio.Text.Base32/actions/workflows/build.yml/badge.svg)](https://github.com/marthijn/Sidio.Text.Base32/actions/workflows/build.yml)
[![NuGet Version](https://img.shields.io/nuget/v/Sidio.Text.Base32)](https://www.nuget.org/packages/Sidio.Text.Base32/)
[![Coverage Status](https://coveralls.io/repos/github/marthijn/Sidio.Text.Base32/badge.svg?branch=main)](https://coveralls.io/github/marthijn/Sidio.Text.Base32?branch=main)

# Usage
## Encode
```csharp
var myString = "foobar";
var bytes = Encoding.UTF8.GetBytes(myString);
var base32 = Base32.Encode(bytes);
```

## Decode
```csharp
var base32 = "MZXW6YTBOI======";
var bytes = Base32.Decode(base32);
var myString = Encoding.UTF8.GetString(bytes);
```

## Encode hex
```csharp
var myString = "foobar";
var bytes = Encoding.UTF8.GetBytes(myString);
var base32 = Base32.EncodeHex(bytes);
```

## Decode hex
```csharp
var base32 = "CPNMUOJ1E8======";
var bytes = Base32.DecodeHex(base32);
var myString = Encoding.UTF8.GetString(bytes);
```

# References
* [RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648)
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Sidio.Text.Base32.Tests;

public partial class Base32EncodingTests
public partial class Base32Tests
{
[Theory]
[ClassData(typeof(Base32HexTestVectors))]
public void FromHexString_ReturnsByteArray(string input, string base32)
public void DecodeHex_ReturnsByteArray(string input, string base32)
{
// act
var result = Base32Encoding.FromHexString(base32);
var result = Base32.DecodeHex(base32);

// assert
result.Should().NotBeNull();
Expand All @@ -21,13 +21,13 @@ public void FromHexString_ReturnsByteArray(string input, string base32)

[Theory]
[ClassData(typeof(Base32HexTestVectors))]
public void ToHexString_ReturnsBase32HexString(string input, string base32)
public void EncodeHex_ReturnsBase32HexString(string input, string base32)
{
// arrange
var bytes = Encoding.UTF8.GetBytes(input);

// act
var result = Base32Encoding.ToHexString(bytes);
var result = Base32.EncodeHex(bytes);

// assert
result.Should().NotBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Sidio.Text.Base32.Tests;

public partial class Base32EncodingTests
public partial class Base32Tests
{
[Theory]
[ClassData(typeof(Base32TestVectors))]
public void FromString_ReturnsByteArray(string input, string base32)
public void Decode_ReturnsByteArray(string input, string base32)
{
// act
var result = Base32Encoding.FromString(base32);
var result = Base32.Decode(base32);

// assert
result.Should().NotBeNull();
Expand All @@ -21,13 +21,13 @@ public void FromString_ReturnsByteArray(string input, string base32)

[Theory]
[ClassData(typeof(Base32TestVectors))]
public void ToString_ReturnsBase32String(string input, string base32)
public void Encode_ReturnsBase32String(string input, string base32)
{
// arrange
var bytes = Encoding.UTF8.GetBytes(input);

// act
var result = Base32Encoding.ToString(bytes);
var result = Base32.Encode(bytes);

// assert
result.Should().NotBeNull();
Expand Down
14 changes: 10 additions & 4 deletions src/Sidio.Text.Base32.Tests/Sidio.Text.Base32.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Sidio.Text.Base32;

public static partial class Base32Encoding
public static partial class Base32
{
private static readonly char[] Base32HexTable =
[
Expand All @@ -15,20 +15,20 @@ public static partial class Base32Encoding
/// </summary>
/// <param name="input">The input string.</param>
/// <returns>A <see cref="byte"/> array.</returns>
public static byte[] FromHexString(string input)
public static byte[] DecodeHex(string input)
{
ArgumentNullException.ThrowIfNull(input);
return input.Length == 0 ? [] : FromString(input.AsSpan(), Base32HexDecodeMap);
return input.Length == 0 ? [] : Decode(input.AsSpan(), Base32HexDecodeMap);
}

/// <summary>
/// Converts a byte array to a base32 hex string.
/// </summary>
/// <param name="inArray">The input array.</param>
/// <returns>A base32 <see cref="string"/>.</returns>
public static string ToHexString(byte[] inArray)
public static string EncodeHex(byte[] inArray)
{
ArgumentNullException.ThrowIfNull(inArray);
return inArray.Length == 0 ? string.Empty : ToString(new ReadOnlySpan<byte>(inArray), Base32HexTable);
return inArray.Length == 0 ? string.Empty : Encode(new ReadOnlySpan<byte>(inArray), Base32HexTable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// The base32 encoding class.
/// </summary>
public static partial class Base32Encoding
public static partial class Base32
{
private const char UnitSeparator = (char)0x1F;
private const char MaxByte = (char)0xFF;
Expand All @@ -21,30 +21,30 @@ public static partial class Base32Encoding
/// </summary>
/// <param name="input">The input string.</param>
/// <returns>A <see cref="byte"/> array.</returns>
public static byte[] FromString(string input)
public static byte[] Decode(string input)
{
ArgumentNullException.ThrowIfNull(input);
return input.Length == 0 ? [] : FromString(input.AsSpan(), Base32DecodeMap);
return input.Length == 0 ? [] : Decode(input.AsSpan(), Base32DecodeMap);
}

/// <summary>
/// Converts a byte array to a base32 string.
/// </summary>
/// <param name="inArray">The input array.</param>
/// <returns>A base32 <see cref="string"/>.</returns>
public static string ToString(byte[] inArray)
public static string Encode(byte[] inArray)
{
ArgumentNullException.ThrowIfNull(inArray);
return inArray.Length == 0 ? string.Empty : ToString(new ReadOnlySpan<byte>(inArray), Base32Table);
return inArray.Length == 0 ? string.Empty : Encode(new ReadOnlySpan<byte>(inArray), Base32Table);
}

private static byte[] FromString(ReadOnlySpan<char> inputSpan, int[] decodeMap)
private static byte[] Decode(ReadOnlySpan<char> inputSpan, int[] decodeMap)
{
// remove padding '=' characters from the end of the input
var trimmedInput = inputSpan.TrimEnd('=');

// calculate the expected output byte array length
// each 8 characters of Base32 results in 5 bytes
// each 8 characters of base32 results in 5 bytes
var outputLength = (trimmedInput.Length * 5) / 8;

// stack allocation for small arrays
Expand Down Expand Up @@ -82,7 +82,7 @@ private static byte[] FromString(ReadOnlySpan<char> inputSpan, int[] decodeMap)
return outputSpan.ToArray();
}

private static string ToString(ReadOnlySpan<byte> inArray, char[] base32Table)
private static string Encode(ReadOnlySpan<byte> inArray, char[] base32Table)
{
// calculate the length of the output: Base32 encoding is 8/5 times the size of the input
var outputLength = ((inArray.Length * 8) + 4) / 5;
Expand All @@ -98,14 +98,14 @@ private static string ToString(ReadOnlySpan<byte> inArray, char[] base32Table)
var bitCount = 0;
var outputIndex = 0;

// process each byte in inputSpan
// process each byte in inArray
foreach (var b in inArray)
{
// shift the buffer left by 8 bits and add the byte value to the buffer
bitBuffer = (bitBuffer << 8) | b;
bitCount += 8;

// while we have 5 or more bits in the buffer, process a Base32 character
// while we have 5 or more bits in the buffer, process a base32 character
while (bitCount >= 5)
{
// extract the top 5 bits from the buffer
Expand Down
2 changes: 1 addition & 1 deletion src/Sidio.Text.Base32/Sidio.Text.Base32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>Base32 encoding and decoding in .NET.</Description>
<PackageTags>base32 encoding decoding text</PackageTags>
<PackageTags>base32 encoding decoding text rfc4648</PackageTags>
<PackageReleaseNotes>https://github.com/marthijn/Sidio.Text.Base32/releases</PackageReleaseNotes>
<PackageIcon>icon.png</PackageIcon>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down

0 comments on commit 7029073

Please sign in to comment.