-
Notifications
You must be signed in to change notification settings - Fork 4
/
hex64.js
265 lines (241 loc) · 7.1 KB
/
hex64.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"use strict";
/** BaseToHex converts base to Hex.
* @param {string} input Input string.
* @param {string} inAlph Input alphabet (i.e. 0123456789ABCDEF)
* @param {Hex}
*/
function BaseToHex(input, inAlph) {
if (input === null || input === "" || input.length === 0) {
return ""
}
if (inAlph !== Base16) {
input = BaseConvert(input, inAlph, Base16);
}
if (input.length % 2 === 1) {
input = input.padStart(input.length + 1, "0");
}
return input; // Hex is always padded.
}
/**
* HexToArrayBuffer converts string Hex to ArrayBuffer.
*
* @param {Hex} Hex String Hex.
* @returns {ArrayBuffer} ArrayBuffer.
*/
async function HexToArrayBuffer(hex) {
if (hex === undefined) { // undefined is different from 0 since 0 == "AA"
return new Uint8Array().buffer;
}
if ((hex.length % 2) !== 0) {
throw new RangeError('HexToArrayBuffer: Hex is not even.')
}
var a = new Uint8Array(hex.length / 2)
for (var i = 0; i < hex.length; i += 2) {
a[i / 2] = parseInt(hex.substring(i, i + 2), 16)
}
return a.buffer
};
/**
* ArrayBufferToHex accepts an array buffer and returns a string of hex.
* Taken from https://stackoverflow.com/a/50767210/1923095
*
* @param {ArrayBuffer} buffer str that is being converted to UTF8
* @returns {string} hex String with hex.
*/
async function ArrayBufferToHex(buffer) {
return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, "0")).join('').toUpperCase();
// Alternatively:
// let hashArray = Array.from(new Uint8Array(digest)); // convert buffer to byte array
// let hexHash = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
};
// String to ASCII (utf-8) binary HEX string
function SToHex(input) {
// console.debug(input);
if (typeof input != 'string') {
throw new TypeError('Input is not a string. Given type: ' + typeof input);
}
return input.split("").reduce((hex, c) => hex += c.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0"), "");
}
// ASCII (utf-8) binary HEX string to string
function HexToS(input) {
if (typeof input != 'string') {
throw new TypeError('input is not a string');
}
if (isEmpty(input)) {
return ""; // empty is a valid input.
}
return input.match(/.{1,2}/g).reduce((acc, char) => acc + String.fromCharCode(parseInt(char, 16)), "");
}
/**
* HexPadded accepts input, input alphabet, and alg and returns Hex. Alg is
* used to enforce padding.
* @param {String} input String. Number being converted.
* @param {String} inputBase String. Input alphabet for input.
* @param {String} alg String. Alg for specifying length of pad.
* @returns {String} Base16 padded to alg's specified length.
* @throws {error} Returns error on unsupported algs.
*/
function HexPadded(input, inputBase, alg) {
let hex = BaseConvert(input, inputBase, Base16);
switch (alg) {
case 'ES256':
return hex.padStart(64, "0");
case "ES384":
return hex.padStart(96, "0");
case "ES512":
return hex.padStart(128, "0");
default:
throw new Error("base_convert.HexPadded: unsupported alg");
}
};
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
// RFC 4648 "base64"s
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
/**
* ArrayBufferTo64ut Array buffer to b64ut.
*
* @param {ArrayBuffer} buffer
* @returns {string} String. base64ut.
*/
function ArrayBufferTo64ut(buffer) {
var string = String.fromCharCode.apply(null, new Uint8Array(buffer));
return base64t(URIUnsafeToSafe(btoa(string)));
};
/**
* B64ToHex takes any RFC 4648 base64 to Hex.
*
* @param {string} b64 RFC 4648 any base64.
* @returns {string} Hex representation.
*/
function B64ToHex(b64) {
// console.debug(b64);
let ub64 = URISafeToUnsafe(b64);
const raw = atob(ub64);
let result = '';
for (let i = 0; i < raw.length; i++) {
const hex = raw.charCodeAt(i).toString(16).toUpperCase();
result += (hex.length === 2 ? hex : '0' + hex);
}
return result;
};
/**
* HexToUb64p is Hex to RFC ub64p.
*
* @param {string} Hex String. Hex representation.
* @returns {string} String. ub64 RFC 4648 URI unsafe base 64 padded.
*/
function HexToUb64p(Hex) {
// console.debug(Hex);
if (Hex.length == 0) {
return "";
}
let bytes = Hex.match(/\w{2}/g).map(function (a) {
return String.fromCharCode(parseInt(a, 16));
}).join("");
return btoa(bytes);
}
/**
* HexTob64ut is hex to "RFC 4648 URI Safe Truncated".
*
* @param {string} hex String. Hex representation.
* @returns {string} String. b64ut RFC 4648 URI safe truncated.
*/
async function HexTob64ut(hex) {
let ab = await HexToArrayBuffer(hex);
return await ArrayBufferTo64ut(ab);
};
/**
* SToB64ut encodes a string as base64 URI truncated string.
* "String to base64 uri truncated"
*
* @param {string} string
* @returns {string}
*/
function SToB64ut(string) {
return btoa(string).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
};
/**
* B64utToS takes a b64ut string and decodes it back into a string.
* "base64 uri truncated to string"
*
* @param {string} string
* @returns {string}
*/
function B64utToS(string) {
// atob doesn't care about the padding character '='
return atob(string.replace(/-/g, '+').replace(/_/g, '/'));
};
/**
* SToUB64p takes a string and encodes it into a unsafe Base64 string.
* "String to Unsafe Base64"
*
* @param {string} string string
* @returns {string} Unsafe Base64 string
*/
function SToUB64p(string) {
return btoa(string);
};
/**
* UB64pToS takes an unsafe base64 string and decodes it back into a string.
* "Unsafe base64 padded to String"
*
* @param {string} string Unsafe base64 padded string
* @returns {string}
*/
function UB64pToS(string) {
return atob(string);
};
/**
* URIUnsafeToSafe converts any URI unsafe string to URI safe.
*
* @param {string} ub64t
* @returns {string} b64ut
*/
function URIUnsafeToSafe(ub64) {
return ub64.replace(/\+/g, '-').replace(/\//g, '_');
};
/**
* URISafeToUnsafe converts any URI safe string to URI unsafe.
*
* @param {string} b64ut
* @returns {string} ub64t
*/
function URISafeToUnsafe(ub64) {
return ub64.replace(/-/g, '+').replace(/_/g, '/');
};
/**
* base64t removes base64 padding if applicable.
* @param {string} base64
* @returns {string} base64t
*/
function base64t(base64) {
return base64.replace(/=/g, '');
}
/**
* TToP (truncated to padded) takes any base64 truncated string and adds padding
* if appropriate.
*
* @param {string} base64t
* @returns {string} base64p
*/
function TToP(base64) {
var padding = base64.length % 4;
switch (padding) {
case 0:
return base64;
case 1:
// malformed input, can only 0, 2, 3, or 4 characters, never 1.
console.error("input is invalid base64.");
return;
case 2:
return base64 + "==";
case 3:
return base64 + "=";
}
}