Debian Codegraph @

flask_security/static/js/base64.js

raw
1
2 /**
3 * Thanks to duo-labs/py_webauthn - this is their OLD flask demo.
4 * Modified accordingly...
5 * This code should be considered to be licensed:
6 * Copyright (c) 2017 Duo Security, Inc. All rights reserved.
7 * with the BSD 3-Clause "New" or "Revised" License.
8 */
9
10 var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
11
12 ;(function (exports) {
13 'use strict'
14
15 var Arr = (typeof Uint8Array !== 'undefined')
16 ? Uint8Array
17 : Array
18
19 var PLUS = '+'.charCodeAt(0)
20 var SLASH = '/'.charCodeAt(0)
21 var NUMBER = '0'.charCodeAt(0)
22 var LOWER = 'a'.charCodeAt(0)
23 var UPPER = 'A'.charCodeAt(0)
24 var PLUS_URL_SAFE = '-'.charCodeAt(0)
25 var SLASH_URL_SAFE = '_'.charCodeAt(0)
26
27 function decode (elt) {
28 var code = elt.charCodeAt(0)
29 if (code === PLUS || code === PLUS_URL_SAFE) return 62 // '+'
30 if (code === SLASH || code === SLASH_URL_SAFE) return 63 // '/'
31 if (code < NUMBER) return -1 // no match
32 if (code < NUMBER + 10) return code - NUMBER + 26 + 26
33 if (code < UPPER + 26) return code - UPPER
34 if (code < LOWER + 26) return code - LOWER + 26
35 }
36
37 function b64ToByteArray (b64) {
38 var i, j, l, tmp, placeHolders, arr
39
40 if (b64.length % 4 > 0) {
41 throw new Error('Invalid string. Length must be a multiple of 4')
42 }
43
44 // the number of equal signs (place holders)
45 // if there are two placeholders, than the two characters before it
46 // represent one byte
47 // if there is only one, then the three characters before it represent 2 bytes
48 // this is just a cheap hack to not do indexOf twice
49 var len = b64.length
50 placeHolders = b64.charAt(len - 2) === '=' ? 2 : b64.charAt(len - 1) === '=' ? 1 : 0
51
52 // base64 is 4/3 + up to two characters of the original data
53 arr = new Arr(b64.length * 3 / 4 - placeHolders)
54
55 // if there are placeholders, only get up to the last complete 4 chars
56 l = placeHolders > 0 ? b64.length - 4 : b64.length
57
58 var L = 0
59
60 function push (v) {
61 arr[L++] = v
62 }
63
64 for (i = 0, j = 0; i < l; i += 4, j += 3) {
65 tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
66 push((tmp & 0xFF0000) >> 16)
67 push((tmp & 0xFF00) >> 8)
68 push(tmp & 0xFF)
69 }
70
71 if (placeHolders === 2) {
72 tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
73 push(tmp & 0xFF)
74 } else if (placeHolders === 1) {
75 tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
76 push((tmp >> 8) & 0xFF)
77 push(tmp & 0xFF)
78 }
79
80 return arr
81 }
82
83 function uint8ToBase64 (uint8) {
84 var i
85 var extraBytes = uint8.length % 3 // if we have 1 byte left, pad 2 bytes
86 var output = ''
87 var temp, length
88
89 function encode (num) {
90 return lookup.charAt(num)
91 }
92
93 function tripletToBase64 (num) {
94 return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)
95 }
96
97 // go through the array every three bytes, we'll deal with trailing stuff later
98 for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
99 temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
100 output += tripletToBase64(temp)
101 }
102
103 // pad the end with zeros, but make sure to not forget the extra bytes
104 switch (extraBytes) {
105 case 1:
106 temp = uint8[uint8.length - 1]
107 output += encode(temp >> 2)
108 output += encode((temp << 4) & 0x3F)
109 output += '=='
110 break
111 case 2:
112 temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
113 output += encode(temp >> 10)
114 output += encode((temp >> 4) & 0x3F)
115 output += encode((temp << 2) & 0x3F)
116 output += '='
117 break
118 default:
119 break
120 }
121
122 return output
123 }
124
125 exports.toByteArray = b64ToByteArray
126 exports.fromByteArray = uint8ToBase64
127 }(typeof exports === 'undefined' ? (this.base64js = {}) : exports))