Created
December 8, 2010 23:46
-
-
Save altamic/734133 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Bitcoin | |
module Base58 | |
## | |
# TODO: I need to study: RFC 2045, RFC 4648. | |
# | |
B58_1, B58_9 = ?1, ?9 # (1..9).count => 9 | |
B58_A, B58_H = ?A, ?H # (?A..?H).count => 8 | |
B58_J, B58_N = ?J, ?N # (?J..?N).count => 5 | |
B58_P, B58_Z = ?P, ?Z # (?P..?Z).count => 11 | |
B58_a, B58_k = ?a, ?k # (?a..?k).count => 11 | |
B58_m, B58_z = ?m, ?z # (?m..?z).count => 14 | |
B58_CHARS = [ | |
(B58_1..B58_9), (B58_A..B58_H), (B58_J..B58_N), | |
(B58_P..B58_Z), (B58_a..B58_k), (B58_m..B58_z) | |
].map { |range| range.to_a }.flatten.map! { |i| i.chr } | |
B58_SIZE = B58_CHARS.size | |
self.constants.each {|const| const.freeze} | |
module Number | |
def encode58 | |
return nil if self.nil? || self < 0 | |
string = "" | |
value = self | |
begin | |
case mod = value % 58 | |
when 0..8 then string << (mod - 0 + B58_1) | |
when 9..15 then string << (mod - 9 + B58_A) | |
when 16..21 then string << (mod - 16 + B58_J) | |
when 22..32 then string << (mod - 22 + B58_P) | |
when 33..43 then string << (mod - 33 + B58_a) | |
when 44...58 then string << (mod - 44 + B58_m) | |
end | |
value = value / 58 | |
end until value == 0 | |
string.reverse | |
end | |
end | |
module String | |
def decode58 | |
return nil unless self.base58? | |
acc = 0 | |
key = self.reverse | |
key.size.times do |i| | |
char = key[i] | |
case | |
when (B58_1..B58_9).include?(char) then digit = char - B58_1 | |
when (B58_A..B58_H).include?(char) then digit = char - B58_A + 9 | |
when (B58_J..B58_N).include?(char) then digit = char - B58_J + 16 | |
when (B58_P..B58_Z).include?(char) then digit = char - B58_P + 22 | |
when (B58_a..B58_k).include?(char) then digit = char - B58_a + 33 | |
when (B58_m..B58_z).include?(char) then digit = char - B58_m + 44 | |
end | |
acc = acc + (digit * (58 ** i)) | |
end | |
acc | |
end | |
def base58? | |
return false if self.nil? || self.strip.size.zero? | |
string = self.dup | |
string.each_char do |char| | |
return false unless B58_CHARS.include?(char) | |
end | |
true | |
end | |
end | |
end | |
end | |
class String | |
include Bitcoin::Base58::String | |
end | |
class Fixnum | |
include Bitcoin::Base58::Number | |
end | |
class Bignum | |
include Bitcoin::Base58::Number | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment