Skip to content

Commit a9c06cd

Browse files
authored
#96 - Fixed authentication when using a password containing unicode characters (#97)
1 parent 4e0dbdb commit a9c06cd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

MegaApiClient/Extensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.IO;
5+
using System.Linq;
56
using System.Text;
67

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

45+
public static byte[] ToBytesPassword(this string data)
46+
{
47+
// Store bytes characters in uint array
48+
// discards bits 8-31 of multibyte characters for backwards compatibility
49+
var array = new uint[(data.Length + 3) >> 2];
50+
for (var i = 0; i < data.Length; i++)
51+
{
52+
array[i >> 2] |= (uint)(data[i] << (24 - (i & 3) * 8));
53+
}
54+
55+
return array.SelectMany(x =>
56+
{
57+
var bytes = BitConverter.GetBytes(x);
58+
if (BitConverter.IsLittleEndian)
59+
{
60+
Array.Reverse(bytes);
61+
}
62+
63+
return bytes;
64+
}).ToArray();
65+
}
66+
4467
public static T[] CopySubArray<T>(this T[] source, int length, int offset = 0)
4568
{
4669
T[] result = new T[length];

MegaApiClient/MegaApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static AuthInfos GenerateAuthInfos(string email, string password)
102102
}
103103

104104
// Retrieve password as UTF8 byte array
105-
byte[] passwordBytes = password.ToBytes();
105+
byte[] passwordBytes = password.ToBytesPassword();
106106

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

0 commit comments

Comments
 (0)