Module: Base32H

Extended by:
Base32H
Included in:
Base32H
Defined in:
lib/base32h.rb

Overview

This module provides Base32H encoding and decoding functions.

Constant Summary collapse

VERSION =
"0.1.1"

Encoders collapse

Decoders collapse

Instance Method Summary collapse

Instance Method Details

#decode(str) ⇒ Integer

Decodes a Base32H number to its integer representation.

Parameters:

  • str (#to_s)

    the number to decode

Returns:

  • (Integer)

    the decoded integer



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/base32h.rb', line 130

def decode(str)
  res = str.to_s.chars.reverse.reduce({acc: 0, exp: 0}) do |state, char|
    digit = decode_digit(char)
    if digit.nil?
      state
    else
      {acc: state[:acc] + (digit * 32**state[:exp]),
       exp: state[:exp] + 1}
    end
  end
  res[:acc]
end

#decode_bin(str) ⇒ String

Decodes a Base32H binary into a string of packed unsigned bytes.

Parameters:

  • str (String)

    the binary to decode

Returns:

  • (String)

    the decoded binary



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/base32h.rb', line 149

def decode_bin(str)
  data = str.chars.reject {|c| decode_digit(c).nil?}
  extra = data.length % 8
  data = (['0'] * (8 - extra)) + data if extra > 0
  out = []
  data.each_slice(8) do |s|
    chunk = u40_to_bytes decode(s.join '')
    out += chunk
  end
  out.pack('C*')
end

#decode_digit(d) ⇒ Integer

Decodes a single Base32H digit to its integer representation. Returns nil if the input is not a Base32H digit.

Parameters:

  • d (#to_s)

    the digit to decode

Returns:

  • (Integer)

    the digit’s value



119
120
121
122
# File 'lib/base32h.rb', line 119

def decode_digit(d)
  d = d.to_s
  digits.find_index {|i| i.include? d}
end

#digitsArray<String>

The complete list of Base32H digits, as an array of strings. The index of the array is the numeric value of the digit(s) in the string at that index. The first character in each string is the “canonical” digit (i.e. the one that Base32H-conformant encoders must emit for that value); all other characters (if present) are “aliases” (i.e. alternate forms of that digit that Base32H-conformant decoders must accept and correctly decode to that value).

Examples:

Base32H.digits[27] #=> 'VvUu'

Returns:

  • (Array<String>)

    the full list of digits



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/base32h.rb', line 22

def digits
  ['0Oo',
   '1Ii',
   '2',
   '3',
   '4',
   '5Ss',
   '6',
   '7',
   '8',
   '9',
   'Aa',
   'Bb',
   'Cc',
   'Dd',
   'Ee',
   'Ff',
   'Gg',
   'Hh',
   'Jj',
   'Kk',
   'Ll',
   'Mm',
   'Nn',
   'Pp',
   'Qq',
   'Rr',
   'Tt',
   'VvUu',
   'Ww',
   'Xx',
   'Yy',
   'Zz']
end

#encode(int) ⇒ String

Encodes an integer to its Base32H representation.

Parameters:

  • int (#to_i)

    the integer to encode

Returns:

  • (String)

    the encoded number



79
80
81
82
83
84
85
86
87
88
# File 'lib/base32h.rb', line 79

def encode(int)
  rem = int.to_i.abs
  out = []
  return '0' if rem == 0
  while rem > 0 do
    out.unshift(encode_digit(rem % 32))
    rem /= 32
  end
  out.join ''
end

#encode_bin(bin) ⇒ String

Encodes a (binary) string to its Base32H representation.

Parameters:

  • bin (String)

    the string/binary to encode

Returns:

  • (String)

    the encoded binary



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/base32h.rb', line 96

def encode_bin(bin)
  data = bin.unpack('C*')
  extra = data.length % 5
  data = ([0] * (5 - extra)) + data if extra > 0
  out = []
  data.each_slice(5) do |s|
    chunk = bytes_to_u40 s
    out.push encode(chunk).rjust(8, '0')
  end
  out.join ''
end

#encode_digit(d) ⇒ String, NilClass

Encodes an integer between 0 and 31 (inclusive) to its Base32H representation. Returns nil for input values outside that range.

Parameters:

  • d (#to_i)

    the integer value of the requested digit

Returns:

  • (String, NilClass)

    the resulting digit



67
68
69
70
71
# File 'lib/base32h.rb', line 67

def encode_digit(d)
  d = d.to_i
  return nil unless (0..31).include? d
  digits[d][0]
end