Class: Obfusbit

Inherits:
Object
  • Object
show all
Defined in:
lib/obfuskey/obfusbit.rb

Constant Summary collapse

Error =
Obfuskey::Error
MaximumValueError =
Obfuskey::MaximumValueError
SchemaValidationError =
Obfuskey::SchemaValidationError
BitOverflowError =
Obfuskey::BitOverflowError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, obfuskey: nil) ⇒ Obfusbit

Returns a new instance of Obfusbit.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/obfuskey/obfusbit.rb', line 9

def initialize(schema, obfuskey: nil)
  @schema = schema.is_a?(Obfuskey::ObfusbitSchema) ? schema : Obfuskey::ObfusbitSchema.new(schema)
  @obfuskey = obfuskey
  @total_bits = @schema.total_bits
  @max_bits = @schema.max_bits

  if @obfuskey && @max_bits > @obfuskey.maximum_value
    raise MaximumValueError,
      "The provided schema requires a maximum packed integer value of #{@max_bits} " \
      "(which needs #{@total_bits} bits to represent), but the provided Obfuskey instance " \
      "can only handle up to a maximum value of #{@obfuskey.maximum_value} " \
      "(which covers #{@obfuskey.maximum_value.bit_length} bits)."
  end
end

Instance Attribute Details

#obfuskeyObject (readonly)

Returns the value of attribute obfuskey.



7
8
9
# File 'lib/obfuskey/obfusbit.rb', line 7

def obfuskey
  @obfuskey
end

Instance Method Details

#max_bitsObject



28
29
30
# File 'lib/obfuskey/obfusbit.rb', line 28

def max_bits
  @max_bits
end

#pack(values, obfuscate: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/obfuskey/obfusbit.rb', line 36

def pack(values, obfuscate: false)
  @schema.validate_values!(values)

  normalized = values.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
  packed_int = 0

  @schema.field_info.each do |name, info|
    packed_int |= normalized[name] << info[:shift]
  end

  if obfuscate
    raise ArgumentError, "An Obfuskey instance was not provided during initialization." unless @obfuskey
    return @obfuskey.get_key(packed_int)
  end

  packed_int
end

#pack_bytes(values, byteorder: :big) ⇒ Object



72
73
74
75
# File 'lib/obfuskey/obfusbit.rb', line 72

def pack_bytes(values, byteorder: :big)
  packed_int = pack(values, obfuscate: false)
  int_to_bytes(packed_int, byteorder)
end

#schemaObject



32
33
34
# File 'lib/obfuskey/obfusbit.rb', line 32

def schema
  @schema.definition
end

#to_sObject Also known as: inspect



82
83
84
85
# File 'lib/obfuskey/obfusbit.rb', line 82

def to_s
  key_repr = @obfuskey ? "obfuskey=#{@obfuskey}" : "no obfuskey"
  "Obfusbit(schema=#{@schema}, #{key_repr})"
end

#total_bitsObject



24
25
26
# File 'lib/obfuskey/obfusbit.rb', line 24

def total_bits
  @total_bits
end

#unpack(packed_data, obfuscated: false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/obfuskey/obfusbit.rb', line 54

def unpack(packed_data, obfuscated: false)
  if obfuscated
    raise ArgumentError, "An Obfuskey instance was not provided during initialization." unless @obfuskey
    raise TypeError, "packed_data must be a String when obfuscated is true." unless packed_data.is_a?(String)
    packed_int = @obfuskey.get_value(packed_data)
  else
    raise TypeError, "packed_data must be an Integer when obfuscated is false." unless packed_data.is_a?(Integer)
    packed_int = packed_data
  end

  result = {}
  @schema.field_info.each do |name, info|
    mask = (1 << info[:bits]) - 1
    result[name] = (packed_int >> info[:shift]) & mask
  end
  result
end

#unpack_bytes(byte_data, byteorder: :big) ⇒ Object



77
78
79
80
# File 'lib/obfuskey/obfusbit.rb', line 77

def unpack_bytes(byte_data, byteorder: :big)
  packed_int = bytes_to_int(byte_data, byteorder)
  unpack(packed_int, obfuscated: false)
end