View Javadoc
1   package org.apache.fulcrum.jce.crypto.extended;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.security.GeneralSecurityException;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import org.apache.fulcrum.jce.crypto.CryptoStreamFactory;
30  import org.apache.fulcrum.jce.crypto.CryptoUtil;
31  import org.apache.fulcrum.jce.crypto.StreamUtil;
32  import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
33  
34  /**
35   * Helper class to provide typed functions to work with CryptoStreams.
36   *
37   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
38   * @author <a href="mailto:gk@apache.org">Georg Kallidis</a>
39   */
40  public final class CryptoUtilJ8 extends CryptoUtil {
41  
42  	/** the typed default instances */   
43      private static final Map<TYPES, CryptoUtilJ8> instances = new ConcurrentHashMap<>();
44      
45      /** the default instances with custom settings **/
46      private static final Map<TYPES, CryptoUtilJ8> instancesWithExplicitParams = new ConcurrentHashMap<>();    
47  	
48  	/**
49  	 * Factory method to get a default instance
50  	 * 
51  	 * default type PDC
52  	 * 
53  	 * @return an instance of the CryptoStreamFactory
54  	 */
55  	public static CryptoUtilJ8 getInstance() {
56  		synchronized (CryptoUtilJ8.class) {
57  			TYPES defaultType = CryptoParametersJ8.DEFAULT_TYPE;
58  			if (instances.isEmpty() || !instances.containsKey(defaultType)) {
59  				instances.put(defaultType, new CryptoUtilJ8());
60  			}
61  			return instances.get(defaultType);
62  		}
63  	}
64  	
65  	/**
66  	 * Factory method to get a default instance
67  	 * 
68  	 * @param type one of the enum {@link TYPES}.
69  	 * @return an instance of the CryptoStreamFactory
70  	 */
71  	public static CryptoUtilJ8 getInstance(TYPES type) {
72  		synchronized (CryptoUtilJ8.class) {
73  			if (!instances.containsKey(type)) {
74  				instances.put(type, new CryptoUtilJ8(type));
75  			}
76  			return instances.get(type);
77  		}
78  	}
79  	
80  	/**
81  	 * Factory method to get a default instance
82  	 * 
83  	 * @param type one of the enum {@link TYPES}.
84  	 * @param salt the salt
85  	 * @param count the iteration count
86  	 * @return an instance of the CryptoStreamFactory
87  	 */
88  	public static CryptoUtilJ8 getInstance(TYPES type, byte[] salt, int count) {
89  		synchronized (CryptoUtilJ8.class) {
90  			if (!instancesWithExplicitParams.containsKey(type)) {
91  				instancesWithExplicitParams.put(type, new CryptoUtilJ8(type, salt, count));
92  			}
93  			return instancesWithExplicitParams.get(type);
94  		}
95  	}
96  	
97  	private CryptoUtilJ8() {
98  		cryptoStreamFactory = CryptoStreamFactoryJ8Template.getInstance();
99  	}
100 	
101 	private CryptoUtilJ8(TYPES type) {
102 		cryptoStreamFactory = CryptoStreamFactoryJ8Template.getInstance(type);
103 	}
104 	
105     /**
106      * 
107      * @param type one of the enum {@link TYPES}.
108      * @param salt v
109      * @param count the iteration count
110      */
111     protected CryptoUtilJ8(TYPES type, byte[] salt, int count) {
112     	cryptoStreamFactory = CryptoStreamFactoryJ8Template.getInstance(type, salt, count);
113     }
114 
115 	/**
116 	 * Copies from a source to a target object using encryption and a caller
117 	 * supplied CryptoStreamFactory.
118 	 * 
119 	 * {@link CryptoStreamFactoryJ8Template#getOutputStream(InputStream, OutputStream, char[])} 
120 	 *
121 	 * @param factory  the factory to create the crypto streams
122 	 * @param source   the source object
123 	 * @param target   the target object
124 	 * @param password the password to use for encryption
125 	 * @throws GeneralSecurityException accessing JCE failed
126 	 * @throws IOException              accessing the source failed
127 	 */
128 	@Override
129 	public void encrypt(CryptoStreamFactory factory, Object source, Object target, char[] password)
130 			throws GeneralSecurityException, IOException {
131 		InputStream is = StreamUtil.createInputStream(source);
132 		OutputStream os = StreamUtil.createOutputStream(target);
133 		((CryptoStreamFactoryJ8) factory).getOutputStream(is, os, password);
134 	}
135 
136 	/**
137 	 * Copies from a source to a target object using decryption and a caller-suppier
138 	 * CryptoStreamFactory.
139 	 *
140 	 * @param factory  the factory to create the crypto streams
141 	 * @param source   the source object
142 	 * @param target   the target object
143 	 * @param password the password to use for decryption
144 	 * @throws GeneralSecurityException accessing JCE failed
145 	 * @throws IOException              accessing the source failed
146 	 */
147 	@Override
148 	protected void decrypt(CryptoStreamFactory factory, Object source, Object target, char[] password)
149 			throws GeneralSecurityException, IOException {
150 		InputStream is = StreamUtil.createInputStream(source);
151 		OutputStream os = StreamUtil.createOutputStream(target);
152 		InputStream dis = factory.getInputStream(is, password);
153 		StreamUtil.copy(dis, os);
154 	}
155 
156 	public static Map<TYPES, CryptoUtilJ8> getInstances() {
157 		return instances;
158 	}
159 
160 	public static Map<TYPES, CryptoUtilJ8> getInstancesWithExplicitParams() {
161 		return instancesWithExplicitParams;
162 	}
163 
164 }