Skip to content

Instantly share code, notes, and snippets.

@tomtaylor
Created April 25, 2013 06:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomtaylor/5457965 to your computer and use it in GitHub Desktop.
Save tomtaylor/5457965 to your computer and use it in GitHub Desktop.
Base58 encoder and decoder in Ruby, suitable for converting to/from Flickr short URLs.
module Base58
class InvalidCharacterError < RuntimeError; end
ALPHABET =
%w(
1 2 3 4 5 6 7 8 9
a b c d e f g h i
j k m n o p q r s
t u v w x y z A B
C D E F G H J K L
M N P Q R S T U V
W X Y Z
)
BASE = ALPHABET.length
def self.encode(n)
return ALPHABET[0] if n == 0
buffer = String.new
while n > 0
remainder = n % BASE
n = n / BASE
buffer = ALPHABET[remainder] + buffer
end
return buffer
end
def self.decode(string)
n = 0
power = string.length - 1
string.each_char do |c|
position = ALPHABET.index(c)
raise InvalidCharacterError if position.nil?
n += position * (BASE ** power)
power -= 1
end
return n
end
end
@njmurarka
Copy link

ALPHABET =
%w(
1 2 3 4 5 6 7 8 9
A B C D E F G H J
K L M N P Q R S T
U V W X Y Z a b c
d e f g h i j k m
n o p q r s t u v
w x y z
)

@ilyacherevkov
Copy link

It depends, Flickr uses, for example, preceding lowercase

@sacredstarfamily
Copy link

why no lower case l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment