Class: Multibases::Base16

Inherits:
Object
  • Object
show all
Defined in:
lib/multibases/base16.rb

Defined Under Namespace

Classes: Table

Constant Summary collapse

Default =
Base16.new('0123456789abcdef')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alphabet, strict: false, encoding: nil) ⇒ Base16

Returns a new instance of Base16.



54
55
56
# File 'lib/multibases/base16.rb', line 54

def initialize(alphabet, strict: false, encoding: nil)
  @table = Table.from(alphabet, strict: strict, encoding: encoding)
end

Class Method Details

.decode(packed) ⇒ Object



26
27
28
29
30
31
# File 'lib/multibases/base16.rb', line 26

def self.decode(packed)
  packed = packed.pack('C*') if packed.is_a?(Array)

  # packed.scan(/../).map { |x| x.hex.chr }.join
  DecodedByteArray.new(Array(String(packed)).pack('H*').bytes)
end

.encode(plain) ⇒ Object

RFC 4648 implementation



16
17
18
19
20
21
22
23
24
# File 'lib/multibases/base16.rb', line 16

def self.encode(plain)
  plain = plain.pack('C*') if plain.is_a?(Array)

  # plain.each_byte.map do |byte| byte.to_s(16) end.join
  EncodedByteArray.new(
    plain.unpack1('H*').bytes,
    encoding: Encoding::US_ASCII
  )
end

Instance Method Details

#decodable?(encoded) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/multibases/base16.rb', line 101

def decodable?(encoded)
  (encoded.uniq - @table.tr_ords).length.zero?
end

#decode(encoded) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/multibases/base16.rb', line 69

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::Base16.decode(encoded)
end

#default?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/multibases/base16.rb', line 91

def default?
  eql?(Default)
end

#encode(plain) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/multibases/base16.rb', line 58

def encode(plain)
  encoded = Multibases::Base16.encode(plain)
  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: ==

Returns:

  • (Boolean)


95
96
97
# File 'lib/multibases/base16.rb', line 95

def eql?(other)
  other.is_a?(Base16) && other.instance_variable_get(:@table) == @table
end

#inspectObject



8
9
10
11
12
13
# File 'lib/multibases/base16.rb', line 8

def inspect
  '[Multibases::Base16 ' \
    "alphabet=\"#{@table.alphabet}\"" \
    "#{@table.strict? ? ' strict' : ''}" \
  ']'
end

#table_ords(force_strict: nil) ⇒ Object



105
106
107
# File 'lib/multibases/base16.rb', line 105

def table_ords(force_strict: nil)
  @table.tr_ords(force_strict: force_strict)
end