nurl/base58.js
Martin Donnelly 1209271cd4 Re work
2017-08-05 21:14:17 +01:00

29 lines
714 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;