class Ronin::Support::Encoding::Base32::Chunk
Represents a chunk of data.
@api private
Public Class Methods
new(bytes)
click to toggle source
Initializes the chunk.
@param [Array<Integer>] bytes
The bytes for the chunk.
# File lib/ronin/support/encoding/base32.rb, line 93 def initialize(bytes) @bytes = bytes end
Public Instance Methods
decode(output=String.new)
click to toggle source
Decodes the chunk.
@param [String] output
Optional output buffer.
@return [String]
The Base32 decoded chunk.
# File lib/ronin/support/encoding/base32.rb, line 106 def decode(output=String.new) bytes = @bytes.take_while { |b| b != 61 } # strip padding n = ((bytes.length * 5.0) / 8.0).floor p = if bytes.length < 8 5 - ((n * 8) % 5) else 0 end c = bytes.reduce(0) { |m,o| unless (i = Base32::TABLE.index(o.chr)) raise ArgumentError, "invalid character '#{o.chr}'" end (m << 5) + i } >> p (0..(n - 1)).reverse_each do |i| output << ((c >> (i * 8)) & 0xff).chr end return output end
encode(output=String.new)
click to toggle source
Encodes the chunk.
@param [String] output
Optional output buffer.
@return [String]
The Base32 encoded chunk.
# File lib/ronin/support/encoding/base32.rb, line 139 def encode(output=String.new) n = ((@bytes.length * 8.0) / 5.0).ceil p = if n < 8 5 - ((@bytes.length * 8) % 5) else 0 end c = @bytes.inject(0) { |m,o| (m << 8) + o } << p (0..(n - 1)).reverse_each do |i| output << Base32::TABLE[(c >> (i * 5)) & 0x1f].chr end return output << ("=" * (8 - n)) end