aboutsummaryrefslogtreecommitdiffstats
path: root/ncomp-utils-java/src/main/java/org/openecomp/ncomp/utils/CryptoUtilsTest.java
blob: fd4671a8a250608f1df71f3fe10492281d2c24b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*-
 * ============LICENSE_START==========================================
 * OPENECOMP - DCAE
 * ===================================================================
 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * 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.
 * ============LICENSE_END============================================
 */
	
package org.openecomp.ncomp.utils;

import static org.openecomp.ncomp.utils.Base64.decode64;
import static org.openecomp.ncomp.utils.CryptoUtils.createKeyPair;
import static org.openecomp.ncomp.utils.CryptoUtils.decrypt;
import static org.openecomp.ncomp.utils.CryptoUtils.decryptPrivate;
import static org.openecomp.ncomp.utils.CryptoUtils.digest;
import static org.openecomp.ncomp.utils.CryptoUtils.digestFile;
import static org.openecomp.ncomp.utils.CryptoUtils.encrypt;
import static org.openecomp.ncomp.utils.CryptoUtils.encryptPublic;
import static org.openecomp.ncomp.utils.CryptoUtils.getInputStream;
import static org.openecomp.ncomp.utils.CryptoUtils.getKey;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;

import junit.framework.TestCase;

import org.openecomp.ncomp.utils.CryptoUtils.EncryptionType;
import org.openecomp.ncomp.webservice.utils.FileUtils;



public class CryptoUtilsTest extends TestCase {
	String k = "dafdfkj";
	String v = "Hello";

    public void test_encrypt() {
    	assertEquals(v, decrypt(k,encrypt(k, v)));
    }
    public void test_streams() throws Exception {
		Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
		MessageDigest digest = MessageDigest.getInstance("SHA");
		digest.update("foobar".getBytes());
		SecretKeySpec key1 = new SecretKeySpec(digest.digest(), 0, 16, "AES");
		aes.init(Cipher.ENCRYPT_MODE, key1);
		InputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream("test/Test.txt");
			CipherInputStream in2 = new CipherInputStream(in, aes);
			out = new FileOutputStream("test/Encrypted.txt");
			FileUtils.copyStream(in2, out);
		} finally {
			if (in != null)
				in.close();
			if (out != null)
				out.close();
		}
		in = null;
		out = null;
		aes.init(Cipher.DECRYPT_MODE, key1);
		try {
			in = new FileInputStream("test/Encrypted.txt");
			CipherInputStream in2 = new CipherInputStream(in, aes);
			out = new FileOutputStream("test/Decrypted.txt");
			FileUtils.copyStream(in2, out);
		} finally {
			if (in != null)
				in.close();
			if (out != null)
				out.close();
		}
		assertEquals(digestFile("test/Test.txt"), digestFile("test/Decrypted.txt"));
	}
    @SuppressWarnings("resource")
	public void test_streams_2() throws Exception {
    	InputStream in = null;
    	InputStream in2 = null;
    	FileOutputStream out = null;
		try {
	    	in = new FileInputStream("test/Test.txt");
	    	in2 = getInputStream(in, EncryptionType.ENCRYPT, k);
	    	out = new FileOutputStream("test/Encrypted.txt");
			FileUtils.copyStream(in2, out);
		} finally {
			if (in != null)
				in.close();
			if (in2 != null)
				in2.close();
			if (out != null)
				out.close();
		}
		in = null;
		in2 = null;
		out = null;
		try {
	    	in = new FileInputStream("test/Encrypted.txt");
	    	in2 = getInputStream(in, EncryptionType.DECRYPT, k);
	    	out = new FileOutputStream("test/Decrypted.txt");
			FileUtils.copyStream(in2, out);
		} finally {
			if (in != null)
				in.close();
			if (in2 != null)
				in2.close();
			if (out != null)
				out.close();
		}
		assertEquals(digestFile("test/Test.txt"), digestFile("test/Decrypted.txt"));
    }
    public void test_public_key() throws Exception {
    	KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    	KeyPair keyPair = keyPairGenerator.generateKeyPair();
		Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		rsa.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
		byte[] ciphertext = rsa.doFinal(v.getBytes());
		rsa.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
		byte[] text = rsa.doFinal(ciphertext);
		assertEquals(v, new String(text));
    }
    
    public void test_public_key_1() throws Exception {
    	createKeyPair("test/key");
    	String publicKey = getKey("test/key.public");
    	System.out.println(digest(decode64(publicKey)));
    	String privateKey = getKey("test/key.private");
    	System.out.println(digest(decode64(privateKey)));
    	KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    	PublicKey k1 = keyFactory.generatePublic(new X509EncodedKeySpec(decode64(publicKey)));
    	PrivateKey k2 = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(decode64(privateKey)));
    	Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    	rsa.init(Cipher.ENCRYPT_MODE, k1);
		byte[] ciphertext = rsa.doFinal(v.getBytes());
		rsa.init(Cipher.DECRYPT_MODE, k2);
		byte[] text = rsa.doFinal(ciphertext);
		assertEquals(v, new String(text));

    }

    public void test_public_key_2() throws Exception {
    	createKeyPair("test/key");
    	String publicKey = getKey("test/key.public");
    	System.out.println(digest(decode64(publicKey)));
    	String privateKey = getKey("test/key.private");
    	System.out.println(digest(decode64(privateKey)));
    	assertEquals(v, decryptPrivate(privateKey,encryptPublic(publicKey, v)));
    }

}