This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
201 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict' | ||
|
||
function decode (input, alphabet) { | ||
input = input.replace(new RegExp('=', 'g'), '') | ||
let length = input.length | ||
|
||
let bits = 0 | ||
let value = 0 | ||
|
||
let index = 0 | ||
let output = new Uint8Array((length * 5 / 8) | 0) | ||
|
||
for (let i = 0; i < length; i++) { | ||
value = (value << 5) | alphabet.indexOf(input[i]) | ||
bits += 5 | ||
|
||
if (bits >= 8) { | ||
output[index++] = (value >>> (bits - 8)) & 255 | ||
bits -= 8 | ||
} | ||
} | ||
|
||
return output.buffer | ||
} | ||
|
||
function encode (buffer, alphabet) { | ||
let length = buffer.byteLength | ||
let view = new Uint8Array(buffer) | ||
let padding = alphabet.indexOf('=') === alphabet.length - 1 | ||
|
||
if (padding) { | ||
alphabet = alphabet.substring(0, alphabet.length - 2) | ||
} | ||
|
||
let bits = 0 | ||
let value = 0 | ||
let output = '' | ||
|
||
for (let i = 0; i < length; i++) { | ||
value = (value << 8) | view[i] | ||
bits += 8 | ||
|
||
while (bits >= 5) { | ||
output += alphabet[(value >>> (bits - 5)) & 31] | ||
bits -= 5 | ||
} | ||
} | ||
|
||
if (bits > 0) { | ||
output += alphabet[(value << (5 - bits)) & 31] | ||
} | ||
|
||
if (padding) { | ||
while ((output.length % 8) !== 0) { | ||
output += '=' | ||
} | ||
} | ||
|
||
return output | ||
} | ||
|
||
module.exports = function base32 (alphabet) { | ||
return { | ||
encode (input) { | ||
if (typeof input === 'string') { | ||
return encode(new Buffer(input), alphabet) | ||
} | ||
return encode(input, alphabet) | ||
}, | ||
decode (input) { | ||
for (let char of input) { | ||
if (alphabet.indexOf(char) < 0) { | ||
throw new Error('invalid base32 character') | ||
} | ||
} | ||
return decode(input, alphabet) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
|
||
module.exports = function base64 (alphabet) { | ||
// The alphabet is only used to know if padding is enabled or | ||
// if this should be url-encoded. The actual alphabet will | ||
// always be the one from RFC 4648. | ||
const padding = alphabet.indexOf('=') > -1 | ||
const url = alphabet.indexOf('-') > -1 && alphabet.indexOf('_') > -1 | ||
|
||
if (alphabet.length !== 64 && !padding) { | ||
throw new Error('invalid base64 alphabet') | ||
} | ||
|
||
return { | ||
encode (input) { | ||
let output = '' | ||
|
||
if (typeof input === 'string') { | ||
output = new Buffer(input).toString('base64') | ||
} else { | ||
output = input.toString('base64') | ||
} | ||
|
||
if (url) { | ||
output = output.replace('+', '-') | ||
output = output.replace('/', '_') | ||
} | ||
|
||
const pad = output.indexOf('=') | ||
if (pad > 0 && !padding) { | ||
output = output.substring(0, pad) | ||
} | ||
|
||
return output | ||
}, | ||
decode (input) { | ||
if (url) { | ||
input = input.replace('+', '-') | ||
input = input.replace('/', '_') | ||
} | ||
|
||
for (let char of input) { | ||
if (alphabet.indexOf(char) < 0 && !(char === '=' && padding)) { | ||
throw new Error('invalid base64 character') | ||
} | ||
} | ||
|
||
return new Buffer(input, 'base64') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters