summaryrefslogtreecommitdiff
path: root/vendor/base32/lib/base32.rb
blob: 4df2b1aed23ba1f0d2be79c5865bbe0e157d1d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
56
57
58
59
60
61
62
63
64
65
66
67
require 'openssl'

# Module for encoding and decoding in Base32 per RFC 3548
module Base32
  TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.freeze

  class Chunk
    def initialize(bytes)
      @bytes = bytes
    end

    def decode
      bytes = @bytes.take_while {|c| c != 61} # strip padding
      n = (bytes.length * 5.0 / 8.0).floor
      p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0
      c = bytes.inject(0) {|m,o| (m << 5) + Base32.table.index(o.chr)} >> p
      (0..n-1).to_a.reverse.collect {|i| ((c >> i * 8) & 0xff).chr}
    end

    def encode
      n = (@bytes.length * 8.0 / 5.0).ceil
      p = n < 8 ? 5 - (@bytes.length * 8) % 5 : 0
      c = @bytes.inject(0) {|m,o| (m << 8) + o} << p
      [(0..n-1).to_a.reverse.collect {|i| Base32.table[(c >> i * 5) & 0x1f].chr},
       ("=" * (8-n))]
    end
  end

  def self.chunks(str, size)
    result = []
    bytes = str.bytes
    while bytes.any? do
      result << Chunk.new(bytes.take(size))
      bytes = bytes.drop(size)
    end
    result
  end

  def self.encode(str)
    chunks(str, 5).collect(&:encode).flatten.join
  end

  def self.decode(str)
    chunks(str, 8).collect(&:decode).flatten.join
  end

  def self.random_base32(length=16, padding=true)
    random = ''
    OpenSSL::Random.random_bytes(length).each_byte do |b|
      random << self.table[b % 32]
    end
    padding ? random.ljust((length / 8.0).ceil * 8, '=') : random
  end

  def self.table=(table)
    raise ArgumentError, "Table must have 32 unique characters" unless self.table_valid?(table)
    @table = table
  end

  def self.table
    @table || TABLE
  end

  def self.table_valid?(table)
    table.bytes.to_a.size == 32 && table.bytes.to_a.uniq.size == 32
  end
end