Update AAF Version 1.0.0
[aaf/cadi.git] / core / src / test / java / com / att / cadi / JU_AES.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi;\r
24 \r
25 import java.io.ByteArrayInputStream;\r
26 import java.io.ByteArrayOutputStream;\r
27 \r
28 import javax.crypto.CipherInputStream;\r
29 import javax.crypto.CipherOutputStream;\r
30 \r
31 import org.junit.Test;\r
32 \r
33 import junit.framework.Assert;\r
34 \r
35 public class JU_AES {\r
36 \r
37         @Test\r
38         public void test() throws Exception {\r
39                 AES aes = new AES();\r
40                 String orig = "I'm a password, really";\r
41                 byte[] passin = orig.getBytes();\r
42                 byte[] encrypted = aes.encrypt(passin);\r
43                 byte[] b64enc = Symm.base64.encode(encrypted);\r
44                 System.out.println(new String(b64enc));\r
45                 \r
46                 encrypted = Symm.base64.decode(b64enc);\r
47                 passin = aes.decrypt(encrypted);\r
48                 Assert.assertEquals(orig, new String(passin));\r
49         }\r
50 \r
51         @Test\r
52         public void testInputStream() throws Exception {\r
53                 AES aes = new AES();\r
54                 String orig = "I'm a password, really";\r
55                 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
56                 CipherInputStream cis = aes.inputStream(bais, true);\r
57                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
58                 Symm.base64.encode(cis, baos);\r
59                 cis.close();\r
60 \r
61                 byte[] b64encrypted;\r
62                 System.out.println(new String(b64encrypted=baos.toByteArray()));\r
63                 \r
64                 \r
65                 baos.reset();\r
66                 CipherOutputStream cos = aes.outputStream(baos, false);\r
67                 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
68                 cos.close();\r
69                 Assert.assertEquals(orig, new String(baos.toByteArray()));\r
70         }\r
71 \r
72         @Test\r
73         public void testObtain() throws Exception {\r
74                 byte[] keygen = Symm.baseCrypt().keygen();\r
75                 \r
76                 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
77                 \r
78                 String orig ="Another Password, please";\r
79                 String encrypted = symm.enpass(orig);\r
80                 System.out.println(encrypted);\r
81                 String decrypted = symm.depass(encrypted);\r
82                 System.out.println(decrypted);\r
83                 Assert.assertEquals(orig, decrypted);\r
84         }\r
85 \r
86 }\r