lib/symmetric_encryption/cipher.rb in symmetric-encryption-4.3.1 vs lib/symmetric_encryption/cipher.rb in symmetric-encryption-4.3.2
- old
+ new
@@ -1,6 +1,6 @@
-require 'openssl'
+require "openssl"
module SymmetricEncryption
# Hold all information related to encryption keys
# as well as encrypt and decrypt data using those keys.
#
# Cipher is thread safe so that the same instance can be called by multiple
@@ -10,11 +10,11 @@
attr_accessor :cipher_name, :version, :iv, :always_add_header
attr_reader :encoding
attr_writer :key
# Returns [Cipher] from a cipher config instance.
- def self.from_config(cipher_name: 'aes-256-cbc',
+ def self.from_config(cipher_name: "aes-256-cbc",
version: 0,
always_add_header: true,
encoding: :base64strict,
**config)
@@ -70,11 +70,11 @@
# Increases the length of the encrypted data by a few bytes, but makes
# migration to a new key trivial
# Default: true
def initialize(key:,
iv: nil,
- cipher_name: 'aes-256-cbc',
+ cipher_name: "aes-256-cbc",
version: 0,
always_add_header: true,
encoding: :base64strict)
@key = key
@@ -82,11 +82,13 @@
@cipher_name = cipher_name
self.encoding = encoding.to_sym
@version = version.to_i
@always_add_header = always_add_header
- raise(ArgumentError, "Cipher version has a valid range of 0 to 255. #{@version} is too high, or negative") if (@version > 255) || @version.negative?
+ if (@version > 255) || @version.negative?
+ raise(ArgumentError, "Cipher version has a valid range of 0 to 255. #{@version} is too high, or negative")
+ end
end
# Change the encoding
def encoding=(encoding)
@encoder = nil
@@ -165,11 +167,13 @@
return decoded if decoded.empty?
decrypted = binary_decrypt(decoded)
# Try to force result to UTF-8 encoding, but if it is not valid, force it back to Binary
- decrypted.force_encoding(SymmetricEncryption::BINARY_ENCODING) unless decrypted.force_encoding(SymmetricEncryption::UTF8_ENCODING).valid_encoding?
+ unless decrypted.force_encoding(SymmetricEncryption::UTF8_ENCODING).valid_encoding?
+ decrypted.force_encoding(SymmetricEncryption::BINARY_ENCODING)
+ end
decrypted
end
# Returns UTF8 encoded string after encoding the supplied Binary string
@@ -178,21 +182,21 @@
# Returns nil if the supplied string is nil
# Note: No encryption or decryption is performed
#
# Returned string is UTF8 encoded except for encoding :none
def encode(binary_string)
- return binary_string if binary_string.nil? || (binary_string == '')
+ return binary_string if binary_string.nil? || (binary_string == "")
encoder.encode(binary_string)
end
# Decode the supplied string using the encoding in this cipher instance
# Note: No encryption or decryption is performed
#
# Returned string is Binary encoded
def decode(encoded_string)
- return encoded_string if encoded_string.nil? || (encoded_string == '')
+ return encoded_string if encoded_string.nil? || (encoded_string == "")
encoder.decode(encoded_string)
end
# Return a new random key using the configured cipher_name
@@ -314,21 +318,21 @@
offset = header.parse(str)
data = offset.positive? ? str[offset..-1] : str
openssl_cipher = ::OpenSSL::Cipher.new(header.cipher_name || cipher_name)
openssl_cipher.decrypt
- openssl_cipher.key = header.key || @key
- if (iv = header.iv || @iv)
+ openssl_cipher.key = header.key || @key
+ if (iv = header.iv || @iv)
openssl_cipher.iv = iv
end
result = openssl_cipher.update(data)
result << openssl_cipher.final
header.compressed? ? Zlib::Inflate.inflate(result) : result
end
# Returns the magic header after applying the encoding in this cipher
def encoded_magic_header
- @encoded_magic_header ||= encoder.encode(SymmetricEncryption::Header::MAGIC_HEADER).delete('=').strip
+ @encoded_magic_header ||= encoder.encode(SymmetricEncryption::Header::MAGIC_HEADER).delete("=").strip
end
# Returns [String] object represented as a string, filtering out the key
def inspect
"#<#{self.class}:0x#{__id__.to_s(16)} @key=\"[FILTERED]\" @iv=#{iv.inspect} @cipher_name=#{cipher_name.inspect}, @version=#{version.inspect}, @encoding=#{encoding.inspect}, @always_add_header=#{always_add_header.inspect}>"