Class: Obfusbit
- Inherits:
-
Object
- Object
- Obfusbit
- 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
-
#obfuskey ⇒ Object
readonly
Returns the value of attribute obfuskey.
Instance Method Summary collapse
-
#initialize(schema, obfuskey: nil) ⇒ Obfusbit
constructor
A new instance of Obfusbit.
- #max_bits ⇒ Object
- #pack(values, obfuscate: false) ⇒ Object
- #pack_bytes(values, byteorder: :big) ⇒ Object
- #schema ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #total_bits ⇒ Object
- #unpack(packed_data, obfuscated: false) ⇒ Object
- #unpack_bytes(byte_data, byteorder: :big) ⇒ Object
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
#obfuskey ⇒ Object (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_bits ⇒ Object
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 |
#schema ⇒ Object
32 33 34 |
# File 'lib/obfuskey/obfusbit.rb', line 32 def schema @schema.definition end |
#to_s ⇒ Object 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_bits ⇒ Object
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 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 |