-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Base64UrlEncoder implementation (#849)
- Loading branch information
Showing
4 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...nsions/SecurityAPI/dotnet/dotnetframework/SecurityAPICommons/Encoders/Base64UrlEncoder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...rityAPI/test/dotnetframework/SecurityAPITest/SecurityAPICommons/encoders/TestBase64Url.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |