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
4 changed files
with
99 additions
and
4 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,77 @@ | ||
'use strict' | ||
|
||
function readChar (alphabet, char) { | ||
let idx = alphabet.indexOf(char) | ||
|
||
if (idx === -1) { | ||
throw new Error('invalid character found: ' + char) | ||
} | ||
|
||
return idx | ||
} | ||
|
||
function decode (input, alphabet) { | ||
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) | readChar(alphabet, 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 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] | ||
} | ||
|
||
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
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