Skip to content

Commit

Permalink
Fix FromBase64
Browse files Browse the repository at this point in the history
- Improve efficiency by using string instead of char.
- Rework native call to use string.
- Bump native version to 100.5.0.11.
- Add unit tests.
  • Loading branch information
josesimoes committed Jun 7, 2021
1 parent bcbf646 commit 95b9b09
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
67 changes: 67 additions & 0 deletions Tests/NFUnitTestConversions/UnitTestConvertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,5 +510,72 @@ public static string FloatToHex(float f)

#endregion

#region Base64 conversions

[TestMethod]
public void Convert_FromToBase64()
{
string sharedAccessKeyPlainText = "IAmALongAndNiceKeyInPlainText";
string sharedAccessKey = "SUFtQUxvbmdBbmROaWNlS2V5SW5QbGFpblRleHQ=";

byte[] sharedAccessKeyAsByte = Convert.FromBase64String(sharedAccessKey);

char[] charArray = new char[sharedAccessKeyAsByte.Length];

for (int i = 0; i < sharedAccessKeyAsByte.Length; i++)
{
charArray[i] = (char)sharedAccessKeyAsByte[i];
}

string sharedAccessKeyAsString = new(charArray);

Assert.Equal(sharedAccessKeyAsString, sharedAccessKeyPlainText, "Converted string as byte array is not correct.");

string convertedFromBase64 = Convert.ToBase64String(sharedAccessKeyAsByte);

Debug.WriteLine($">>{convertedFromBase64}");
Debug.WriteLine($">>{sharedAccessKey}");

Assert.Equal(convertedFromBase64, sharedAccessKey, "Converted string from byte array is not correct.");
}

[TestMethod]
public void Convert_ToFromBase64()
{
byte[] inArray = new byte[256];
byte[] outArray;
int x;

for (x = 0; x < inArray.Length; x++)
{
inArray[x] = (byte)x;
}

string base64EncodedString_WithLineBreaks = @"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4
OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx
cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmq
q6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj
5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";

string base64EncodedString_WithoutLineBreaks = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";

string base64string1 = Convert.ToBase64String(
inArray,
0,
inArray.Length,
Base64FormattingOptions.InsertLineBreaks);

Assert.Equal(base64string1, base64EncodedString_WithLineBreaks, "Converted Base64 string with line breaks is not correct.");

string base64string2 = Convert.ToBase64String(inArray);

Assert.Equal(base64string2, base64EncodedString_WithoutLineBreaks, "Converted Base64 string without line breaks is not correct.");

outArray = Convert.FromBase64String(base64string1);

Assert.Equal(inArray, outArray, "Convert back from Base64 encoded array is not equal");
}

#endregion
}
}
2 changes: 1 addition & 1 deletion nanoFramework.CoreLibrary/System/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
[assembly: AssemblyProduct(".NET nanoFramework mscorlib")]
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")]

[assembly: AssemblyNativeVersion("100.5.0.10")]
[assembly: AssemblyNativeVersion("100.5.0.11")]
22 changes: 5 additions & 17 deletions nanoFramework.CoreLibrary/System/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,8 @@ public static string ToBase64String(byte[] inArray, int offset, int length, Base
/// The white-space characters, and their Unicode names and hexadecimal code points, are tab(CHARACTER TABULATION, U+0009), newline(LINE FEED, U+000A), carriage return (CARRIAGE RETURN, U+000D), and blank(SPACE, U+0020). An arbitrary number of white-space characters can appear in s because all white-space characters are ignored.
/// The valueless character, "=", is used for trailing padding. The end of s can consist of zero, one, or two padding characters.
/// </remarks>
public static byte[] FromBase64String(string inString)
{
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
if (inString == null) throw new ArgumentNullException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one

var chArray = inString.ToCharArray();

return FromBase64CharArray(chArray, 0, chArray.Length);
}
[MethodImpl(MethodImplOptions.InternalCall)]
public extern static byte[] FromBase64String(string inString);

/// <summary>
/// Converts a subset of a Unicode character array, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array. Parameters specify the subset in the input array and the number of elements to convert.
Expand All @@ -341,14 +333,10 @@ public static byte[] FromBase64CharArray(char[] inArray, int offset, int length)
if (offset > inArray.Length - length) throw new ArgumentOutOfRangeException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one

// copy to new array
var destinationArray = new char[length];
Array.Copy(inArray, offset, destinationArray, 0, length);
// copy to new string
string base64String = new(inArray, offset, length);

return FromBase64CharArray(destinationArray, length);
return FromBase64String(base64String);
}

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern byte[] FromBase64CharArray(char[] inArray, int length);
}
}

0 comments on commit 95b9b09

Please sign in to comment.