Sha256: edd4b4098a96b71db4245c5a4a2f82289c1c9e663b9dc9ff197d2300f64d5bb8
Contents?: true
Size: 1.76 KB
Versions: 1
Compression:
Stored size: 1.76 KB
Contents
module ROTP class Base32 class Base32Error < RuntimeError; end CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.each_char.to_a SHIFT = 5 MASK = 31 class << self 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 def encode(b) data = b.unpack('c*') out = '' 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 # Defaults to 160 bit long secret (meaning a 32 character long base32 secret) def random(byte_length = 20) rand_bytes = SecureRandom.random_bytes(byte_length) self.encode(rand_bytes) end private def decode_quint(q) CHARS.index(q) || raise(Base32Error, "Invalid Base32 Character - '#{q}'") end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rotp-5.0.0 | lib/rotp/base32.rb |