From b334f4adab4c51117708fbd67a05aee5becd2f7c Mon Sep 17 00:00:00 2001 From: sgrampone <30872877+sgrampone@users.noreply.github.com> Date: Fri, 21 Jul 2023 15:48:10 -0300 Subject: [PATCH] Base64UrlEncoder implementation (#849) --- .../SecurityAPICommonsNetCore.csproj | 3 +- .../Encoders/Base64UrlEncoder.cs | 161 ++++++++++++++++++ .../SecurityAPITestNetCore.csproj | 1 + .../encoders/TestBase64Url.cs | 68 ++++++++ 4 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 dotnet/src/extensions/SecurityAPI/dotnet/dotnetframework/SecurityAPICommons/Encoders/Base64UrlEncoder.cs create mode 100644 dotnet/src/extensions/SecurityAPI/test/dotnetframework/SecurityAPITest/SecurityAPICommons/encoders/TestBase64Url.cs diff --git a/dotnet/src/extensions/SecurityAPI/dotnet/dotnetcore/SecurityAPICommonsNetCore/SecurityAPICommonsNetCore.csproj b/dotnet/src/extensions/SecurityAPI/dotnet/dotnetcore/SecurityAPICommonsNetCore/SecurityAPICommonsNetCore.csproj index 28c91eaaa..387b02292 100644 --- a/dotnet/src/extensions/SecurityAPI/dotnet/dotnetcore/SecurityAPICommonsNetCore/SecurityAPICommonsNetCore.csproj +++ b/dotnet/src/extensions/SecurityAPI/dotnet/dotnetcore/SecurityAPICommonsNetCore/SecurityAPICommonsNetCore.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -23,6 +23,7 @@ + diff --git a/dotnet/src/extensions/SecurityAPI/dotnet/dotnetframework/SecurityAPICommons/Encoders/Base64UrlEncoder.cs b/dotnet/src/extensions/SecurityAPI/dotnet/dotnetframework/SecurityAPICommons/Encoders/Base64UrlEncoder.cs new file mode 100644 index 000000000..f300d761a --- /dev/null +++ b/dotnet/src/extensions/SecurityAPI/dotnet/dotnetframework/SecurityAPICommons/Encoders/Base64UrlEncoder.cs @@ -0,0 +1,161 @@ +using System; +using System.Security; +using SecurityAPICommons.Commons; +using SecurityAPICommons.Config; +using Org.BouncyCastle.Utilities.Encoders; +using System.Text; + +namespace SecurityAPICommons.Encoders +{ + [SecuritySafeCritical] + public class Base64UrlEncoder: SecurityAPIObject + { + [SecuritySafeCritical] + public Base64UrlEncoder() : base() + { + + } + + [SecuritySafeCritical] + public string toBase64(string text) + { + this.error.cleanError(); + EncodingUtil eu = new EncodingUtil(); + byte[] textBytes = eu.getBytes(text); + if (eu.HasError()) + { + this.error = eu.GetError(); + return ""; + } + string result = ""; + try + { + byte[] resultBytes = UrlBase64.Encode(textBytes); + result = Encoding.UTF8.GetString(resultBytes); + } + catch (Exception e) + { + this.error.setError("BS001", e.Message); + return ""; + } + return result; + } + + [SecuritySafeCritical] + public string toPlainText(string base64Text) + { + this.error.cleanError(); + byte[] bytes; + try + { + bytes = UrlBase64.Decode(base64Text); + } + catch (Exception e) + { + this.error.setError("BS002", e.Message); + return ""; + } + EncodingUtil eu = new EncodingUtil(); + string result = eu.getString(bytes); + if (eu.HasError()) + { + this.error = eu.GetError(); + return ""; + } + return result; + } + + [SecuritySafeCritical] + public string toStringHexa(string base64Text) + { + this.error.cleanError(); + byte[] bytes; + try + { + bytes = UrlBase64.Decode(base64Text); + } + catch (Exception e) + { + this.error.setError("BS003", e.Message); + return ""; + } + string result = ""; + try + { + result = Hex.ToHexString(bytes).ToUpper(); + } + catch (Exception e) + { + this.error.setError("BS004", e.Message); + return ""; + } + return result; + } + + [SecuritySafeCritical] + public string fromStringHexaToBase64(string stringHexa) + { + this.error.cleanError(); + byte[] stringBytes; + try + { + stringBytes = Hex.Decode(stringHexa); + } + catch (Exception e) + { + this.error.setError("BS005", e.Message); + return ""; + } + string result = ""; + try + { + byte[] resultBytes = UrlBase64.Encode(stringBytes); + result = Encoding.UTF8.GetString(resultBytes); + } + catch (Exception e) + { + this.error.setError("BS006", e.Message); + return ""; + } + return result; + } + + [SecuritySafeCritical] + public string base64ToBase64Url(string base64Text) + { + this.error.cleanError(); + string result = ""; + try + { + byte[] b64bytes = Base64.Decode(base64Text); + byte[] bytes = UrlBase64.Encode(b64bytes); + result = Encoding.UTF8.GetString(bytes); + } + catch (Exception e) + { + this.error.setError("BS007", e.Message); + return ""; + } + return result; + } + + [SecuritySafeCritical] + public string base64UrlToBase64(string base64UrlText) + { + this.error.cleanError(); + string result = ""; + try + { + byte[] b64bytes = UrlBase64.Decode(base64UrlText); + byte[] bytes = Base64.Encode(b64bytes); + result = Encoding.UTF8.GetString(bytes); + } + catch (Exception e) + { + this.error.setError("BS008", e.Message); + return ""; + } + return result; + } + } +} diff --git a/dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj b/dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj index 3bc09ce1d..49600c43b 100644 --- a/dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj +++ b/dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj @@ -48,6 +48,7 @@ + diff --git a/dotnet/src/extensions/SecurityAPI/test/dotnetframework/SecurityAPITest/SecurityAPICommons/encoders/TestBase64Url.cs b/dotnet/src/extensions/SecurityAPI/test/dotnetframework/SecurityAPITest/SecurityAPICommons/encoders/TestBase64Url.cs new file mode 100644 index 000000000..d5c3d432c --- /dev/null +++ b/dotnet/src/extensions/SecurityAPI/test/dotnetframework/SecurityAPITest/SecurityAPICommons/encoders/TestBase64Url.cs @@ -0,0 +1,68 @@ +using SecurityAPICommons.Encoders; +using SecurityAPITest.SecurityAPICommons.commons; +using NUnit.Framework; + +namespace SecurityAPITest.SecurityAPICommons.encoders +{ + [TestFixture] + public class TestBase64Url: SecurityAPITestObject + { + protected static string expected_plainText; + protected static string expected_encoded; + protected static string expected_hexaText; + protected static Base64UrlEncoder base64; + protected static string expected_base64; + + [SetUp] + public virtual void SetUp() + { + expected_plainText = "hello world"; + expected_encoded = "aGVsbG8gd29ybGQ."; + expected_hexaText = "68656C6C6F20776F726C64"; + base64 = new Base64UrlEncoder(); + expected_base64 = "aGVsbG8gd29ybGQ="; + } + + [Test] + public void TestToBase64Url() + { + string encoded = base64.toBase64(expected_plainText); + Equals(expected_encoded, encoded, base64); + } + + [Test] + public void TestToPlainTextUrl() + { + string plainText = base64.toPlainText(expected_encoded); + Equals(expected_plainText, plainText, base64); + } + + [Test] + public void TestToStringHexaUrl() + { + string hexaText = base64.toStringHexa(expected_encoded); + Equals(expected_hexaText, hexaText, base64); + } + + [Test] + public void TestFromStringHexaToBase64Url() + { + string encoded = base64.fromStringHexaToBase64(expected_hexaText); + Equals(expected_encoded, encoded, base64); + } + + [Test] + public void TestBase64UrlToBase64() + { + string encoded = base64.base64UrlToBase64(expected_encoded); + Equals(expected_base64, encoded, base64); + } + + [Test] + public void TestBase64ToBase64Url() + { + string encoded = base64.base64ToBase64Url(expected_base64); + Equals(expected_encoded, encoded, base64); + } + } +}