Class: BitcoinCigs::Base58

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

Constant Summary collapse

CHARS =
'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
CHAR_SIZE =
CHARS.size

Class Method Summary collapse

Class Method Details

.decode(s) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bitcoin_cigs/base_58.rb', line 6

def self.decode(s)
  value = s.chars.reverse_each.each_with_index.inject(0) { |acc, (ch, i)| acc + (CHARS.index(ch) || -1) * (CHAR_SIZE ** i) }
  
  result = []
  while value >= 256
    div = value / 256
    mod = value % 256
    result.unshift(mod.chr)
    value = div
  end
  result.unshift(value.chr)
  
  pad_size = s.chars.inject(0) { |acc, ch| acc + (CHARS[0] == ch ? 1 : 0) }
  
  result.unshift(0.chr * pad_size)
  
  result.join
end

.encode(s) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bitcoin_cigs/base_58.rb', line 25

def self.encode(s)
  value = 0
  
  value = s.chars.reverse_each.each_with_index.inject(0) { |acc, (ch, i)| acc + (256 ** i) * ch.ord }

  result = []
  while value >= CHAR_SIZE
    div, mod = value / CHAR_SIZE, value % CHAR_SIZE
    result.unshift(CHARS[mod])
    value = div
  end
  result.unshift(CHARS[value])

  pad_size = s.chars.inject(0) { |acc, ch| acc + (ch == "\0" ? 1 : 0) }
  
  result.unshift(CHARS[0] * pad_size)

  result.join
end