Class: Multibases::Base64
- Inherits:
-
Object
- Object
- Multibases::Base64
- Defined in:
- lib/multibases/base64.rb
Defined Under Namespace
Classes: Table
Constant Summary collapse
- Default =
rubocop:disable Metrics/LineLength
Base64.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=')
- UrlSafe =
Base64.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=')
Class Method Summary collapse
- .decode(packed) ⇒ Object
-
.encode(plain) ⇒ Object
RFC 4648 implementation.
Instance Method Summary collapse
- #decodable?(encoded) ⇒ Boolean
- #decode(encoded) ⇒ Object
- #default? ⇒ Boolean
- #encode(plain) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(alphabet, strict: false, encoding: nil) ⇒ Base64
constructor
A new instance of Base64.
- #inspect ⇒ Object
- #table_ords(force_strict: nil) ⇒ Object
- #table_padder ⇒ Object
Constructor Details
Class Method Details
.decode(packed) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/multibases/base64.rb', line 27 def self.decode(packed) packed = packed.pack('C*') if packed.is_a?(Array) # Base64.strict_decode64("m").first # Don't use m0, as that requires padderding _always_ DecodedByteArray.new(packed.unpack1('m').bytes) end |
.encode(plain) ⇒ Object
RFC 4648 implementation
17 18 19 20 21 22 23 24 25 |
# File 'lib/multibases/base64.rb', line 17 def self.encode(plain) plain = plain.pack('C*') if plain.is_a?(Array) # Base64.strict_encode(plain) EncodedByteArray.new( Array(String(plain)).pack('m0').bytes, encoding: Encoding::US_ASCII ) end |
Instance Method Details
#decodable?(encoded) ⇒ Boolean
108 109 110 |
# File 'lib/multibases/base64.rb', line 108 def decodable?(encoded) (encoded.uniq - table_ords).length.zero? end |
#decode(encoded) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/multibases/base64.rb', line 76 def decode(encoded) return DecodedByteArray::EMPTY if encoded.empty? unless encoded.is_a?(Array) encoded = encoded.force_encoding(@table.encoding).bytes end unless decodable?(encoded) raise ArgumentError, "'#{encoded}' contains unknown characters'" end unless default? encoded = ByteArray.new(encoded).transcode( table_ords, Default.table_ords(force_strict: @table.strict?), encoding: Encoding::US_ASCII ) end Multibases::Base64.decode(encoded) end |
#default? ⇒ Boolean
98 99 100 |
# File 'lib/multibases/base64.rb', line 98 def default? eql?(Default) end |
#encode(plain) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/multibases/base64.rb', line 62 def encode(plain) return EncodedByteArray::EMPTY if plain.empty? encoded = Multibases::Base64.encode(plain) encoded.chomp!(Default.table_padder) unless @table.padder return encoded if default? encoded.transcode( Default.table_ords(force_strict: @table.strict?), table_ords, encoding: @table.encoding ) end |
#eql?(other) ⇒ Boolean Also known as: ==
102 103 104 |
# File 'lib/multibases/base64.rb', line 102 def eql?(other) other.is_a?(Base64) && other.instance_variable_get(:@table) == @table end |
#inspect ⇒ Object
9 10 11 12 13 14 |
# File 'lib/multibases/base64.rb', line 9 def inspect '[Multibases::Base64 ' \ "alphabet=\"#{@table.alphabet}\"" \ "#{@table.padder.nil? ? '' : ' pad="' + @table.padder.chr + '"'}" \ ']' end |
#table_ords(force_strict: nil) ⇒ Object
112 113 114 |
# File 'lib/multibases/base64.rb', line 112 def table_ords(force_strict: nil) @table.tr_ords(force_strict: force_strict) end |
#table_padder ⇒ Object
116 117 118 |
# File 'lib/multibases/base64.rb', line 116 def table_padder @table.padder end |