Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
Expand All @@ -23,6 +23,7 @@
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Config\EncodingUtil.cs" Link="Config\EncodingUtil.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Config\SecurityApiGlobal.cs" Link="Config\Global.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Encoders\Base64Encoder.cs" Link="Encoders\Base64Encoder.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Encoders\Base64UrlEncoder.cs" Link="Encoders\Base64UrlEncoder.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Encoders\HexaEncoder.cs" Link="Encoders\HexaEncoder.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Keys\CertificateX509.cs" Link="Keys\CertificateX509.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Keys\PrivateKeyManager.cs" Link="Keys\PrivateKeyManager.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="..\..\dotnetframework\SecurityAPITest\Jwt\Symmetric\TestSymmetricJwt.cs" Link="Jwt\Symmetric\TestSymmetricJwt.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\commons\SecurityAPITestObject.cs" Link="SecurityAPICommons\comons\SecurityAPITestObject.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\encoders\TestBase64.cs" Link="SecurityAPICommons\encoders\TestBase64.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\encoders\TestBase64Url.cs" Link="SecurityAPICommons\encoders\TestBase64Url.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\encoders\TestHexa.cs" Link="SecurityAPICommons\encoders\TestHexa.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\keys\TestBase64PrivateKey.cs" Link="SecurityAPICommons\keys\TestBase64PrivateKey.cs" />
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\keys\TestBase64Certificate.cs" Link="SecurityAPICommons\keys\TestBase64Certificate.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}