test/cipher_test.rb in symmetric-encryption-2.2.0 vs test/cipher_test.rb in symmetric-encryption-3.0.0

- old
+ new

@@ -91,25 +91,45 @@ assert_equal Encoding.find('utf-8'), @cipher.decrypt(@cipher.encrypt(@social_security_number)).encoding end end end - context "magic header" do + context "with header" do + setup do + @social_security_number = "987654321" + end - should "create and parse magic header" do - random_cipher = SymmetricEncryption::Cipher.new(SymmetricEncryption::Cipher.random_key_pair) - header = SymmetricEncryption::Cipher.magic_header(1, compressed=true, random_cipher.send(:iv), random_cipher.send(:key), random_cipher.cipher_name) - compressed, iv, key, cipher_name, version, decryption_cipher = SymmetricEncryption::Cipher.parse_magic_header!(header) - assert_equal true, compressed - assert_equal random_cipher.cipher_name, cipher_name, "Ciphers differ" - assert_equal random_cipher.send(:key), key, "Keys differ" - assert_equal random_cipher.send(:iv), iv, "IVs differ" + should "build and parse header" do + assert random_key_pair = SymmetricEncryption::Cipher.random_key_pair('aes-128-cbc') + assert binary_header = SymmetricEncryption::Cipher.build_header(SymmetricEncryption.cipher.version, compressed=true, random_key_pair[:iv], random_key_pair[:key], random_key_pair[:cipher_name], binary=true) + header = SymmetricEncryption::Cipher.parse_header!(binary_header) + assert_equal true, header.compressed + assert random_cipher = SymmetricEncryption::Cipher.new(random_key_pair) + assert_equal random_cipher.cipher_name, header.cipher_name, "Ciphers differ" + assert_equal random_cipher.send(:key), header.key, "Keys differ" + assert_equal random_cipher.send(:iv), header.iv, "IVs differ" string = "Hello World" - cipher = SymmetricEncryption::Cipher.new(:key => key, :iv => iv, :cipher_name => cipher_name) + cipher = SymmetricEncryption::Cipher.new(:key => header.key, :iv => header.iv, :cipher_name => header.cipher_name) # Test Encryption assert_equal random_cipher.encrypt(string, false, false), cipher.encrypt(string, false, false), "Encrypted values differ" end + + should "encrypt and then decrypt without a header" do + assert encrypted = @cipher.binary_encrypt(@social_security_number,false,false,false) + assert_equal @social_security_number, @cipher.decrypt(encrypted) + end + + should "encrypt and then decrypt using random iv" do + assert encrypted = @cipher.encrypt(@social_security_number, random_iv=true) + assert_equal @social_security_number, @cipher.decrypt(encrypted) + end + + should "encrypt and then decrypt using random iv with compression" do + assert encrypted = @cipher.encrypt(@social_security_number, random_iv=true, compress=true) + assert_equal @social_security_number, @cipher.decrypt(encrypted) + end + end end end \ No newline at end of file