Class: Base58::Base
- Inherits:
-
Object
- Object
- Base58::Base
- Defined in:
- lib/base58-alphabets/base.rb
Direct Known Subclasses
Class Method Summary collapse
-
.decode(str_or_bytes) ⇒ Object
Converts a base58 string to a base10 integer.
-
.encode(num_or_bytes) ⇒ Object
Converts a base10 integer to a base58 string.
Class Method Details
.decode(str_or_bytes) ⇒ Object
Converts a base58 string to a base10 integer.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/base58-alphabets/base.rb', line 25 def self.decode( str_or_bytes ) if str_or_bytes.is_a?( Array ) bytes = str_or_bytes else ## assume string str = str_or_bytes bytes = str.each_char.reduce([]) do |bytes,char| byte = number[char] raise ArgumentError, "Value passed not a valid base58 string - >#{char}< not found in alphabet" if byte.nil? bytes << byte bytes end end Base58._pack( bytes ) end |
.encode(num_or_bytes) ⇒ Object
Converts a base10 integer to a base58 string.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/base58-alphabets/base.rb', line 9 def self.encode( num_or_bytes ) if num_or_bytes.is_a?( Array ) bytes = num_or_bytes else num = num_or_bytes bytes = Base58._bytes( num ) end bytes.reduce( String.new ) do |buf, byte| buf << alphabet[byte] buf end end |