forked from root/node-base32
add encode base32 func
This commit is contained in:
commit
49df953d19
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules/
|
77
base32.js
Normal file
77
base32.js
Normal file
@ -0,0 +1,77 @@
|
||||
const encodeTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.split('')
|
||||
const decodeTable = {}
|
||||
for (const idx in encodeTable) {
|
||||
decodeTable[encodeTable[idx]] = idx
|
||||
}
|
||||
|
||||
const base32 = {}
|
||||
module.exports = base32
|
||||
|
||||
base32.encode = (b) => {
|
||||
if (!(b instanceof Buffer)) return ''
|
||||
if (b.length === 0) return ''
|
||||
|
||||
let str = ''
|
||||
const len = b.length
|
||||
let v1, v2, v3, v4, v5
|
||||
|
||||
const count = parseInt(len / 5) * 5
|
||||
let i = 0
|
||||
while (i < count) {
|
||||
v1 = b[i++]
|
||||
v2 = b[i++]
|
||||
v3 = b[i++]
|
||||
v4 = b[i++]
|
||||
v5 = b[i++]
|
||||
str += encodeTable[v1 >>> 3] +
|
||||
encodeTable[(v1 << 2 | v2 >>> 6) & 31] +
|
||||
encodeTable[(v2 >>> 1) & 31] +
|
||||
encodeTable[(v2 << 4 | v3 >>> 4) & 31] +
|
||||
encodeTable[(v3 << 1 | v4 >>> 7) & 31] +
|
||||
encodeTable[(v4 >>> 2) & 31] +
|
||||
encodeTable[(v4 << 3 | v5 >>> 5) & 31] +
|
||||
encodeTable[v5 & 31]
|
||||
}
|
||||
|
||||
const remain = len - count
|
||||
switch (remain) {
|
||||
case 1:
|
||||
v1 = b[i]
|
||||
str += encodeTable[v1 >>> 3] +
|
||||
encodeTable[(v1 << 2) & 31] + '======'
|
||||
break
|
||||
case 2:
|
||||
v1 = b[i++]
|
||||
v2 = b[i]
|
||||
str += encodeTable[v1 >>> 3] +
|
||||
encodeTable[(v1 << 2 | v2 >>> 6) & 31] +
|
||||
encodeTable[(v2 >>> 1) & 31] +
|
||||
encodeTable[(v2 << 4) & 31] + '===='
|
||||
break
|
||||
case 3:
|
||||
v1 = b[i++]
|
||||
v2 = b[i++]
|
||||
v3 = b[i]
|
||||
str += encodeTable[v1 >>> 3] +
|
||||
encodeTable[(v1 << 2 | v2 >>> 6) & 31] +
|
||||
encodeTable[(v2 >>> 1) & 31] +
|
||||
encodeTable[(v2 << 4 | v3 >>> 4) & 31] +
|
||||
encodeTable[(v3 << 1) & 31] + '==='
|
||||
break
|
||||
case 4:
|
||||
v1 = b[i++]
|
||||
v2 = b[i++]
|
||||
v3 = b[i++]
|
||||
v4 = b[i]
|
||||
str += encodeTable[v1 >>> 3] +
|
||||
encodeTable[(v1 << 2 | v2 >>> 6) & 31] +
|
||||
encodeTable[(v2 >>> 1) & 31] +
|
||||
encodeTable[(v2 << 4 | v3 >>> 4) & 31] +
|
||||
encodeTable[(v3 << 1 | v4 >>> 7) & 31] +
|
||||
encodeTable[(v4 >>> 2) & 31] +
|
||||
encodeTable[(v4 << 3) & 31] + '='
|
||||
break
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
1699
package-lock.json
generated
Normal file
1699
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
package.json
Normal file
15
package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "node-base32",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"standard": "^12.0.1"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user