Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d5758c3
Version bump to 1.2.0.
postmodern Aug 1, 2024
94e175e
Added a `Encoding::SMTP` alias to `QuotedPrintable` (closes #522).
postmodern Aug 1, 2024
941fdec
Added `Ronin::Support::Crypto::Cipher::DES3` (closes #524).
postmodern Aug 9, 2024
317c88f
Added `Crypto.des3_cipher`, `.des3_encrypt`, `.des3_decrypt` (closes …
postmodern Aug 9, 2024
ba54ae8
Added `Crypto::Mixin#crypto_des3_cipher`, `#crypto_des3_encrypt`, `#c…
postmodern Aug 9, 2024
a3495fc
Ruby 3.0's openssl does not support the `des3-wrap` cipher.
postmodern Aug 9, 2024
b54493a
Added `String#des3_encrypt` and `String#des3_decrypt` (closes #527).
postmodern Aug 9, 2024
a5afff4
Added `File.des3_encrypt` and `File.des3_decrypt` (closes #528).
postmodern Aug 9, 2024
c85992b
Added `Encoding::PHP` (closes #518).
postmodern Aug 16, 2024
cffc8ce
Replaced `Encoding::RUBY::UNESCAPE_CHARS` with `BACKSLASH_CHARS`.
postmodern Aug 16, 2024
94beeb7
Added `Encoding::NodeJS` (closes #535).
postmodern Aug 17, 2024
2800246
Added `Encoding::Python` (closes #533).
postmodern Aug 17, 2024
bd2ea94
Added `Network::URL` (closes #536).
postmodern Aug 17, 2024
08b0d03
Avoid including `URI::QueryParams::Mixin` twice.
postmodern Aug 18, 2024
01fdfd4
Added `Encoding::Perl` (closes #534).
postmodern Aug 19, 2024
c5510c5
Added `Network::Defang` (closes #540, #541, #516, #517, #542).
postmodern Nov 18, 2024
35fe6f5
Added `Network::Host#defang` (closes #543).
postmodern Nov 18, 2024
ab40733
Added `Network::IP#defang` (closes #544).
postmodern Nov 18, 2024
9803c06
Added `Network::URL#defang` (closes #545).
postmodern Nov 18, 2024
b95da92
Added `Network::Wildcard#===` / `#include?` (closes #539).
postmodern Nov 21, 2024
d832365
Renamed `UnboundedArrayType` to `FlexibleArrayType` (closes #537).
postmodern Nov 21, 2024
e76279d
Added `Encoding::Java` (closes #521).
postmodern Nov 22, 2024
9f32a46
Added `Network::URL::REGEX` to complement `Network::IP::REGEX`.
postmodern Nov 25, 2024
261ac29
Added `Network::Defang.defang` (closes #547).
postmodern Nov 25, 2024
b189b2a
Added `Network::Defang::Mixin#defang` (closes #548).
postmodern Nov 25, 2024
ab5c5d8
Added `String#defang` (closes #549).
postmodern Nov 25, 2024
7e82783
Added `Software::Version` (closes #510).
postmodern Nov 28, 2024
8b12ee8
Added `Software::VersionConstraint` (close #551).
postmodern Nov 28, 2024
eb53490
Added `Software::VersionRange` (closes #546).
postmodern Nov 28, 2024
132fa0e
Added `ronin/support/software`.
postmodern Nov 28, 2024
37fe141
Link to the docs for `Software::Version` and `Software::VersionRange`.
postmodern Dec 9, 2024
ea6fc9c
Added `Network::Host::REGEX` (closes #550).
postmodern Dec 10, 2024
0556462
Added `Text::Patterns::DECIMAL_BYTE` (issue #553).
postmodern Dec 10, 2024
752dee5
Added `Text::Patterns::HEX_BYTE` (issue #553).
postmodern Dec 10, 2024
b1ac537
Added `Text::Patterns::HEX_WORD` (issue #553).
postmodern Dec 11, 2024
ad0ac27
Added `Text::Patterns::HEX_DWORD` (issue #553).
postmodern Dec 11, 2024
dab60de
Added `Text::Patterns::HEX_QWORD` (issue #553).
postmodern Dec 11, 2024
ea1b20e
Added `Text::Patterns::OCTAL_BYTE` (issue #553).
postmodern Dec 11, 2024
af2bed1
Added `Text::Patterns::FLOATING_POINT_NUMBER` (closes #553).
postmodern Dec 11, 2024
0587e86
Deprecate `Text::Patterns::DECIMAL_OCTET` in favor of `DECIMAL_BYTE`.
postmodern Dec 11, 2024
b2f8c3d
Allow `Text::Patterns::NUMBER` and `FLOAT` to match negative numbers.
postmodern Dec 12, 2024
b4468fd
Allow `Text::Patterns::NUMBER` to match `e` exponents.
postmodern Dec 12, 2024
1ddf3bd
Moved `VERSION_NUMBER` to `ronin/support/text/patterns/software` (clo…
postmodern Dec 12, 2024
8b231ef
Added `Text::Patterns::VERSION_CONSTRAINT` (closes #555).
postmodern Dec 12, 2024
7060c55
Added `Text::Patterns::VERSION_RANGE` (closes #556).
postmodern Dec 12, 2024
5e4afce
Refactor `ronin/support/encoding/base64` to not use the `Base64` library
Feb 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 93 additions & 8 deletions lib/ronin/support/encoding/base64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# along with ronin-support. If not, see <https://www.gnu.org/licenses/>.
#

require 'base64'

module Ronin
module Support
class Encoding < ::Encoding
Expand Down Expand Up @@ -48,9 +46,9 @@ module Base64
#
def self.encode(data, mode: nil)
case mode
when :strict then ::Base64.strict_encode64(data)
when :url_safe then ::Base64.urlsafe_encode64(data)
when nil then ::Base64.encode64(data)
when :strict then strict_encode64(data)
when :url_safe then urlsafe_encode64(data)
when nil then encode64(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline the encode64 logic directly into encode.

else
raise(ArgumentError,"Base64 mode must be either :string, :url_safe, or nil: #{mode.inspect}")
end
Expand All @@ -70,13 +68,100 @@ def self.encode(data, mode: nil)
#
def self.decode(data, mode: nil)
case mode
when :strict then ::Base64.strict_decode64(data)
when :url_safe then ::Base64.urlsafe_decode64(data)
when nil then ::Base64.decode64(data)
when :strict then strict_decode64(data)
when :url_safe then urlsafe_decode64(data)
when nil then decode64(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline the decode64 logic directly into decode.

else
raise(ArgumentError,"Base64 mode must be either :string, :url_safe, or nil: #{mode.inspect}")
end
end

#
# Base64 encodes the given data.
#
# @param [String] data
# The data to Base64 encode.
#
# @return [String]
# The Base64 encoded data.
#
def self.encode64(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove encode64.

[data].pack("m")
end

#
# Base64 decodes the given data.
#
# @param [String] data
# The Base64 data to decode.
#
# @return [String]
# The decoded data.
#
def self.decode64(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove decode64.

data.unpack1("m")
end

#
# Base64 strict encodes the given data.
#
# @param [String] data
# The data to Base64 encode.
#
# @return [String]
# The Base64 strict encoded data.
#
def self.strict_encode64(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the 64 suffix.

[data].pack("m0")
end

#
# Base64 strict decodes the given data.
#
# @param [String] data
# The Base64 data to decode.
#
# @return [String]
# The strict decoded data.
#
def self.strict_decode64(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the 64 suffix.

data.unpack1("m0")
end

#
# Base64 url-safe encodes the given data.
#
# @param [String] data
# The data to Base64 encode.
#
# @return [String]
# The Base64 url-safe encoded data.
#
def self.urlsafe_encode64(data, padding: true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to encode_urlsafe.

str = strict_encode64(data)
str.chomp!("==") or str.chomp!("=") unless padding
str.tr!("+/", "-_")
str
end

#
# Base64 url-safe decodes the given data.
#
# @param [String] data
# The Base64 data to decode.
#
# @return [String]
# The url-safe decoded data.
#
def self.urlsafe_decode64(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to decode_urlsafe.

if !data.end_with?("=") && data.length % 4 != 0
data = data.ljust((str.length + 3) & ~3, "=")
data.tr!("-_", "+/")
else
data = data.tr("-_", "+/")
end
strict_decode64(data)
end
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/encoding/base64/core_ext/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
subject { "hello" }

it "must Base64 encode the String" do
expect(subject.base64_encode).to eq(Base64.encode64(subject))
expect(subject.base64_encode).to eq(Ronin::Support::Encoding::Base64.encode64(subject))
end

context "when given the mode: keyword of :strict" do
subject { 'A' * 256 }

it "must strict encode the String" do
expect(subject.base64_encode(mode: :strict)).to eq(
Base64.strict_encode64(subject)
Ronin::Support::Encoding::Base64.strict_encode64(subject)
)
end
end
Expand All @@ -31,7 +31,7 @@

it "must URL-safe encode the String" do
expect(subject.base64_encode(mode: :url_safe)).to eq(
Base64.strict_encode64(subject)
Ronin::Support::Encoding::Base64.strict_encode64(subject)
)
end
end
Expand All @@ -49,15 +49,15 @@

describe "#base64_decode" do
let(:data) { "hello" }
let(:subject) { Base64.encode64(data) }
let(:subject) { Ronin::Support::Encoding::Base64.encode64(data) }

it "must Base64 decode the given data" do
expect(subject.base64_decode).to eq(data)
end

context "when given the mode: keyword of :strict" do
let(:data) { 'A' * 256 }
let(:subject) { Base64.strict_encode64(data) }
let(:subject) { Ronin::Support::Encoding::Base64.strict_encode64(data) }

it "must strict decode the given data" do
expect(subject.base64_decode(mode: :strict)).to eq(data)
Expand All @@ -66,7 +66,7 @@

context "when given the mode: keyword of :url_safe" do
let(:data) { 'A' * 256 }
let(:subject) { Base64.urlsafe_encode64(data) }
let(:subject) { Ronin::Support::Encoding::Base64.urlsafe_encode64(data) }

it "must URL-safe decode the given data" do
expect(subject.base64_decode(mode: :url_safe)).to eq(data)
Expand Down
66 changes: 60 additions & 6 deletions spec/encoding/base64_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
let(:data) { "hello" }

it "must Base64 encode the given data" do
expect(subject.encode(data)).to eq(Base64.encode64(data))
expect(subject.encode(data)).to eq(described_class.encode64(data))
end

context "when given the mode: keyword of :strict" do
let(:data) { 'A' * 256 }

it "must strict encode the given data" do
expect(subject.encode(data, mode: :strict)).to eq(
Base64.strict_encode64(data)
described_class.strict_encode64(data)
)
end
end
Expand All @@ -24,7 +24,7 @@

it "must URL-safe encode the given data" do
expect(subject.encode(data, mode: :url_safe)).to eq(
Base64.strict_encode64(data)
described_class.strict_encode64(data)
)
end
end
Expand All @@ -42,15 +42,15 @@

describe ".decode" do
let(:data) { "hello" }
let(:encoded_data) { Base64.encode64(data) }
let(:encoded_data) { described_class.encode64(data) }

it "must Base64 decode the given data" do
expect(subject.decode(encoded_data)).to eq(data)
end

context "when given the mode: keyword of :strict" do
let(:data) { 'A' * 256 }
let(:encoded_data) { Base64.strict_encode64(data) }
let(:encoded_data) { described_class.strict_encode64(data) }

it "must strict decode the given data" do
expect(subject.decode(encoded_data, mode: :strict)).to eq(data)
Expand All @@ -59,7 +59,7 @@

context "when given the mode: keyword of :url_safe" do
let(:data) { 'A' * 256 }
let(:encoded_data) { Base64.urlsafe_encode64(data) }
let(:encoded_data) { described_class.urlsafe_encode64(data) }

it "must URL-safe decode the given data" do
expect(subject.decode(encoded_data, mode: :url_safe)).to eq(data)
Expand All @@ -76,4 +76,58 @@
end
end
end

describe "#encode64" do
let(:data) { "AAAA" }
let(:encoded_data) { "QUFBQQ==\n" }

it "must encode the given data" do
expect(subject.encode64(data)).to eq(encoded_data)
end
end

describe "#strict_encode64" do
let(:data) { "AAAA" }
let(:encoded_data) { "QUFBQQ==" }

it "must strict encode the given data" do
expect(subject.strict_encode64(data)).to eq(encoded_data)
end
end

describe "#urlsafe_encode64" do
let(:data) { "AAAA" }
let(:encoded_data) { "QUFBQQ==" }

it "must URL-safe encode the given data" do
expect(subject.urlsafe_encode64(data)).to eq(encoded_data)
end
end

describe "#decode64" do
let(:data) { "QUFBQQ==\n" }
let(:decoded_data) { "AAAA" }

it "must decode the given data" do
expect(subject.decode64(data)).to eq(decoded_data)
end
end

describe "#strict_decode64" do
let(:data) { "QUFBQQ==" }
let(:decoded_data) { "AAAA" }

it "must strict decode the given data" do
expect(subject.strict_decode64(data)).to eq(decoded_data)
end
end

describe "#urlsafe_decode64" do
let(:data) { "QUFBQQ==" }
let(:decoded_data) { "AAAA" }

it "must URL-safe decode the given data" do
expect(subject.urlsafe_decode64(data)).to eq(decoded_data)
end
end
end