lib/base58.rb in base58-0.2.1 vs lib/base58.rb in base58-0.2.2
- old
+ new
@@ -51,20 +51,29 @@
prefix = ALPHABETS[alphabet][0] * nzeroes
else
prefix = ''
end
- prefix + int_to_base58(binary_val.bytes.inject{|a,b|(a<<8)+b}, alphabet)
+ prefix + int_to_base58(binary_val.unpack('H*')[0].to_i(16), alphabet)
end
# Converts a base58 string to an ASCII-8BIT (binary) encoded string.
# All leading zeroes in the base58 input are preserved and converted to
# "\x00" in the output.
def self.base58_to_binary(base58_val, alphabet = :flickr)
raise ArgumentError, 'Invalid alphabet selection.' unless ALPHABETS.include?(alphabet)
nzeroes = base58_val.chars.find_index{|c| c != ALPHABETS[alphabet][0]} || base58_val.length-1
prefix = nzeroes < 0 ? '' : '00' * nzeroes
- [prefix + base58_to_int(base58_val, alphabet).to_s(16)].pack('H*')
+ [prefix + Private::int_to_hex(base58_to_int(base58_val, alphabet))].pack('H*')
+ end
+
+ module Private
+ def self.int_to_hex int
+ hex = int.to_s(16)
+ # The hex string must always consist of an even number of characters,
+ # otherwise the pack() parsing will be misaligned.
+ (hex.length % 2 == 0) ? hex : ('0'+hex)
+ end
end
class << self
alias_method :encode, :int_to_base58
alias_method :decode, :base58_to_int