Sha256: 7b5be5e5681f6b2acb225a6c5835065bab3673e81c87cdddc4bd5dc363915078

Contents?: true

Size: 597 Bytes

Versions: 2

Compression:

Stored size: 597 Bytes

Contents

# frozen_string_literal: true

require 'openssl'

# Module for encoding and decoding in Base32 per RFC 3548
module Base32
  class Random
    attr_reader :length, :padding

    def initialize(length = 16, padding = true)
      @length  = length
      @padding = padding
    end

    def call
      random = ''

      OpenSSL::Random.random_bytes(length).each_byte do |b|
        random += alphabet.to_s[b % 32]
      end
      padding ? random.ljust((length / 8.0).ceil * 8, '=') : random
    end

    def alphabet
      @alphabet ||= Base32::Alphabet.new Base32::Alphabet::CHARS
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
base32.rb-0.4.2 lib/base32/random.rb
base32.rb-0.4.1 lib/base32/random.rb