Class: ROTP::Base32

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

Defined Under Namespace

Classes: Base32Error

Constant Summary collapse

CHARS =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.each_char.to_a
SHIFT =
5
MASK =
31

Class Method Summary collapse

Class Method Details

.decode(str) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rotp/base32.rb', line 12

def decode(str)
  buffer = 0
  idx = 0
  bits_left = 0
  str = str.tr('=', '').upcase
  result = []
  str.split('').each do |char|
    buffer = buffer << SHIFT
    buffer = buffer | (decode_quint(char) & MASK)
    bits_left = bits_left + SHIFT
    if bits_left >= 8
      result[idx] = (buffer >> (bits_left - 8)) & 255
      idx = idx + 1
      bits_left = bits_left - 8
    end
  end
  result.pack('c*')
end

.encode(b) ⇒ Object



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/rotp/base32.rb', line 31

def encode(b)
  data = b.unpack('c*')
  out = String.new
  buffer = data[0]
  idx = 1
  bits_left = 8
  while bits_left > 0 || idx < data.length
    if bits_left < SHIFT
      if idx < data.length
        buffer = buffer << 8
        buffer = buffer | (data[idx] & 255)
        bits_left = bits_left + 8
        idx = idx + 1
      else
        pad = SHIFT - bits_left
        buffer = buffer << pad
        bits_left = bits_left + pad
      end
    end
    val = MASK & (buffer >> (bits_left - SHIFT))
    bits_left = bits_left - SHIFT
    out.concat(CHARS[val])
  end
  return out
end

.random(byte_length = 20) ⇒ Object

Defaults to 160 bit long secret (meaning a 32 character long base32 secret)



58
59
60
61
# File 'lib/rotp/base32.rb', line 58

def random(byte_length = 20)
 rand_bytes = SecureRandom.random_bytes(byte_length)
 self.encode(rand_bytes)
end

.random_base32(str_len = 32) ⇒ Object

Prevent breaking changes



64
65
66
67
# File 'lib/rotp/base32.rb', line 64

def random_base32(str_len = 32)
  byte_length = str_len * 5/8
  random(byte_length)
end