Sha256: 5a5e584111c1d296e8431bc95239186dc1ac16183be4587099144a9ccd9ed225
Contents?: true
Size: 1.16 KB
Versions: 6
Compression:
Stored size: 1.16 KB
Contents
module Bitcoin # Base58Check encoding # https://en.bitcoin.it/wiki/Base58Check_encoding module Base58 module_function ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' SIZE = ALPHABET.size # encode hex value to base58 string. def encode(hex) leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2 int_val = hex.to_i(16) base58_val = '' while int_val > 0 int_val, remainder = int_val.divmod(SIZE) base58_val = ALPHABET[remainder] + base58_val end ('1' * leading_zero_bytes) + base58_val end # decode base58 string to hex value. def decode(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 * (SIZE**index) end s = int_val.to_s(16) s = (s.bytesize.odd? ? '0' + s : s) s = '' if s == '00' leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size s = ('00' * leading_zero_bytes) + s if leading_zero_bytes > 0 s end end end
Version data entries
6 entries across 6 versions & 1 rubygems