Module: Faker::Base58

Defined in:
lib/helpers/base58.rb

Class Method Summary collapse

Class Method Details

.encode(str) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/helpers/base58.rb', line 5

def self.encode(str)
  alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  base = alphabet.size

  lv = 0
  str.split('').reverse.each_with_index { |v, i| lv += v.unpack('C')[0] * 256**i }

  ret = +''
  while lv.positive?
    lv, mod = lv.divmod(base)
    ret << alphabet[mod]
  end

  npad = str.match(/^#{0.chr}*/)[0].to_s.size
  '1' * npad + ret.reverse
end