Skip to content

Instantly share code, notes, and snippets.

@maxogden

maxogden/index.js

Last active Feb 1, 2020
Embed
What would you like to do?
JavaScript Base-40 Class
// based on https://github.com/blockstack/blockstack-core/blob/ff86948ed2f720824cd5e6ece6a63aaaf2bf81ff/blockstack/lib/b40.py
class B40 {
constructor() {
this.B16_CHARS = '0123456789abcdef'
this.B40_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz-_.+'
}
divmod(x, y) {
const div = x / y
const rem = x % y
return [div, rem]
}
intToCharset(val, charset) {
if (val < BigInt(0)) return
if (val === BigInt(0)) return charset[0]
let output = ''
while (val > BigInt(0)) {
let [newval, digit] = this.divmod(val, BigInt(charset.length))
val = newval
output += charset[digit]
}
return output
.split('')
.reverse()
.join('')
}
charsetToInt(str, charset) {
let output = BigInt(0)
str.split('').forEach(function(char) {
output = output * BigInt(charset.length) + BigInt(charset.indexOf(char))
})
return output
}
convertCharset(str, original, target) {
let intermediate = this.charsetToInt(str, original)
return this.intToCharset(intermediate, target)
}
hexPad(x) {
return (x.length % 2) > 0 ? ('0' * (x.length % 2)) + x : x
}
charsetToHex(str, charset) {
return this.hexPad(this.convertCharset(str, charset, this.B16_CHARS))
}
hexToCharset(str, charset) {
return this.convertCharset(str, this.B16_CHARS, charset)
}
base40ToBin(str) {
return Buffer.from(this.charsetToHex(str, this.B40_CHARS), 'hex')
}
binToBase40(buf) {
return this.hexToCharset(buf.toString('hex'), this.B40_CHARS)
}
base40ToHex(str) {
return this.base40ToBin(str).toString('hex')
}
}
export default B40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.