Skip to content

Commit

Permalink
#96 - Fixed authentication when using a password containing unicode c…
Browse files Browse the repository at this point in the history
…haracters (#97)
  • Loading branch information
gpailler authored Apr 5, 2018
1 parent 4e0dbdb commit a9c06cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions MegaApiClient/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.IO;
using System.Linq;
using System.Text;

internal static class Extensions
Expand Down Expand Up @@ -41,6 +42,28 @@ public static byte[] ToBytes(this string data)
return Encoding.UTF8.GetBytes(data);
}

public static byte[] ToBytesPassword(this string data)
{
// Store bytes characters in uint array
// discards bits 8-31 of multibyte characters for backwards compatibility
var array = new uint[(data.Length + 3) >> 2];
for (var i = 0; i < data.Length; i++)
{
array[i >> 2] |= (uint)(data[i] << (24 - (i & 3) * 8));
}

return array.SelectMany(x =>
{
var bytes = BitConverter.GetBytes(x);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bytes);
}
return bytes;
}).ToArray();
}

public static T[] CopySubArray<T>(this T[] source, int length, int offset = 0)
{
T[] result = new T[length];
Expand Down
2 changes: 1 addition & 1 deletion MegaApiClient/MegaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static AuthInfos GenerateAuthInfos(string email, string password)
}

// Retrieve password as UTF8 byte array
byte[] passwordBytes = password.ToBytes();
byte[] passwordBytes = password.ToBytesPassword();

// Encrypt password to use password as key for the hash
byte[] passwordAesKey = PrepareKey(passwordBytes);
Expand Down

0 comments on commit a9c06cd

Please sign in to comment.