Skip to content

Commit e21e193

Browse files
committed
Base64UrlEncoder implementation
1 parent 650a17c commit e21e193

File tree

4 files changed

+232
-1
lines changed

4 files changed

+232
-1
lines changed

dotnet/src/extensions/SecurityAPI/dotnet/dotnetcore/SecurityAPICommonsNetCore/SecurityAPICommonsNetCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net6.0</TargetFrameworks>
@@ -23,6 +23,7 @@
2323
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Config\EncodingUtil.cs" Link="Config\EncodingUtil.cs" />
2424
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Config\SecurityApiGlobal.cs" Link="Config\Global.cs" />
2525
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Encoders\Base64Encoder.cs" Link="Encoders\Base64Encoder.cs" />
26+
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Encoders\Base64UrlEncoder.cs" Link="Encoders\Base64UrlEncoder.cs" />
2627
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Encoders\HexaEncoder.cs" Link="Encoders\HexaEncoder.cs" />
2728
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Keys\CertificateX509.cs" Link="Keys\CertificateX509.cs" />
2829
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Keys\PrivateKeyManager.cs" Link="Keys\PrivateKeyManager.cs" />
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Security;
3+
using SecurityAPICommons.Commons;
4+
using SecurityAPICommons.Config;
5+
using Org.BouncyCastle.Utilities.Encoders;
6+
using System.Text;
7+
8+
namespace SecurityAPICommons.Encoders
9+
{
10+
[SecuritySafeCritical]
11+
public class Base64UrlEncoder: SecurityAPIObject
12+
{
13+
[SecuritySafeCritical]
14+
public Base64UrlEncoder() : base()
15+
{
16+
17+
}
18+
19+
[SecuritySafeCritical]
20+
public string toBase64(string text)
21+
{
22+
this.error.cleanError();
23+
EncodingUtil eu = new EncodingUtil();
24+
byte[] textBytes = eu.getBytes(text);
25+
if (eu.HasError())
26+
{
27+
this.error = eu.GetError();
28+
return "";
29+
}
30+
string result = "";
31+
try
32+
{
33+
byte[] resultBytes = UrlBase64.Encode(textBytes);
34+
result = Encoding.UTF8.GetString(resultBytes);
35+
}
36+
catch (Exception e)
37+
{
38+
this.error.setError("BS001", e.Message);
39+
return "";
40+
}
41+
return result;
42+
}
43+
44+
[SecuritySafeCritical]
45+
public string toPlainText(string base64Text)
46+
{
47+
this.error.cleanError();
48+
byte[] bytes;
49+
try
50+
{
51+
bytes = UrlBase64.Decode(base64Text);
52+
}
53+
catch (Exception e)
54+
{
55+
this.error.setError("BS002", e.Message);
56+
return "";
57+
}
58+
EncodingUtil eu = new EncodingUtil();
59+
string result = eu.getString(bytes);
60+
if (eu.HasError())
61+
{
62+
this.error = eu.GetError();
63+
return "";
64+
}
65+
return result;
66+
}
67+
68+
[SecuritySafeCritical]
69+
public string toStringHexa(string base64Text)
70+
{
71+
this.error.cleanError();
72+
byte[] bytes;
73+
try
74+
{
75+
bytes = UrlBase64.Decode(base64Text);
76+
}
77+
catch (Exception e)
78+
{
79+
this.error.setError("BS003", e.Message);
80+
return "";
81+
}
82+
string result = "";
83+
try
84+
{
85+
result = Hex.ToHexString(bytes).ToUpper();
86+
}
87+
catch (Exception e)
88+
{
89+
this.error.setError("BS004", e.Message);
90+
return "";
91+
}
92+
return result;
93+
}
94+
95+
[SecuritySafeCritical]
96+
public string fromStringHexaToBase64(string stringHexa)
97+
{
98+
this.error.cleanError();
99+
byte[] stringBytes;
100+
try
101+
{
102+
stringBytes = Hex.Decode(stringHexa);
103+
}
104+
catch (Exception e)
105+
{
106+
this.error.setError("BS005", e.Message);
107+
return "";
108+
}
109+
string result = "";
110+
try
111+
{
112+
byte[] resultBytes = UrlBase64.Encode(stringBytes);
113+
result = Encoding.UTF8.GetString(resultBytes);
114+
}
115+
catch (Exception e)
116+
{
117+
this.error.setError("BS006", e.Message);
118+
return "";
119+
}
120+
return result;
121+
}
122+
123+
[SecuritySafeCritical]
124+
public string base64ToBase64Url(string base64Text)
125+
{
126+
this.error.cleanError();
127+
string result = "";
128+
try
129+
{
130+
byte[] b64bytes = Base64.Decode(base64Text);
131+
byte[] bytes = UrlBase64.Encode(b64bytes);
132+
result = Encoding.UTF8.GetString(bytes);
133+
}
134+
catch (Exception e)
135+
{
136+
this.error.setError("BS007", e.Message);
137+
return "";
138+
}
139+
return result;
140+
}
141+
142+
[SecuritySafeCritical]
143+
public string base64UrlToBase64(string base64UrlText)
144+
{
145+
this.error.cleanError();
146+
string result = "";
147+
try
148+
{
149+
byte[] b64bytes = UrlBase64.Decode(base64UrlText);
150+
byte[] bytes = Base64.Encode(b64bytes);
151+
result = Encoding.UTF8.GetString(bytes);
152+
}
153+
catch (Exception e)
154+
{
155+
this.error.setError("BS008", e.Message);
156+
return "";
157+
}
158+
return result;
159+
}
160+
}
161+
}

dotnet/src/extensions/SecurityAPI/test/dotnetcore/SecurityAPITestNetCore/SecurityAPITestNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Compile Include="..\..\dotnetframework\SecurityAPITest\Jwt\Symmetric\TestSymmetricJwt.cs" Link="Jwt\Symmetric\TestSymmetricJwt.cs" />
4949
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\commons\SecurityAPITestObject.cs" Link="SecurityAPICommons\comons\SecurityAPITestObject.cs" />
5050
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\encoders\TestBase64.cs" Link="SecurityAPICommons\encoders\TestBase64.cs" />
51+
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\encoders\TestBase64Url.cs" Link="SecurityAPICommons\encoders\TestBase64Url.cs" />
5152
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\encoders\TestHexa.cs" Link="SecurityAPICommons\encoders\TestHexa.cs" />
5253
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\keys\TestBase64PrivateKey.cs" Link="SecurityAPICommons\keys\TestBase64PrivateKey.cs" />
5354
<Compile Include="..\..\dotnetframework\SecurityAPITest\SecurityAPICommons\keys\TestBase64Certificate.cs" Link="SecurityAPICommons\keys\TestBase64Certificate.cs" />
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using SecurityAPICommons.Encoders;
2+
using SecurityAPITest.SecurityAPICommons.commons;
3+
using NUnit.Framework;
4+
5+
namespace SecurityAPITest.SecurityAPICommons.encoders
6+
{
7+
[TestFixture]
8+
public class TestBase64Url: SecurityAPITestObject
9+
{
10+
protected static string expected_plainText;
11+
protected static string expected_encoded;
12+
protected static string expected_hexaText;
13+
protected static Base64UrlEncoder base64;
14+
protected static string expected_base64;
15+
16+
[SetUp]
17+
public virtual void SetUp()
18+
{
19+
expected_plainText = "hello world";
20+
expected_encoded = "aGVsbG8gd29ybGQ.";
21+
expected_hexaText = "68656C6C6F20776F726C64";
22+
base64 = new Base64UrlEncoder();
23+
expected_base64 = "aGVsbG8gd29ybGQ=";
24+
}
25+
26+
[Test]
27+
public void TestToBase64Url()
28+
{
29+
string encoded = base64.toBase64(expected_plainText);
30+
Equals(expected_encoded, encoded, base64);
31+
}
32+
33+
[Test]
34+
public void TestToPlainTextUrl()
35+
{
36+
string plainText = base64.toPlainText(expected_encoded);
37+
Equals(expected_plainText, plainText, base64);
38+
}
39+
40+
[Test]
41+
public void TestToStringHexaUrl()
42+
{
43+
string hexaText = base64.toStringHexa(expected_encoded);
44+
Equals(expected_hexaText, hexaText, base64);
45+
}
46+
47+
[Test]
48+
public void TestFromStringHexaToBase64Url()
49+
{
50+
string encoded = base64.fromStringHexaToBase64(expected_hexaText);
51+
Equals(expected_encoded, encoded, base64);
52+
}
53+
54+
[Test]
55+
public void TestBase64UrlToBase64()
56+
{
57+
string encoded = base64.base64UrlToBase64(expected_encoded);
58+
Equals(expected_base64, encoded, base64);
59+
}
60+
61+
[Test]
62+
public void TestBase64ToBase64Url()
63+
{
64+
string encoded = base64.base64ToBase64Url(expected_base64);
65+
Equals(expected_encoded, encoded, base64);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)