lib/symmetric/encryption.rb in symmetric-encryption-0.1.2 vs lib/symmetric/encryption.rb in symmetric-encryption-0.2.0
- old
+ new
@@ -1,8 +1,9 @@
require 'base64'
require 'openssl'
require 'zlib'
+require 'yaml'
module Symmetric
# Encrypt using 256 Bit AES CBC symmetric key and initialization vector
# The symmetric key is protected using the private key below and must
@@ -14,10 +15,31 @@
unless defined? MAGIC_HEADER
MAGIC_HEADER = '@EnC'
MAGIC_HEADER_SIZE = MAGIC_HEADER.size
end
+ # The minimum length for an encrypted string
+ def self.min_encrypted_length
+ @@min_encrypted_length ||= encrypt('1').length
+ end
+
+ # Returns [true|false] a best effort determination as to whether the supplied
+ # string is encrypted or not, without incurring the penalty of actually
+ # decrypting the supplied data
+ # Parameters:
+ # encrypted_data: Encrypted string
+ def self.encrypted?(encrypted_data)
+ # Simple checks first
+ return false if (encrypted_data.length < min_encrypted_length) || (!encrypted_data.end_with?("\n"))
+ # For now have to decrypt it fully
+ begin
+ decrypt(encrypted_data) ? true : false
+ rescue
+ false
+ end
+ end
+
# Set the Symmetric Cipher to be used
def self.cipher=(cipher)
@@cipher = cipher
end
@@ -53,11 +75,12 @@
# Hard coded symmetric_key?
if symmetric_key
self.key = symmetric_key
self.iv = symmetric_iv
else
- self.load_keys(config['symmetric_key_filename'], config['symmetric_iv_filename'], config['private_rsa_key'])
+ load_keys(config['symmetric_key_filename'], config['symmetric_iv_filename'], config['private_rsa_key'])
end
+ true
end
# Load the symmetric key to use for encrypting and decrypting data
# Call from environment.rb before calling encrypt or decrypt
#
\ No newline at end of file