lib/symmetric_encryption/header.rb in symmetric-encryption-4.1.0.beta1 vs lib/symmetric_encryption/header.rb in symmetric-encryption-4.1.0
- old
+ new
@@ -36,10 +36,11 @@
# Returns whether the supplied buffer starts with a symmetric_encryption header
# Note: The encoding of the supplied buffer is forced to binary if not already binary
def self.present?(buffer)
return false if buffer.nil? || (buffer == '')
+
buffer.force_encoding(SymmetricEncryption::BINARY_ENCODING)
buffer.start_with?(MAGIC_HEADER)
end
# Returns a magic header for this cipher instance that can be placed at
@@ -110,10 +111,11 @@
# buffer
# String to extract the header from
def parse!(buffer)
offset = parse(buffer)
return if offset.zero?
+
buffer.slice!(0..offset - 1)
buffer
end
# Returns [Integer] the offset within the buffer of the data after the header has been read.
@@ -149,20 +151,20 @@
offset += MAGIC_HEADER_SIZE
# Remove header and extract flags
self.version = buffer.getbyte(offset)
- offset += 1
+ offset += 1
unless cipher
raise(
SymmetricEncryption::CipherError,
"Cipher with version:#{version.inspect} not found in any of the configured SymmetricEncryption ciphers"
)
end
- flags = buffer.getbyte(offset)
+ flags = buffer.getbyte(offset)
offset += 1
self.compress = (flags & FLAG_COMPRESSED) != 0
if (flags & FLAG_IV) != 0
@@ -193,11 +195,11 @@
offset
end
# Returns [String] this header as a string
def to_s
- flags = 0
+ flags = 0
flags |= FLAG_COMPRESSED if compressed?
flags |= FLAG_IV if iv
flags |= FLAG_KEY if key
flags |= FLAG_CIPHER_NAME if cipher_name
flags |= FLAG_AUTH_TAG if auth_tag
@@ -254,12 +256,12 @@
def read_string(buffer, offset)
# TODO: Length check
# Exception when
# - offset exceeds length of buffer
# byteslice truncates when too long, but returns nil when start is beyond end of buffer
- len = buffer.byteslice(offset, 2).unpack('v').first
+ len = buffer.byteslice(offset, 2).unpack('v').first
offset += 2
- out = buffer.byteslice(offset, len)
+ out = buffer.byteslice(offset, len)
[out, offset + len]
end
end
end