base64.js
1.12 KB
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
'use strict';
var Enc = require('./bytes.js');
// to Base64
function bufToBase64(buf) {
// we want to maintain api compatability with browser APIs,
// so we assume that this could be a Uint8Array
return Buffer.from(buf).toString('base64');
}
Enc.bufToBase64 = bufToBase64;
Enc.strToBase64 = function(str) {
return Buffer.from(str).toString('base64');
};
// from Base64
function base64ToBuf(b64) {
// node handles URL Safe Base64 automatically
return Buffer.from(b64, 'base64');
}
Enc.base64ToBuf = base64ToBuf;
Enc.base64ToStr = function(b64) {
return base64ToBuf(b64).toString('utf8');
};
// URL Safe Base64
Enc.urlBase64ToBase64 = function(u64) {
var r = u64 % 4;
if (2 === r) {
u64 += '==';
} else if (3 === r) {
u64 += '=';
}
return u64.replace(/-/g, '+').replace(/_/g, '/');
};
Enc.base64ToUrlBase64 = function(b64) {
return b64
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
};
Enc.bufToUrlBase64 = function(buf) {
return Enc.base64ToUrlBase64(bufToBase64(buf));
};
Enc.strToUrlBase64 = function(str) {
return Enc.base64ToUrlBase64(bufToBase64(Buffer.from(str)));
};
module.exports = Enc;