forked from root/node-base32
rename encode func name, add decode func
This commit is contained in:
@@ -7,8 +7,13 @@ for (const idx in encodeTable) {
|
||||
const base32 = {}
|
||||
module.exports = base32
|
||||
|
||||
base32.encode = (b) => {
|
||||
if (!(b instanceof Buffer)) return ''
|
||||
/**
|
||||
* encode byte data to string
|
||||
* @param {ArrayBuffer} b buffer data
|
||||
* @return {string}
|
||||
*/
|
||||
base32.encodeToString = (b) => {
|
||||
if (!(b instanceof Buffer)) throw new Error('input not Buffer')
|
||||
if (b.length === 0) return ''
|
||||
|
||||
let str = ''
|
||||
@@ -75,3 +80,87 @@ base32.encode = (b) => {
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
/**
|
||||
* decode base32 string to byte
|
||||
* @param {string} str
|
||||
* @return {ArrayBuffer}
|
||||
*/
|
||||
base32.decodeFromString = (str) => {
|
||||
if (typeof str !== 'string' || str.length === 0) throw new Error('input not string or empty')
|
||||
if (!/^[A-Z2-7=]+$/.test(str)) throw new Error('Invalid base32 characters')
|
||||
str = str.toUpperCase().replace(/=/g, '')
|
||||
|
||||
let v = []
|
||||
const buf = []
|
||||
const len = str.length
|
||||
const count = len >> 3 << 3
|
||||
let i = 0
|
||||
|
||||
while (i < count) {
|
||||
v[0] = decodeTable[str.charAt(i++)]
|
||||
v[1] = decodeTable[str.charAt(i++)]
|
||||
v[2] = decodeTable[str.charAt(i++)]
|
||||
v[3] = decodeTable[str.charAt(i++)]
|
||||
v[4] = decodeTable[str.charAt(i++)]
|
||||
v[5] = decodeTable[str.charAt(i++)]
|
||||
v[6] = decodeTable[str.charAt(i++)]
|
||||
v[7] = decodeTable[str.charAt(i++)]
|
||||
buf.push(
|
||||
(v[0] << 3 | v[1] >>> 2) & 0xff,
|
||||
(v[1] << 6 | v[2] << 1 | v[3] >>> 4) & 0xff,
|
||||
(v[3] << 4 | v[4] >>> 1) & 0xff,
|
||||
(v[4] << 7 | v[5] << 2 | v[6] >>> 3) & 0xff,
|
||||
(v[6] << 5 | v[7]) & 0xff
|
||||
)
|
||||
}
|
||||
|
||||
switch (len - count) {
|
||||
case 2:
|
||||
v[0] = decodeTable[str.charAt(i++)]
|
||||
v[1] = decodeTable[str.charAt(i++)]
|
||||
buf.push(
|
||||
(v[0] << 3 | v[1] >>> 2) & 0xff
|
||||
)
|
||||
break
|
||||
case 4:
|
||||
v[0] = decodeTable[str.charAt(i++)]
|
||||
v[1] = decodeTable[str.charAt(i++)]
|
||||
v[2] = decodeTable[str.charAt(i++)]
|
||||
v[3] = decodeTable[str.charAt(i++)]
|
||||
buf.push(
|
||||
(v[0] << 3 | v[1] >>> 2) & 0xff,
|
||||
(v[1] << 6 | v[2] << 1 | v[3] >>> 4) & 0xff
|
||||
)
|
||||
break
|
||||
case 5:
|
||||
v[0] = decodeTable[str.charAt(i++)]
|
||||
v[1] = decodeTable[str.charAt(i++)]
|
||||
v[2] = decodeTable[str.charAt(i++)]
|
||||
v[3] = decodeTable[str.charAt(i++)]
|
||||
v[4] = decodeTable[str.charAt(i++)]
|
||||
buf.push(
|
||||
(v[0] << 3 | v[1] >>> 2) & 0xff,
|
||||
(v[1] << 6 | v[2] << 1 | v[3] >>> 4) & 0xff,
|
||||
(v[3] << 4 | v[4] >>> 1) & 0xff
|
||||
)
|
||||
break
|
||||
case 7:
|
||||
v[0] = decodeTable[str.charAt(i++)]
|
||||
v[1] = decodeTable[str.charAt(i++)]
|
||||
v[2] = decodeTable[str.charAt(i++)]
|
||||
v[3] = decodeTable[str.charAt(i++)]
|
||||
v[4] = decodeTable[str.charAt(i++)]
|
||||
v[5] = decodeTable[str.charAt(i++)]
|
||||
v[6] = decodeTable[str.charAt(i++)]
|
||||
buf.push(
|
||||
(v[0] << 3 | v[1] >>> 2) & 0xff,
|
||||
(v[1] << 6 | v[2] << 1 | v[3] >>> 4) & 0xff,
|
||||
(v[3] << 4 | v[4] >>> 1) & 0xff,
|
||||
(v[4] << 7 | v[5] << 2 | v[6] >>> 3) & 0xff
|
||||
)
|
||||
break
|
||||
}
|
||||
|
||||
return Buffer.from(buf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user