1 package org.apache.fulcrum.jce.crypto.extended;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40 public final class CryptoUtilJ8 extends CryptoUtil {
41
42
43 private static final Map<TYPES, CryptoUtilJ8> instances = new ConcurrentHashMap<>();
44
45
46 private static final Map<TYPES, CryptoUtilJ8> instancesWithExplicitParams = new ConcurrentHashMap<>();
47
48
49
50
51
52
53
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
67
68
69
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
82
83
84
85
86
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
108
109
110
111 protected CryptoUtilJ8(TYPES type, byte[] salt, int count) {
112 cryptoStreamFactory = CryptoStreamFactoryJ8Template.getInstance(type, salt, count);
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
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
138
139
140
141
142
143
144
145
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 }