lib/symmetric_encryption/header.rb in symmetric-encryption-4.3.1 vs lib/symmetric_encryption/header.rb in symmetric-encryption-4.3.2
- old
+ new
@@ -6,11 +6,11 @@
# * Decode data first before trying to extract its header.
# * Decoding is not required when encoding is set to `:none`.
class Header
# Encrypted data includes this header prior to encoding when
# `always_add_header` is true.
- MAGIC_HEADER = '@EnC'.force_encoding(SymmetricEncryption::BINARY_ENCODING)
+ MAGIC_HEADER = "@EnC".force_encoding(SymmetricEncryption::BINARY_ENCODING)
MAGIC_HEADER_SIZE = MAGIC_HEADER.size
# [true|false] Whether to compress the data before encryption.
# If supplied in the header.
attr_accessor :compress
@@ -35,11 +35,11 @@
attr_reader :auth_tag
# 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 == '')
+ return false if buffer.nil? || (buffer == "")
buffer.force_encoding(SymmetricEncryption::BINARY_ENCODING)
buffer.start_with?(MAGIC_HEADER)
end
@@ -120,11 +120,11 @@
# Returns [Integer] the offset within the buffer of the data after the header has been read.
#
# Returns 0 if no header is present
def parse(buffer, offset = 0)
- return 0 if buffer.nil? || (buffer == '') || (buffer.length <= MAGIC_HEADER_SIZE + 2)
+ return 0 if buffer.nil? || (buffer == "") || (buffer.length <= MAGIC_HEADER_SIZE + 2)
# Symmetric Encryption Header
#
# Consists of:
# 4 Bytes: Magic Header Prefix: @Enc
@@ -151,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
@@ -195,37 +195,37 @@
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
header = "#{MAGIC_HEADER}#{version.chr(SymmetricEncryption::BINARY_ENCODING)}#{flags.chr(SymmetricEncryption::BINARY_ENCODING)}"
if iv
- header << [iv.length].pack('v')
+ header << [iv.length].pack("v")
header << iv
end
if key
encrypted = cipher.binary_encrypt(key, header: false)
- header << [encrypted.length].pack('v')
+ header << [encrypted.length].pack("v")
header << encrypted
end
if cipher_name
- header << [cipher_name.length].pack('v')
+ header << [cipher_name.length].pack("v")
header << cipher_name
end
if auth_tag
- header << [auth_tag.length].pack('v')
+ header << [auth_tag.length].pack("v")
header << auth_tag
end
header
end
@@ -256,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