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/security-token #64

Merged
merged 4 commits into from
Jun 9, 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
16 changes: 16 additions & 0 deletions OnixLabs.Core.UnitTests/StringExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,20 @@ public void WrapShouldProduceExpectedResult(string before, string value, string
// Then
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "String.ToEscapedString should produce the expected result")]
[InlineData("\n", @"\n")]
[InlineData("\r", @"\r")]
[InlineData("\t", @"\t")]
[InlineData("\"", @"\""")]
[InlineData("\'", @"\'")]
[InlineData("\\", @"\\")]
public void ToEscapedStringShouldProduceExpectedResult(string value, string expected)
{
// When
string actual = value.ToEscapedString();

// Then
Assert.Equal(expected, actual);
}
}
42 changes: 42 additions & 0 deletions OnixLabs.Core/Extensions.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Globalization;
using System.Linq;
using System.Text;
using OnixLabs.Core.Text;

namespace OnixLabs.Core;

Expand Down Expand Up @@ -308,4 +309,45 @@ public static bool TryCopyTo(this string value, Span<char> destination, out int
/// <param name="after">The <see cref="String"/> that should succeed the current <see cref="String"/> instance.</param>
/// <returns>Returns a new <see cref="String"/> instance representing the current <see cref="String"/> instance, wrapped between the specified before and after <see cref="String"/> instances.</returns>
public static string Wrap(this string value, string before, string after) => $"{before}{value}{after}";

/// <summary>
/// Returns a <see cref="string"/> with all escape characters formatted as escape character literals.
/// </summary>
/// <param name="value">The current <see cref="String"/> value.</param>
/// <returns>Returns a <see cref="string"/> with all escape characters formatted as escape character literals.</returns>
public static string ToEscapedString(this string value)
{
StringBuilder result = new();

foreach (char c in value)
{
switch (c)
{
case '\n':
result.AppendEscaped('n');
break;
case '\r':
result.AppendEscaped('r');
break;
case '\t':
result.AppendEscaped('t');
break;
case '\"':
result.AppendEscaped('"');
break;
case '\'':
result.AppendEscaped('\'');
break;
case '\\':
result.AppendEscaped('\\');
break;
default:
if (char.IsControl(c)) result.Append($"\\u{(int)c:X4}");
else result.Append(c);
break;
}
}

return result.ToString();
}
}
8 changes: 5 additions & 3 deletions OnixLabs.Core/OnixLabs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
<Title>OnixLabs.Core</Title>
<Authors>ONIXLabs</Authors>
<Description>ONIXLabs Core API for .NET</Description>
<AssemblyVersion>8.0.0</AssemblyVersion>
<AssemblyVersion>8.9.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.8.0</PackageVersion>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageVersion>8.9.0</PackageVersion>
</PropertyGroup>
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand All @@ -31,6 +30,9 @@
<PropertyGroup Condition="'$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
14 changes: 12 additions & 2 deletions OnixLabs.Core/Text/Extensions.StringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,30 @@ namespace OnixLabs.Core.Text;
[EditorBrowsable(EditorBrowsableState.Never)]
public static class StringBuilderExtensions
{
private const char EscapeSequence = '\\';

/// <summary>
/// Appends the specified values to the current <see cref="StringBuilder"/>.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder"/> to append to.</param>
/// <param name="values">The values to append.</param>
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified items appended.</returns>
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values appended.</returns>
public static StringBuilder Append(this StringBuilder builder, params object[] values) => builder.Append(values.JoinToString(string.Empty));

/// <summary>
/// Appends the specified value, prefixed with the escape sequence to the current <see cref="StringBuilder"/>.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder"/> to append to.</param>
/// <param name="value">The value to append.</param>
/// <returns>Returns the current <see cref="StringBuilder"/> with the escape sequence and specified value appended.</returns>
internal static StringBuilder AppendEscaped(this StringBuilder builder, char value) => builder.Append(EscapeSequence).Append(value);

/// <summary>
/// Prepends the specified values to the current <see cref="StringBuilder"/>.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder"/> to prepend to.</param>
/// <param name="values">The values to prepend.</param>
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified items prepended.</returns>
/// <returns>Returns the current <see cref="StringBuilder"/> with the specified values prepended.</returns>
public static StringBuilder Prepend(this StringBuilder builder, params object[] values) => builder.Insert(0, values.JoinToString(string.Empty));

/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions OnixLabs.Numerics/OnixLabs.Numerics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
<Title>OnixLabs.Numerics</Title>
<Authors>ONIXLabs</Authors>
<Description>ONIXLabs Numerics API for .NET</Description>
<AssemblyVersion>8.0.0</AssemblyVersion>
<AssemblyVersion>8.9.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.8.0</PackageVersion>
<PackageVersion>8.9.0</PackageVersion>
<LangVersion>12</LangVersion>
<PackageLicenseUrl></PackageLicenseUrl>
</PropertyGroup>
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand All @@ -31,6 +30,9 @@
<PropertyGroup Condition="'$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
1 change: 1 addition & 0 deletions OnixLabs.Playground/OnixLabs.Playground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ProjectReference Include="..\OnixLabs.Core\OnixLabs.Core.csproj"/>
<ProjectReference Include="..\OnixLabs.Numerics\OnixLabs.Numerics.csproj"/>
<ProjectReference Include="..\OnixLabs.Security.Cryptography\OnixLabs.Security.Cryptography.csproj"/>
<ProjectReference Include="..\OnixLabs.Security\OnixLabs.Security.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="OnixLabs.Core.Preconditions" Static="True"/>
Expand Down
22 changes: 22 additions & 0 deletions OnixLabs.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

namespace OnixLabs.Playground;

internal static class Program
{
private static void Main()
{
int[] lengths = [1, 2, 4, 8, 16, 32, 64, 128];
int[] seeds = [0, 4, 7, 9, 123, 256, 721, 999];

foreach (int length in lengths)
{
foreach (int seed in seeds)
{
string token = SecurityTokenBuilder
.CreatePseudoRandom(length, seed)
.UseAlphaNumericCharacters()
.UseExtendedSpecialCharacters()
.ToSecurityToken()
.ToString()
.ToEscapedString();

Console.WriteLine($"[InlineData({length}, {seed}, \"{token}\")]");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
<Title>OnixLabs.Security.Cryptography</Title>
<Authors>ONIXLabs</Authors>
<Description>ONIXLabs Cryptography API for .NET</Description>
<AssemblyVersion>8.0.0</AssemblyVersion>
<AssemblyVersion>8.9.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.8.0</PackageVersion>
<PackageVersion>8.9.0</PackageVersion>
<LangVersion>12</LangVersion>
<PackageLicenseUrl></PackageLicenseUrl>
</PropertyGroup>
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand All @@ -31,6 +30,9 @@
<PropertyGroup Condition="'$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
27 changes: 27 additions & 0 deletions OnixLabs.Security.UnitTests/OnixLabs.Security.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<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"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OnixLabs.Security\OnixLabs.Security.csproj" />
</ItemGroup>

</Project>
Loading
Loading