Skip to content

Instantly share code, notes, and snippets.

@vglebov
Last active August 7, 2017 10:01
Show Gist options
  • Save vglebov/5d8c1f4ad625c539a7b494f358338fff to your computer and use it in GitHub Desktop.
Save vglebov/5d8c1f4ad625c539a7b494f358338fff to your computer and use it in GitHub Desktop.
class Base58
ALPHABET = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
BASE = ALPHABET.length
# Converts a base58 string to a base10 integer.
def self.base58_to_int(base58_val)
int_val = 0
base58_val.reverse.split(//).each_with_index do |char,index|
raise ArgumentError, 'Value passed not a valid Base58 String.' if (char_index = ALPHABET.index(char)).nil?
int_val += (char_index)*(BASE**(index))
end
int_val
end
# Converts a base10 integer to a base58 string.
def self.int_to_base58(int_val)
raise ArgumentError, 'Value passed is not an Integer.' unless int_val.is_a?(Integer)
base58_val = ''
while(int_val >= BASE)
mod = int_val % BASE
base58_val = ALPHABET[mod,1] + base58_val
int_val = (int_val - mod)/BASE
end
ALPHABET[int_val,1] + base58_val
end
class << self
alias_method :encode, :int_to_base58
alias_method :decode, :base58_to_int
end
end
h = '19df3a4355c44479a0f47a9557f17270'
dec = h.to_i 16
p h
p dec
p dec.to_s 16
p dec.to_s 36
p Base58.int_to_base58 dec
b58 = Base58.int_to_base58 'F00119df3a4355c44479a0f47a9557f17270'.to_i 16
p b58
p Base58.base58_to_int(b58).to_s(16).upcase
line = "hello world! \nsdfasd \nasdf adsf adsfas df asdf asdfadsf asdf dsv dfvdf vs dfvs fvs fvf "
p line
hh = line.unpack('H*')[0]
p hh
dec = hh.to_i(16)
p dec
p Base58.int_to_base58 dec
p dec.to_s 2
"19df3a4355c44479a0f47a9557f17270"
34389763808663562296386380833253061232
"19df3a4355c44479a0f47a9557f17270"
"1j528y38k9lp57znw45ipnpm8"
"4cifrZNdDP6U5qaHZBGQnw"
"aXnuk4UfTZaDWnPEbg729eJSd"
"F00119DF3A4355C44479A0F47A9557F17270"
"hello world! \nsdfasd \nasdf adsf adsfas df asdf asdfadsf asdf dsv dfvdf vs dfvs fvs fvf "
"68656c6c6f20776f726c6421200a736466617364200a6173646620616473662061647366617320646620617364662061736466616473662061736466206473762064667664662076732064667673206676732066766620"
134066924850456263226002610823472315195755895538175650465665490401217181528305704879248544682572954165876967214096961682849075560083288808177085324632235450912211952490312091713875114464485397955114557427312160
"c36Fk5psu29EekbYZUJJFua59cUbzTCER3wjw3YKc73mJYup1u3Ge1Zx6rLMZNTxjYihUpoorbrZKM5eNLm8Wr6zkmY1qsAWct5gLhFwdEwNdsHgW4ufPhm"
"11010000110010101101100011011000110111100100000011101110110111101110010011011000110010000100001001000000000101001110011011001000110011001100001011100110110010000100000000010100110000101110011011001000110011000100000011000010110010001110011011001100010000001100001011001000111001101100110011000010111001100100000011001000110011000100000011000010111001101100100011001100010000001100001011100110110010001100110011000010110010001110011011001100010000001100001011100110110010001100110001000000110010001110011011101100010000001100100011001100111011001100100011001100010000001110110011100110010000001100100011001100111011001110011001000000110011001110110011100110010000001100110011101100110011000100000"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment