diff options
Diffstat (limited to 'templates/js/base64.js')
-rw-r--r-- | templates/js/base64.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/templates/js/base64.js b/templates/js/base64.js new file mode 100644 index 0000000..52df92b --- /dev/null +++ b/templates/js/base64.js | |||
@@ -0,0 +1,66 @@ | |||
1 | /* | ||
2 | * Base64URL-ArrayBuffer | ||
3 | * https://github.com/herrjemand/Base64URL-ArrayBuffer | ||
4 | * | ||
5 | * Copyright (c) 2017 Yuriy Ackermann <ackermann.yuriy@gmail.com> | ||
6 | * Copyright (c) 2012 Niklas von Hertzen | ||
7 | * Licensed under the MIT license. | ||
8 | * | ||
9 | */ | ||
10 | (function(){ | ||
11 | 'use strict'; | ||
12 | |||
13 | let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; | ||
14 | |||
15 | // Use a lookup table to find the index. | ||
16 | let lookup = new Uint8Array(256); | ||
17 | for (let i = 0; i < chars.length; i++) { | ||
18 | lookup[chars.charCodeAt(i)] = i; | ||
19 | } | ||
20 | |||
21 | let encode = function(arraybuffer) { | ||
22 | let bytes = new Uint8Array(arraybuffer), | ||
23 | i, len = bytes.length, base64url = ''; | ||
24 | |||
25 | for (i = 0; i < len; i+=3) { | ||
26 | base64url += chars[bytes[i] >> 2]; | ||
27 | base64url += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; | ||
28 | base64url += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; | ||
29 | base64url += chars[bytes[i + 2] & 63]; | ||
30 | } | ||
31 | |||
32 | if ((len % 3) === 2) { | ||
33 | base64url = base64url.substring(0, base64url.length - 1); | ||
34 | } else if (len % 3 === 1) { | ||
35 | base64url = base64url.substring(0, base64url.length - 2); | ||
36 | } | ||
37 | |||
38 | return base64url; | ||
39 | }; | ||
40 | |||
41 | let decode = function(base64string) { | ||
42 | let bufferLength = base64string.length * 0.75, | ||
43 | len = base64string.length, i, p = 0, | ||
44 | encoded1, encoded2, encoded3, encoded4; | ||
45 | |||
46 | let bytes = new Uint8Array(bufferLength); | ||
47 | |||
48 | for (i = 0; i < len; i+=4) { | ||
49 | encoded1 = lookup[base64string.charCodeAt(i)]; | ||
50 | encoded2 = lookup[base64string.charCodeAt(i+1)]; | ||
51 | encoded3 = lookup[base64string.charCodeAt(i+2)]; | ||
52 | encoded4 = lookup[base64string.charCodeAt(i+3)]; | ||
53 | |||
54 | bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); | ||
55 | bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); | ||
56 | bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); | ||
57 | } | ||
58 | |||
59 | return bytes.buffer | ||
60 | }; | ||
61 | |||
62 | window.base64url = { | ||
63 | 'decode': decode, | ||
64 | 'encode': encode | ||
65 | }; | ||
66 | })(); | ||