-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathEncryptionUtilities.java
531 lines (494 loc) · 18.4 KB
/
EncryptionUtilities.java
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package com.cedarsoftware.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
/**
* Utility class providing cryptographic operations including hashing, encryption, and decryption.
* <p>
* This class offers:
* </p>
* <ul>
* <li><b>Hash Functions:</b>
* <ul>
* <li>MD5 (fast implementation)</li>
* <li>SHA-1 (fast implementation)</li>
* <li>SHA-256</li>
* <li>SHA-512</li>
* </ul>
* </li>
* <li><b>Encryption/Decryption:</b>
* <ul>
* <li>AES-128 encryption</li>
* <li>CBC mode with PKCS5 padding</li>
* <li>IV generation from key</li>
* </ul>
* </li>
* <li><b>Optimized File Operations:</b>
* <ul>
* <li>Zero-copy I/O using DirectByteBuffer</li>
* <li>Efficient large file handling</li>
* <li>Custom filesystem support</li>
* </ul>
* </li>
* </ul>
*
* <p><b>Hash Function Usage:</b></p>
* <pre>{@code
* // File hashing
* String md5 = EncryptionUtilities.fastMD5(new File("example.txt"));
* String sha1 = EncryptionUtilities.fastSHA1(new File("example.txt"));
*
* // Byte array hashing
* String hash = EncryptionUtilities.calculateMD5Hash(bytes);
* }</pre>
*
* <p><b>Encryption Usage:</b></p>
* <pre>{@code
* // String encryption/decryption
* String encrypted = EncryptionUtilities.encrypt("password", "sensitive data");
* String decrypted = EncryptionUtilities.decrypt("password", encrypted);
*
* // Byte array encryption/decryption
* String encryptedHex = EncryptionUtilities.encryptBytes("password", originalBytes);
* byte[] decryptedBytes = EncryptionUtilities.decryptBytes("password", encryptedHex);
* }</pre>
*
* <p><b>Security Notes:</b></p>
* <ul>
* <li>MD5 and SHA-1 are provided for legacy compatibility but are cryptographically broken</li>
* <li>Use SHA-256 or SHA-512 for secure hashing</li>
* <li>AES implementation uses CBC mode with PKCS5 padding</li>
* <li>IV is deterministically generated from the key using MD5</li>
* </ul>
*
* <p><b>Performance Features:</b></p>
* <ul>
* <li>Optimized buffer sizes for modern storage systems</li>
* <li>Direct ByteBuffer usage for zero-copy I/O</li>
* <li>Efficient memory management</li>
* <li>Thread-safe implementation</li>
* </ul>
*
* @author John DeRegnaucourt ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
public class EncryptionUtilities {
private EncryptionUtilities() {
}
/**
* Calculates an MD5 hash of a file using optimized I/O operations.
* <p>
* This implementation uses:
* <ul>
* <li>DirectByteBuffer for zero-copy I/O</li>
* <li>FileChannel for optimal file access</li>
* <li>Fallback for non-standard filesystems</li>
* </ul>
*
* @param file the file to hash
* @return hexadecimal string of the MD5 hash, or null if the file cannot be read
*/
public static String fastMD5(File file) {
try (InputStream in = Files.newInputStream(file.toPath())) {
if (in instanceof FileInputStream) {
return calculateFileHash(((FileInputStream) in).getChannel(), getMD5Digest());
}
// Fallback for non-file input streams (rare, but possible with custom filesystem providers)
return calculateStreamHash(in, getMD5Digest());
} catch (NoSuchFileException e) {
return null;
} catch (IOException e) {
return null;
}
}
/**
* Calculates a hash from an InputStream using the specified MessageDigest.
* <p>
* This implementation uses:
* <ul>
* <li>64KB buffer optimized for modern storage systems</li>
* <li>Matches OS and filesystem page sizes</li>
* <li>Aligns with SSD block sizes</li>
* </ul>
*
* @param in InputStream to read from
* @param digest MessageDigest to use for hashing
* @return hexadecimal string of the hash value
* @throws IOException if an I/O error occurs
*/
private static String calculateStreamHash(InputStream in, MessageDigest digest) throws IOException {
// 64KB buffer size - optimal for:
// 1. Modern OS page sizes
// 2. SSD block sizes
// 3. Filesystem block sizes
// 4. Memory usage vs. throughput balance
final int BUFFER_SIZE = 64 * 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while ((read = in.read(buffer)) != -1) {
digest.update(buffer, 0, read);
}
return ByteUtilities.encode(digest.digest());
}
/**
* Calculates a SHA-256 hash of a file using optimized I/O operations.
* <p>
* This implementation uses:
* <ul>
* <li>DirectByteBuffer for zero-copy I/O</li>
* <li>FileChannel for optimal file access</li>
* <li>Fallback for non-standard filesystems</li>
* </ul>
*
* @param file the file to hash
* @return hexadecimal string of the SHA-256 hash, or null if the file cannot be read
*/
public static String fastSHA1(File file) {
try (InputStream in = Files.newInputStream(file.toPath())) {
if (in instanceof FileInputStream) {
return calculateFileHash(((FileInputStream) in).getChannel(), getSHA1Digest());
}
// Fallback for non-file input streams (rare, but possible with custom filesystem providers)
return calculateStreamHash(in, getSHA1Digest());
} catch (NoSuchFileException e) {
return null;
} catch (IOException e) {
return null;
}
}
/**
* Calculates a SHA-256 hash of a file using optimized I/O operations.
* <p>
* This implementation uses:
* <ul>
* <li>DirectByteBuffer for zero-copy I/O</li>
* <li>FileChannel for optimal file access</li>
* <li>Fallback for non-standard filesystems</li>
* </ul>
*
* @param file the file to hash
* @return hexadecimal string of the SHA-256 hash, or null if the file cannot be read
*/
public static String fastSHA256(File file) {
try (InputStream in = Files.newInputStream(file.toPath())) {
if (in instanceof FileInputStream) {
return calculateFileHash(((FileInputStream) in).getChannel(), getSHA256Digest());
}
// Fallback for non-file input streams (rare, but possible with custom filesystem providers)
return calculateStreamHash(in, getSHA1Digest());
} catch (NoSuchFileException e) {
return null;
} catch (IOException e) {
return null;
}
}
/**
* Calculates a SHA-512 hash of a file using optimized I/O operations.
* <p>
* This implementation uses:
* <ul>
* <li>DirectByteBuffer for zero-copy I/O</li>
* <li>FileChannel for optimal file access</li>
* <li>Fallback for non-standard filesystems</li>
* </ul>
*
* @param file the file to hash
* @return hexadecimal string of the SHA-512 hash, or null if the file cannot be read
*/
public static String fastSHA512(File file) {
try (InputStream in = Files.newInputStream(file.toPath())) {
if (in instanceof FileInputStream) {
return calculateFileHash(((FileInputStream) in).getChannel(), getSHA512Digest());
}
// Fallback for non-file input streams (rare, but possible with custom filesystem providers)
return calculateStreamHash(in, getSHA1Digest());
} catch (NoSuchFileException e) {
return null;
} catch (IOException e) {
return null;
}
}
/**
* Calculates a hash of a file using the provided MessageDigest and FileChannel.
* <p>
* This implementation uses:
* <ul>
* <li>64KB buffer size optimized for modern storage systems</li>
* <li>DirectByteBuffer for zero-copy I/O</li>
* <li>Efficient buffer management</li>
* </ul>
*
* @param channel FileChannel to read from
* @param digest MessageDigest to use for hashing
* @return hexadecimal string of the hash value
* @throws IOException if an I/O error occurs
*/
public static String calculateFileHash(FileChannel channel, MessageDigest digest) throws IOException {
// Modern OS/disk optimal transfer size (64KB)
// Matches common SSD page sizes and OS buffer sizes
final int BUFFER_SIZE = 64 * 1024;
// Direct buffer for zero-copy I/O
// Reuse buffer to avoid repeated allocation/deallocation
ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
// Read until EOF
while (channel.read(buffer) != -1) {
buffer.flip(); // Prepare buffer for reading
digest.update(buffer); // Update digest
buffer.clear(); // Prepare buffer for writing
}
return ByteUtilities.encode(digest.digest());
}
/**
* Calculates an MD5 hash of a byte array.
*
* @param bytes the data to hash
* @return hexadecimal string of the MD5 hash, or null if input is null
*/
public static String calculateMD5Hash(byte[] bytes) {
return calculateHash(getMD5Digest(), bytes);
}
/**
* Creates a MessageDigest instance for the specified algorithm.
*
* @param digest the name of the digest algorithm
* @return MessageDigest instance for the specified algorithm
* @throws IllegalArgumentException if the algorithm is not available
*/
public static MessageDigest getDigest(String digest) {
try {
return MessageDigest.getInstance(digest);
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(String.format("The requested MessageDigest (%s) does not exist", digest), e);
}
}
/**
* Creates an MD5 MessageDigest instance.
*
* @return MessageDigest configured for MD5
* @throws IllegalArgumentException if MD5 algorithm is not available
*/
public static MessageDigest getMD5Digest() {
return getDigest("MD5");
}
/**
* Calculates a SHA-1 hash of a byte array.
*
* @param bytes the data to hash
* @return hexadecimal string of the SHA-1 hash, or null if input is null
*/
public static String calculateSHA1Hash(byte[] bytes) {
return calculateHash(getSHA1Digest(), bytes);
}
/**
* Creates a SHA-1 MessageDigest instance.
*
* @return MessageDigest configured for SHA-1
* @throws IllegalArgumentException if SHA-1 algorithm is not available
*/
public static MessageDigest getSHA1Digest() {
return getDigest("SHA-1");
}
/**
* Calculates a SHA-256 hash of a byte array.
*
* @param bytes the data to hash
* @return hexadecimal string of the SHA-256 hash, or null if input is null
*/
public static String calculateSHA256Hash(byte[] bytes) {
return calculateHash(getSHA256Digest(), bytes);
}
/**
* Creates a SHA-256 MessageDigest instance.
*
* @return MessageDigest configured for SHA-256
* @throws IllegalArgumentException if SHA-256 algorithm is not available
*/
public static MessageDigest getSHA256Digest() {
return getDigest("SHA-256");
}
/**
* Calculates a SHA-512 hash of a byte array.
*
* @param bytes the data to hash
* @return hexadecimal string of the SHA-512 hash, or null if input is null
*/
public static String calculateSHA512Hash(byte[] bytes) {
return calculateHash(getSHA512Digest(), bytes);
}
/**
* Creates a SHA-512 MessageDigest instance.
*
* @return MessageDigest configured for SHA-512
* @throws IllegalArgumentException if SHA-512 algorithm is not available
*/
public static MessageDigest getSHA512Digest() {
return getDigest("SHA-512");
}
/**
* Creates a byte array suitable for use as an AES key from a string password.
* <p>
* The key is derived using MD5 and truncated to the specified bit length.
*
* @param key the password to derive the key from
* @param bitsNeeded the required key length in bits (typically 128, 192, or 256)
* @return byte array containing the derived key
*/
public static byte[] createCipherBytes(String key, int bitsNeeded) {
String word = calculateMD5Hash(key.getBytes(StandardCharsets.UTF_8));
return word.substring(0, bitsNeeded / 8).getBytes(StandardCharsets.UTF_8);
}
/**
* Creates an AES cipher in encryption mode.
*
* @param key the encryption key
* @return Cipher configured for AES encryption
* @throws Exception if cipher creation fails
*/
public static Cipher createAesEncryptionCipher(String key) throws Exception {
return createAesCipher(key, Cipher.ENCRYPT_MODE);
}
/**
* Creates an AES cipher in decryption mode.
*
* @param key the decryption key
* @return Cipher configured for AES decryption
* @throws Exception if cipher creation fails
*/
public static Cipher createAesDecryptionCipher(String key) throws Exception {
return createAesCipher(key, Cipher.DECRYPT_MODE);
}
/**
* Creates an AES cipher with the specified mode.
* <p>
* Uses CBC mode with PKCS5 padding and IV derived from the key.
*
* @param key the encryption/decryption key
* @param mode Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
* @return configured Cipher instance
* @throws Exception if cipher creation fails
*/
public static Cipher createAesCipher(String key, int mode) throws Exception {
Key sKey = new SecretKeySpec(createCipherBytes(key, 128), "AES");
return createAesCipher(sKey, mode);
}
/**
* Creates an AES cipher with the specified key and mode.
* <p>
* Uses CBC mode with PKCS5 padding and IV derived from the key.
*
* @param key SecretKeySpec for encryption/decryption
* @param mode Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
* @return configured Cipher instance
* @throws Exception if cipher creation fails
*/
public static Cipher createAesCipher(Key key, int mode) throws Exception {
// Use password key as seed for IV (must be 16 bytes)
MessageDigest d = getMD5Digest();
d.update(key.getEncoded());
byte[] iv = d.digest();
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // CBC faster than CFB8/NoPadding (but file length changes)
cipher.init(mode, key, paramSpec);
return cipher;
}
/**
* Encrypts a string using AES-128.
*
* @param key encryption key
* @param content string to encrypt
* @return hexadecimal string of encrypted data
* @throws IllegalStateException if encryption fails
*/
public static String encrypt(String key, String content) {
try {
return ByteUtilities.encode(createAesEncryptionCipher(key).doFinal(content.getBytes(StandardCharsets.UTF_8)));
} catch (Exception e) {
throw new IllegalStateException("Error occurred encrypting data", e);
}
}
/**
* Encrypts a byte array using AES-128.
*
* @param key encryption key
* @param content bytes to encrypt
* @return hexadecimal string of encrypted data
* @throws IllegalStateException if encryption fails
*/
public static String encryptBytes(String key, byte[] content) {
try {
return ByteUtilities.encode(createAesEncryptionCipher(key).doFinal(content));
} catch (Exception e) {
throw new IllegalStateException("Error occurred encrypting data", e);
}
}
/**
* Decrypts a hexadecimal string of encrypted data to its original string form.
*
* @param key decryption key
* @param hexStr hexadecimal string of encrypted data
* @return decrypted string
* @throws IllegalStateException if decryption fails
*/
public static String decrypt(String key, String hexStr) {
try {
return new String(createAesDecryptionCipher(key).doFinal(ByteUtilities.decode(hexStr)));
} catch (Exception e) {
throw new IllegalStateException("Error occurred decrypting data", e);
}
}
/**
* Decrypts a hexadecimal string of encrypted data to its original byte array form.
*
* @param key decryption key
* @param hexStr hexadecimal string of encrypted data
* @return decrypted byte array
* @throws IllegalStateException if decryption fails
*/
public static byte[] decryptBytes(String key, String hexStr) {
try {
return createAesDecryptionCipher(key).doFinal(ByteUtilities.decode(hexStr));
} catch (Exception e) {
throw new IllegalStateException("Error occurred decrypting data", e);
}
}
/**
* Calculates a hash of a byte array using the specified MessageDigest.
*
* @param d MessageDigest to use
* @param bytes data to hash
* @return hexadecimal string of the hash value, or null if input is null
*/
public static String calculateHash(MessageDigest d, byte[] bytes) {
if (bytes == null) {
return null;
}
d.update(bytes);
return ByteUtilities.encode(d.digest());
}
}