-
-
Notifications
You must be signed in to change notification settings - Fork 11
Refactor ronin/support/encoding/base64 to not use the Base64 library
#563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d5758c3
94e175e
941fdec
317c88f
ba54ae8
a3495fc
b54493a
a5afff4
c85992b
cffc8ce
94beeb7
2800246
bd2ea94
08b0d03
01fdfd4
c5510c5
35fe6f5
ab40733
9803c06
b95da92
d832365
e76279d
9f32a46
261ac29
b189b2a
ab5c5d8
7e82783
8b12ee8
eb53490
132fa0e
37fe141
ea6fc9c
0556462
752dee5
b1ac537
ad0ac27
dab60de
ea1b20e
af2bed1
0587e86
b2f8c3d
b4468fd
1ddf3bd
8b231ef
7060c55
5e4afce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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) | ||
| else | ||
| raise(ArgumentError,"Base64 mode must be either :string, :url_safe, or nil: #{mode.inspect}") | ||
| end | ||
|
|
@@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inline the |
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
| [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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the |
||
| [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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the |
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
| 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline the
encode64logic directly intoencode.