-
Notifications
You must be signed in to change notification settings - Fork 1
/
Base32Decode.js
71 lines (65 loc) · 2.94 KB
/
Base32Decode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var Base32Decode = function (base32EncodedString) {
/// <summary>Decodes a base32 encoded string into a Uin8Array, note padding is not supported</summary>
/// <param name="base32EncodedString" type="String">The base32 encoded string to be decoded</param>
/// <returns type="Uint8Array">The Unit8Array representation of the data that was encoded in base32EncodedString</returns>
if (!base32EncodedString && base32EncodedString !== "") {
throw "base32EncodedString cannot be null or undefined";
}
if (base32EncodedString.length * 5 % 8 !== 0) {
throw "base32EncodedString is not of the proper length. Please verify padding.";
}
base32EncodedString = base32EncodedString.toLowerCase();
var alphabet = "abcdefghijklmnopqrstuvwxyz234567";
var returnArray = new Array(base32EncodedString.length * 5 / 8);
var currentByte = 0;
var bitsRemaining = 8;
var mask = 0;
var arrayIndex = 0;
for (var count = 0; count < base32EncodedString.length; count++) {
var currentIndexValue = alphabet.indexOf(base32EncodedString[count]);
if (-1 === currentIndexValue) {
if ("=" === base32EncodedString[count]) {
var paddingCount = 0;
for (count = count; count < base32EncodedString.length; count++) {
if ("=" !== base32EncodedString[count]) {
throw "Invalid '=' in encoded string";
} else {
paddingCount++;
}
}
switch (paddingCount) {
case 6:
returnArray = returnArray.slice(0, returnArray.length - 4);
break;
case 4:
returnArray = returnArray.slice(0, returnArray.length - 3);
break;
case 3:
returnArray = returnArray.slice(0, returnArray.length - 2);
break;
case 1:
returnArray = returnArray.slice(0, returnArray.length - 1);
break;
default:
throw "Incorrect padding";
}
} else {
throw "base32EncodedString contains invalid characters or invalid padding.";
}
} else {
if (bitsRemaining > 5) {
mask = currentIndexValue << (bitsRemaining - 5);
currentByte = currentByte | mask;
bitsRemaining -= 5;
} else {
mask = currentIndexValue >> (5 - bitsRemaining);
currentByte = currentByte | mask;
returnArray[arrayIndex++] = currentByte;
currentByte = currentIndexValue << (3 + bitsRemaining);
bitsRemaining += 3;
}
}
}
return new Uint8Array(returnArray);
};
//# sourceMappingURL=Base32Decode.js.map