Created
June 14, 2012 09:50
-
-
Save inflammable/2929362 to your computer and use it in GitHub Desktop.
Base58 (and other) Encoding and Decoding in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* base58.js | |
* - encodes integers to and decodes from a base58 (or your own) base58 alphabet | |
* - based on Flickr's url shortening | |
* | |
* usage: | |
* base58.encode(integer); | |
* base58.decode(string); | |
* | |
* (c) 2012 inflammable/raromachine | |
* Licensed under the MIT License. | |
* | |
*/ | |
var base58 = (function(alpha) { | |
var alphabet = alpha || '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', | |
base = alphabet.length; | |
return { | |
encode: function(enc) { | |
if(typeof enc!=='number' || enc !== parseInt(enc)) | |
throw '"encode" only accepts integers.'; | |
var encoded = ''; | |
while(enc) { | |
var remainder = enc % base; | |
enc = Math.floor(enc / base); | |
encoded = alphabet[remainder].toString() + encoded; | |
} | |
return encoded; | |
}, | |
decode: function(dec) { | |
if(typeof dec!=='string') | |
throw '"decode" only accepts strings.'; | |
var decoded = 0; | |
while(dec) { | |
var alphabetPosition = alphabet.indexOf(dec[0]); | |
if (alphabetPosition < 0) | |
throw '"decode" can\'t find "' + dec[0] + '" in the alphabet: "' + alphabet + '"'; | |
var powerOf = dec.length - 1; | |
decoded += alphabetPosition * (Math.pow(base, powerOf)); | |
dec = dec.substring(1); | |
} | |
return decoded; | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fails on base58.encode(0), convert the while loop to a do/while loop