1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.common.crypto;
22
23 import goldengate.common.exception.CryptoException;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.security.NoSuchAlgorithmException;
30 import java.util.Enumeration;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.atomic.AtomicBoolean;
35
36
37
38
39
40
41
42 public abstract class KeyManager {
43 ConcurrentHashMap<String, KeyObject> keysConcurrentHashMap =
44 new ConcurrentHashMap<String, KeyObject>();
45 volatile AtomicBoolean isInitialized = new AtomicBoolean(false);
46
47 public abstract KeyObject createKeyObject();
48
49
50
51
52
53
54 public List<String> initFromList(List<String> keys, String extension) {
55 LinkedList<String> wrong = new LinkedList<String>();
56 for (String filename: keys) {
57 File file = new File(filename);
58 if (file.canRead()) {
59 String basename = file.getName();
60 int lastpos = basename.lastIndexOf(extension);
61 if (lastpos <= 0) {
62 wrong.add(filename);
63 continue;
64 }
65 String firstname = basename.substring(0, lastpos-1);
66 int len = (int)file.length();
67 byte []key = new byte[len];
68 FileInputStream inputStream = null;
69 try {
70 inputStream = new FileInputStream(file);
71 } catch (FileNotFoundException e) {
72
73 wrong.add(filename);
74 continue;
75 }
76 int read = 0;
77 int offset = 0;
78 while (read > 0) {
79 try {
80 read = inputStream.read(key, offset, len);
81 } catch (IOException e) {
82 wrong.add(filename);
83 read = -2;
84 break;
85 }
86 offset += read;
87 if (offset < len) {
88 len -= read;
89 } else {
90 break;
91 }
92 }
93 if (read < -1) {
94
95 continue;
96 }
97 KeyObject keyObject = createKeyObject();
98 keyObject.setSecretKey(key);
99 this.setKey(firstname, keyObject);
100 } else {
101 wrong.add(filename);
102 }
103 }
104 this.isInitialized.set(true);
105 return wrong;
106 }
107
108 public void saveToFiles(String extension) throws CryptoException, IOException {
109 Enumeration<String> names = keysConcurrentHashMap.keys();
110 while (names.hasMoreElements()) {
111 String name = names.nextElement();
112 KeyObject key = keysConcurrentHashMap.get(name);
113 key.saveSecretKey(new File(name+"."+extension));
114 }
115 }
116
117
118
119
120
121
122 public void setKey(String name, KeyObject keyObject) {
123 this.keysConcurrentHashMap.put(name, keyObject);
124 }
125
126
127
128
129
130 public KeyObject getKey(String name) {
131 return this.keysConcurrentHashMap.get(name);
132 }
133
134
135
136
137
138
139
140 public String crypt(String keyName, String toBeCrypted) throws Exception {
141 KeyObject keyObject = this.getKey(keyName);
142 if (keyName == null) {
143 throw new NoSuchAlgorithmException("Key does not exist: "+keyName);
144 }
145 return keyObject.cryptToHex(toBeCrypted);
146 }
147
148
149
150
151
152
153
154 public String decrypt(String keyName, String toBeDecrypted) throws Exception {
155 KeyObject keyObject = this.getKey(keyName);
156 if (keyName == null) {
157 throw new NoSuchAlgorithmException("Key does not exist: "+keyName);
158 }
159 return keyObject.decryptHexInString(toBeDecrypted);
160 }
161
162 }