Class: Base58

Inherits:
Object
  • Object
show all
Defined in:
lib/base58.rb

Overview

Base58 Copyright © 2009 Douglas F Shearer. douglasfshearer.com Distributed under the MIT license as included with this plugin.

Constant Summary collapse

ALPHABET =
"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
BASE =
ALPHABET.length

Class Method Summary collapse

Class Method Details

.base58_to_int(base58_val) ⇒ Object Also known as: decode

Converts a base58 string to a base10 integer.



12
13
14
15
16
17
18
19
# File 'lib/base58.rb', line 12

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

.int_to_base58(int_val) ⇒ Object Also known as: encode

Converts a base10 integer to a base58 string.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
# File 'lib/base58.rb', line 22

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