lib/symmetric_encryption/cipher.rb in symmetric-encryption-4.0.0 vs lib/symmetric_encryption/cipher.rb in symmetric-encryption-4.0.1
- old
+ new
@@ -11,14 +11,14 @@
attr_reader :encoding
attr_writer :key
# Returns [Cipher] from a cipher config instance.
def self.from_config(cipher_name: 'aes-256-cbc',
- version: 0,
- always_add_header: true,
- encoding: :base64strict,
- **config)
+ version: 0,
+ always_add_header: true,
+ encoding: :base64strict,
+ **config)
Key.migrate_config!(config)
key = Key.from_config(cipher_name: cipher_name, **config)
Cipher.new(
@@ -82,11 +82,11 @@
@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 < 0)
+ raise(ArgumentError, "Cipher version has a valid range of 0 to 255. #{@version} is too high, or negative") if (@version > 255) || @version.negative?
end
# Change the encoding
def encoding=(encoding)
@encoder = nil
@@ -134,11 +134,11 @@
def encrypt(str, random_iv: false, compress: false, header: always_add_header)
return if str.nil?
str = str.to_s
return str if str.empty?
encrypted = binary_encrypt(str, random_iv: random_iv, compress: compress, header: header)
- self.encode(encrypted)
+ encode(encrypted)
end
# Decode and Decrypt string
# Returns a decrypted string after decoding it first according to the
# encoding setting of this cipher
@@ -155,20 +155,18 @@
#
# Creates a new OpenSSL::Cipher with every call so that this call
# is thread-safe and can be called concurrently by multiple threads with
# the same instance of Cipher
def decrypt(str)
- decoded = self.decode(str)
+ decoded = decode(str)
return unless decoded
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
- unless decrypted.force_encoding(SymmetricEncryption::UTF8_ENCODING).valid_encoding?
- decrypted.force_encoding(SymmetricEncryption::BINARY_ENCODING)
- end
+ decrypted.force_encoding(SymmetricEncryption::BINARY_ENCODING) unless decrypted.force_encoding(SymmetricEncryption::UTF8_ENCODING).valid_encoding?
decrypted
end
# Returns UTF8 encoded string after encoding the supplied Binary string
@@ -247,23 +245,23 @@
return if str.nil?
string = str.to_s
return string if string.empty?
# Header required when adding a random_iv or compressing
- header = Header.new(version: version, compress: compress) if (header == true) || random_iv || compress
+ header = Header.new(version: version, compress: compress) if header || random_iv || compress
# Creates a new OpenSSL::Cipher with every call so that this call is thread-safe.
openssl_cipher = ::OpenSSL::Cipher.new(cipher_name)
openssl_cipher.encrypt
openssl_cipher.key = @key
result =
if header
if random_iv
openssl_cipher.iv = header.iv = openssl_cipher.random_iv
- elsif self.iv
- openssl_cipher.iv = self.iv
+ elsif iv
+ openssl_cipher.iv = iv
end
header.to_s + openssl_cipher.update(compress ? Zlib::Deflate.deflate(string) : string)
else
openssl_cipher.iv = iv if iv
openssl_cipher.update(string)
@@ -305,31 +303,31 @@
str = encrypted_string.to_s
str.force_encoding(SymmetricEncryption::BINARY_ENCODING)
return str if str.empty?
offset = header.parse(str)
- data = offset > 0 ? str[offset..-1] : 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)
+ 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).gsub('=', '').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#{self.__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}>"
+ "#<#{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}>"
end
# DEPRECATED
def self.has_header?(buffer)
SymmetricEncryption::Header.present?(buffer)
@@ -348,8 +346,7 @@
end
private
attr_reader :key
-
end
end