/*
 * Copyright 2011 Jonathan Anderson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.footlights.core.crypto;

import java.security.InvalidKeyException;

import javax.crypto.Cipher;

import me.footlights.core.crypto.SecretKey.CipherBuilder;
import me.footlights.core.crypto.SecretKey.Operation;

import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
import static org.junit.Assert.*;


/** Tests {@see SecretKey}. */
public class SecretKeyTest
{
	/** Make sure we generate something that looks like a key. */
	@Test public void testGeneration() throws Throwable
	{
		SecretKey k = SecretKey.newGenerator().generate();
		assertTrue(k.keySpec.getEncoded().length > 0);
	}

	/**
	 * Test encryption and decryption using test vectors from
	 * @url http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip.
	 */
	@Test public void testEncryptDecrypt() throws Throwable
	{
		String[][] vectors =
		{
			{
				"AES", "ECB",
				"80000000000000000000000000000000",
				"",
				"00000000000000000000000000000000",
				"0edd33d3c621e546455bd8ba1418bec8",
			},
			{
				"AES", "CBC",
				"8000000000000000000000000000000000000000000000000000000000000000",
				"00000000000000000000000000000000",
				"00000000000000000000000000000000",
				"e35a6dcb19b201a01ebcfa8aa22b5759",
			},
		};

		for (String[] v : vectors)
		{
			int i = 0;

			String algorithm = v[i++];
			String mode = v[i++];
			byte[] secret = Hex.decodeHex(v[i++].toCharArray());
			short ivLength = (short) (4 * v[i++].length());
			byte[] plaintext = Hex.decodeHex(v[i++].toCharArray());
			byte[] ciphertext = Hex.decodeHex(v[i++].toCharArray());

			SecretKey key = SecretKey.newGenerator()
				.setAlgorithm(algorithm)
				.setBytes(secret)
				.generate();

			CipherBuilder builder = 
				key.newCipherBuilder()
					.setMode(mode)
					.setIvLength(ivLength);

			try
			{
				Cipher e = builder.setOperation(Operation.ENCRYPT).build();
				Cipher d = builder.setOperation(Operation.DECRYPT).build();

				assertArrayEquals(ciphertext, e.doFinal(plaintext));
				assertArrayEquals(plaintext, d.doFinal(ciphertext));
			}
			catch (InvalidKeyException e)
			{
				fail("Unable to construct an AES cipher with a " + (8 * secret.length)
					+ "-bit key; is the JCE unlimited strength policy installed?");
			}
		}
	}
}

@@ -630,16 +630,44 @@
 rypto;%0A%0A
+import java.nio.ByteBuffer;%0A
 import j
@@ -2845,11 +2845,1070 @@
 %7D%0A%09%09%7D%0A%09%7D
+%0A%0A%09@Test public void encryptAndDecrypt() throws Throwable%0A%09%7B%0A%09%09byte%5B%5D keyBytes = Hex.decodeHex(%2280000000000000000000000000000000%22.toCharArray());%0A%0A%09%09final String algorithm = %22AES%22;%0A%09%09final String fullAlgorithm = algorithm + %22/CBC/NOPADDING%22;%0A%0A%09%09SecretKey key = SecretKey.newGenerator()%0A%09%09%09.setAlgorithm(algorithm)%0A%09%09%09.setBytes(keyBytes)%0A%09%09%09.generate();%0A%0A%09%09Cipher encryptor = key%0A%09%09%09.newCipherBuilder()%0A%09%09%09.parseAlgorithm(fullAlgorithm)%0A%09%09%09.setOperation(Operation.ENCRYPT)%0A%09%09%09.setIvLength((short) (8 * keyBytes.length))%0A%09%09%09.build();%0A%0A%09%09Cipher decryptor = key%0A%09%09%09.newCipherBuilder()%0A%09%09%09.parseAlgorithm(fullAlgorithm)%0A%09%09%09.setOperation(Operation.DECRYPT)%0A%09%09%09.setIvLength((short) (8 * keyBytes.length))%0A%09%09%09.build();%0A%0A%09%09ByteBuffer plaintext = ByteBuffer.wrap(new byte%5B128%5D);%0A%09%09ByteBuffer ciphertext = ByteBuffer.allocate(plaintext.remaining());%0A%09%09ByteBuffer decrypted = ByteBuffer.allocate(plaintext.remaining());%0A%0A%09%09encryptor.doFinal(plaintext, ciphertext);%0A%09%09decryptor.doFinal(ciphertext, decrypted);%0A%0A%09%09assertArrayEquals(plaintext.array(), decrypted.array());%0A%09%7D
 %0A%7D%0A

Test encryption and decryption.