nurl/base58.js
2017-09-25 17:48:55 +01:00

29 lines
700 B
JavaScript

// var alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
const alphabet = 'bMJZSrnxEyq8kN3UYQL5oXwV7BCFRtvpmDf1shAuHzKicTjeG29Pg4adW6';
const base = alphabet.length;
function encode(num) {
let encoded = '';
while (num) {
const remainder = num % base;
num = Math.floor(num / base);
encoded = alphabet[remainder].toString() + encoded;
}
return encoded;
}
function decode(str) {
let decoded = 0;
while (str) {
const index = alphabet.indexOf(str[0]);
const power = str.length - 1;
decoded += index * (Math.pow(base, power));
str = str.substring(1);
}
return decoded;
}
module.exports.encode = encode;
module.exports.decode = decode;