-
Notifications
You must be signed in to change notification settings - Fork 11
/
base64.js
148 lines (135 loc) · 4.07 KB
/
base64.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* This file contains a few core utilities and polyfills.
*/
'use strict';
// Write to the various div containers.
function output(target, value) {
if (target == null){
return
}
try {
let t = document.getElementsByName(target)[0];
if (t instanceof HTMLInputElement) {
t.value = value;
return;
}
if (t instanceof HTMLTextAreaElement) {
t.value = value;
return;
}
t.innerHTML = value;
} catch (e) {
console.error("output", target, value, e);
return null;
}
}
function get(target) {
let t = document.getElementsByName(target)[0];
if (t instanceof HTMLInputElement) {
return t.value;
}
if (t instanceof HTMLTextAreaElement) {
return t.value;
}
return t.innerHTML;
}
// Split a byte array into chunks of size.
function chunkArray(array, size) {
var start = array.byteOffset || 0;
array = array.buffer || array;
var index = 0;
var result = [];
while(index + size <= array.byteLength) {
result.push(new Uint8Array(array, start + index, size));
index += size;
}
if (index <= array.byteLength) {
result.push(new Uint8Array(array, start + index));
}
return result;
}
function newSalt() {
return window.crypto.getRandomValues(new Uint8Array(16));
}
/* I can't believe that this is needed here, in this day and age ...
* Note: these are not efficient, merely expedient.
*/
var base64url = {
_strmap: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg' +
'hijklmnopqrstuvwxyz0123456789-_',
encode: function(data) {
data = new Uint8Array(data);
var len = Math.ceil(data.length * 4 / 3);
return chunkArray(data, 3).map(chunk => [
chunk[0] >>> 2,
((chunk[0] & 0x3) << 4) | (chunk[1] >>> 4),
((chunk[1] & 0xf) << 2) | (chunk[2] >>> 6),
chunk[2] & 0x3f
].map(v => base64url._strmap[v]).join('')).join('').slice(0, len);
},
_lookup: function(s, i) {
return base64url._strmap.indexOf(s.charAt(i));
},
decode: function(str) {
var v = new Uint8Array(Math.floor(str.length * 3 / 4));
var vi = 0;
for (var si = 0; si < str.length;) {
var w = base64url._lookup(str, si++);
var x = base64url._lookup(str, si++);
var y = base64url._lookup(str, si++);
var z = base64url._lookup(str, si++);
v[vi++] = w << 2 | x >>> 4;
v[vi++] = x << 4 | y >>> 2;
v[vi++] = y << 6 | z;
}
return v;
}
};
// Hash-Based Message Authentication Code
// This generates a secure hash based on the key.
function hmac(key) {
// key = mzcc.rawToJWK(key);
this.keyPromise = webCrypto.importKey(
'raw',
key,
{
name: 'HMAC',
hash: 'SHA-256'
},
true, // Should be false for production.
['sign']);
};
hmac.prototype.hash = function(input) {
return this.keyPromise.then(k => webCrypto.sign('HMAC', k, input));
};
// HMAC (Hash-Based Message Authentication Code)-Based extract & expand Key
// Derivation Function. (Yeah, that's why everyone calls it "hkdf")
function hkdf(salt, ikm) {
this.prkhPromise = new hmac(salt).hash(ikm)
.then(prk => new hmac(prk));
};
hkdf.prototype.extract = function(info, len) {
var input = concatArray([info, new Uint8Array([1])]);
return this.prkhPromise
.then(prkh => prkh.hash(input))
.then(h => {
if (h.byteLength < len) {
throw new Error('Length is too long');
}
return h.slice(0, len);
});
};
function concatArray(arrays) {
// Concatenate the byte arrays into a single Uint8Array.
var size = arrays.reduce((total, a) => total + a.byteLength, 0);
var index = 0;
return arrays.reduce((result, a) => {
result.set(new Uint8Array(a), index);
index += a.byteLength;
return result;
}, new Uint8Array(size));
}
function be16(val) {
// present an 8bit value as a Big Endian 16bit value
return ((val & 0xFF) << 8 ) | ((val >> 8) & 0xFF);
}
window.base64url = base64url;