Remove old license headers
[lgpl/argeo-commons.git] / org.argeo.cms / ext / test / org / argeo / cms / security / PasswordBasedEncryptionTest.java
index 49319f15a15517c92c989d3c2ac122fbcfe1b7b4..62f97936d02726fc11146bc1786bceadcdd6a0a9 100644 (file)
@@ -1,23 +1,9 @@
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * 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 org.argeo.cms.security;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
+import java.util.Base64;
 
 import javax.crypto.Cipher;
 import javax.crypto.CipherInputStream;
@@ -28,31 +14,26 @@ import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.PBEKeySpec;
 import javax.crypto.spec.PBEParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
-import javax.xml.bind.DatatypeConverter;
 
 import junit.framework.TestCase;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.security.PasswordBasedEncryption;
+import org.argeo.util.PasswordEncryption;
 
 public class PasswordBasedEncryptionTest extends TestCase {
-       private final static Log log = LogFactory
-                       .getLog(PasswordBasedEncryptionTest.class);
+       private final static Log log = LogFactory.getLog(PasswordBasedEncryptionTest.class);
 
        public void testEncryptDecrypt() {
                final String password = "test long password since they are safer";
-               PasswordBasedEncryption pbeEnc = new PasswordBasedEncryption(
-                               password.toCharArray());
+               PasswordEncryption pbeEnc = new PasswordEncryption(password.toCharArray());
                String message = "Hello World!";
                log.info("Password:\t'" + password + "'");
                log.info("Message:\t'" + message + "'");
                byte[] encrypted = pbeEnc.encryptString(message);
-               log.info("Encrypted:\t'"
-                               + DatatypeConverter.printBase64Binary(encrypted) + "'");
-               PasswordBasedEncryption pbeDec = new PasswordBasedEncryption(
-                               password.toCharArray());
+               log.info("Encrypted:\t'" + Base64.getEncoder().encode(encrypted) + "'");
+               PasswordEncryption pbeDec = new PasswordEncryption(password.toCharArray());
                InputStream in = null;
                in = new ByteArrayInputStream(encrypted);
                String decrypted = pbeDec.decryptAsString(in);
@@ -65,15 +46,14 @@ public class PasswordBasedEncryptionTest extends TestCase {
                String password = "test";
                String message = "Hello World!";
 
-               byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
-                               (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
+               byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
+                               (byte) 0x99 };
 
                int count = 1024;
 
                String cipherAlgorithm = "PBEWithMD5AndDES";
                String secretKeyAlgorithm = "PBEWithMD5AndDES";
-               SecretKeyFactory keyFac = SecretKeyFactory
-                               .getInstance(secretKeyAlgorithm);
+               SecretKeyFactory keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm);
                PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
                PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
                SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
@@ -92,12 +72,11 @@ public class PasswordBasedEncryptionTest extends TestCase {
                String password = "test";
                String message = "Hello World!";
 
-               byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
-                               (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
-               byte[] iv = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
-                               (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99,
-                               (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
-                               (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
+               byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
+                               (byte) 0x99 };
+               byte[] iv = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
+                               (byte) 0x99, (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
+                               (byte) 0x99 };
 
                int count = 1024;
                // int keyLength = 256;
@@ -105,10 +84,8 @@ public class PasswordBasedEncryptionTest extends TestCase {
 
                String cipherAlgorithm = "AES/CBC/PKCS5Padding";
                String secretKeyAlgorithm = "PBKDF2WithHmacSHA1";
-               SecretKeyFactory keyFac = SecretKeyFactory
-                               .getInstance(secretKeyAlgorithm);
-               PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt,
-                               count, keyLength);
+               SecretKeyFactory keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm);
+               PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count, keyLength);
                SecretKey tmp = keyFac.generateSecret(pbeKeySpec);
                SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
                Cipher ecipher = Cipher.getInstance(cipherAlgorithm);
@@ -116,8 +93,7 @@ public class PasswordBasedEncryptionTest extends TestCase {
 
                // decrypt
                keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm);
-               pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count,
-                               keyLength);
+               pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count, keyLength);
                tmp = keyFac.generateSecret(pbeKeySpec);
                secret = new SecretKeySpec(tmp.getEncoded(), "AES");
                // AlgorithmParameters params = ecipher.getParameters();