Skip to content

Commit 42482a4

Browse files
committed
Convert strings to UTF-8 before encoding to Base64
1 parent 3f80c94 commit 42482a4

File tree

1 file changed

+9
-66
lines changed

1 file changed

+9
-66
lines changed

src/js/services/Base64.js

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,28 @@
22
define(['./_module'], function (app) {
33

44
'use strict';
5-
// http://wemadeyoulook.at/en/blog/implementing-basic-http-authentication-http-requests-angular/
65
return app.factory('Base64', [ 'MessageService', function (msg) {
7-
var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
6+
var utf8Encoder = new TextEncoder('utf-8');
7+
var utf8Decoder = new TextDecoder('utf-8');
88

99
return {
1010
encode: function (input) {
11-
var output = '';
12-
var chr1, chr2, chr3 = '';
13-
var enc1, enc2, enc3, enc4 = '';
14-
var i = 0;
15-
16-
do {
17-
chr1 = input.charCodeAt(i++);
18-
chr2 = input.charCodeAt(i++);
19-
chr3 = input.charCodeAt(i++);
20-
21-
enc1 = chr1 >> 2;
22-
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
23-
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
24-
enc4 = chr3 & 63;
25-
26-
if (isNaN(chr2)) {
27-
enc3 = enc4 = 64;
28-
} else if (isNaN(chr3)) {
29-
enc4 = 64;
30-
}
31-
32-
output = output +
33-
keyStr.charAt(enc1) +
34-
keyStr.charAt(enc2) +
35-
keyStr.charAt(enc3) +
36-
keyStr.charAt(enc4);
37-
chr1 = chr2 = chr3 = '';
38-
enc1 = enc2 = enc3 = enc4 = '';
39-
} while (i < input.length);
40-
41-
return output;
11+
var utf8Bytes = utf8Encoder.encode(input);
12+
var binString = String.fromCodePoint(...utf8Bytes);
13+
return btoa(binString);
4214
},
43-
4415
decode: function (input) {
45-
var output = '';
46-
var chr1, chr2, chr3 = '';
47-
var enc1, enc2, enc3, enc4 = '';
48-
var i = 0;
49-
5016
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
5117
var base64test = /[^A-Za-z0-9\+\/\=]/g;
5218
if (base64test.exec(input)) {
5319
msg.failure('There were invalid base64 characters in the input text.\n' +
5420
'Valid base64 characters are A-Z, a-z, 0-9, \'+\', \'/\',and \'=\'\n' +
5521
'Expect errors in decoding.');
5622
}
57-
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
58-
59-
do {
60-
enc1 = keyStr.indexOf(input.charAt(i++));
61-
enc2 = keyStr.indexOf(input.charAt(i++));
62-
enc3 = keyStr.indexOf(input.charAt(i++));
63-
enc4 = keyStr.indexOf(input.charAt(i++));
64-
65-
chr1 = (enc1 << 2) | (enc2 >> 4);
66-
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
67-
chr3 = ((enc3 & 3) << 6) | enc4;
68-
69-
output = output + String.fromCharCode(chr1);
70-
71-
if (enc3 !== 64) {
72-
output = output + String.fromCharCode(chr2);
73-
}
74-
if (enc4 !== 64) {
75-
output = output + String.fromCharCode(chr3);
76-
}
77-
78-
chr1 = chr2 = chr3 = '';
79-
enc1 = enc2 = enc3 = enc4 = '';
80-
81-
} while (i < input.length);
82-
83-
return output;
23+
24+
var binString = atob(input);
25+
var utf8Bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
26+
return utf8Decoder.decode(utf8Bytes);
8427
}
8528
};
8629
}]);

0 commit comments

Comments
 (0)