Class: Ronin::Support::Encoding::Base32::Chunk Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/encoding/base32.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents a chunk of data.

Since:

  • 1.0.0

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Chunk

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the chunk.

Parameters:

Since:

  • 1.0.0



93
94
95
# File 'lib/ronin/support/encoding/base32.rb', line 93

def initialize(bytes)
  @bytes = bytes
end

Instance Method Details

#decode(output = String.new) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decodes the chunk.

Parameters:

  • output (String) (defaults to: String.new)

    Optional output buffer.

Returns:

  • (String)

    The Base32 decoded chunk.

Since:

  • 1.0.0



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encodes the chunk.

Parameters:

  • output (String) (defaults to: String.new)

    Optional output buffer.

Returns:

  • (String)

    The Base32 encoded chunk.

Since:

  • 1.0.0



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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