1 package gboat2.base.bridge.util.security;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.security.InvalidKeyException;
6 import java.security.NoSuchAlgorithmException;
7 import java.security.PrivateKey;
8 import java.security.PublicKey;
9 import java.security.Signature;
10 import java.security.SignatureException;
11 import java.security.spec.RSAPrivateKeySpec;
12 import java.security.spec.RSAPublicKeySpec;
13
14 import org.apache.commons.codec.binary.Base64;
15 import org.apache.commons.io.IOUtils;
16 import org.apache.commons.lang3.StringUtils;
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 public class RSASignUtil {
63 public static final String MD2_WITH_RSA = "MD2withRSA";
64 public static final String MD5_WITH_RSA = "MD5withRSA";
65 public static final String SHA1_WITH_RSA = "SHA1withRSA";
66 public static final String SHA256_WITH_RSA = "SHA256withRSA";
67 public static final String SHA384_with_RSA = "SHA384withRSA";
68 public static final String SHA512_WITH_RSA = "SHA512withRSA";
69
70
71 private static final int FILE_CACHE_SIZE = 2048;
72
73
74
75
76
77
78
79 public static byte[] sign(byte[] data, PrivateKey privateKey) {
80 return sign(data, privateKey, MD5_WITH_RSA);
81 }
82
83
84
85
86
87
88
89
90 public static byte[] sign(byte[] data, PrivateKey privateKey, String algorithm){
91 try {
92 Signature signature = Signature.getInstance(algorithm);
93 signature.initSign(privateKey);
94 signature.update(data);
95 return signature.sign();
96 } catch (InvalidKeyException e) {
97 throw new GboatSecurityException("无效的私钥", e);
98 } catch (NoSuchAlgorithmException e) {
99 throw new GboatSecurityException("算法 [" + algorithm + "] 不存在,或当前 JDK 不支持该算法。", e);
100 } catch (SignatureException e) {
101 throw new GboatSecurityException("数据签名失败", e);
102 }
103 }
104
105
106
107
108
109
110
111 public static byte[] sign(File file, PrivateKey privateKey){
112 return sign(file, privateKey, MD5_WITH_RSA);
113 }
114
115
116
117
118
119
120
121
122 public static byte[] sign(File file, PrivateKey privateKey, String algorithm) {
123 FileInputStream in = null;
124 try {
125 Signature signature = Signature.getInstance(algorithm);
126 signature.initSign(privateKey);
127 in = new FileInputStream(file);
128 byte[] cache = new byte[FILE_CACHE_SIZE];
129 int nRead = 0;
130 while ((nRead = in.read(cache)) != -1) {
131 signature.update(cache, 0, nRead);
132 }
133 return signature.sign();
134 } catch (FileNotFoundException e) {
135 throw new GboatSecurityException("要进行签名的文件 [" + file.getAbsolutePath() + "] 不存在。", e);
136 } catch (InvalidKeyException e) {
137 throw new GboatSecurityException("无效的私钥", e);
138 } catch (NoSuchAlgorithmException e) {
139 throw new GboatSecurityException("算法 [" + algorithm + "] 不存在,或当前 JDK 不支持该算法。", e);
140 } catch (Exception e) {
141 throw new GboatSecurityException("数据签名失败", e);
142 } finally {
143 IOUtils.closeQuietly(in);
144 }
145 }
146
147
148
149
150
151
152
153 public static String sign(String data, PrivateKey privateKey){
154 return sign(data, privateKey, MD5_WITH_RSA);
155 }
156
157
158
159
160
161
162
163 public static String sign(String data, PrivateKey privateKey, String algorithm){
164 if(StringUtils.isEmpty(data))
165 return data;
166
167 byte[] result = sign(data.getBytes(), privateKey, algorithm);
168 return Base64.encodeBase64String(result);
169 }
170
171
172
173
174
175
176
177 public static byte[] sign(byte[] data, RSAPrivateKeySpec privateKeySpec) {
178 return sign(data, privateKeySpec, MD5_WITH_RSA);
179 }
180
181
182
183
184
185
186
187
188 public static byte[] sign(byte[] data, RSAPrivateKeySpec privateKeySpec, String algorithm){
189 return sign(data, RSAUtil.getPrivateKey(privateKeySpec), algorithm);
190 }
191
192
193
194
195
196
197
198 public static byte[] sign(File file, RSAPrivateKeySpec privateKeySpec){
199 return sign(file, privateKeySpec, MD5_WITH_RSA);
200 }
201
202
203
204
205
206
207
208
209 public static byte[] sign(File file, RSAPrivateKeySpec privateKeySpec, String algorithm) {
210 return sign(file, RSAUtil.getPrivateKey(privateKeySpec), algorithm);
211 }
212
213
214
215
216
217
218
219 public static String sign(String data, RSAPrivateKeySpec privateKeySpec){
220 return sign(data, privateKeySpec, MD5_WITH_RSA);
221 }
222
223
224
225
226
227
228
229 public static String sign(String data, RSAPrivateKeySpec privateKeySpec, String algorithm){
230 return sign(data, RSAUtil.getPrivateKey(privateKeySpec), algorithm);
231 }
232
233
234
235
236
237
238
239 public static byte[] sign(byte[] data, String privateKey) {
240 return sign(data, privateKey, MD5_WITH_RSA);
241 }
242
243
244
245
246
247
248
249
250 public static byte[] sign(byte[] data, String privateKey, String algorithm){
251 return sign(data, RSAUtil.getPrivateKey(privateKey), algorithm);
252 }
253
254
255
256
257
258
259
260 public static byte[] sign(File file, String privateKey){
261 return sign(file, privateKey, MD5_WITH_RSA);
262 }
263
264
265
266
267
268
269
270
271 public static byte[] sign(File file, String privateKey, String algorithm) {
272 return sign(file, RSAUtil.getPrivateKey(privateKey), algorithm);
273 }
274
275
276
277
278
279
280
281 public static String sign(String data, String privateKey){
282 return sign(data, privateKey, MD5_WITH_RSA);
283 }
284
285
286
287
288
289
290
291 public static String sign(String data, String privateKey, String algorithm){
292 return sign(data, RSAUtil.getPrivateKey(privateKey), algorithm);
293 }
294
295
296
297
298
299
300
301
302
303
304 public static boolean verify(byte[] data, PublicKey publicKey, byte[] signature) {
305 return verify(data, publicKey, signature, MD5_WITH_RSA);
306 }
307
308
309
310
311
312
313
314
315
316 public static boolean verify(byte[] data, PublicKey publicKey, byte[] signature, String algorithm) {
317 try {
318 Signature sign = Signature.getInstance(algorithm);
319 sign.initVerify(publicKey);
320 sign.update(data);
321
322 return sign.verify(signature);
323 } catch (InvalidKeyException e) {
324 throw new GboatSecurityException("无效的公钥", e);
325 } catch (NoSuchAlgorithmException e) {
326 throw new GboatSecurityException("算法 [" + algorithm + "] 不存在,或当前 JDK 不支持该算法。", e);
327 } catch (SignatureException e) {
328 throw new GboatSecurityException("数据签名校验失败", e);
329 }
330 }
331
332
333
334
335
336
337
338
339 public static boolean verify(File file, PublicKey publicKey, byte[] signature){
340 return verify(file, publicKey, signature, MD5_WITH_RSA);
341 }
342
343
344
345
346
347
348
349
350
351 public static boolean verify(File file, PublicKey publicKey, byte[] signature, String algorithm) {
352 FileInputStream in = null;
353 try {
354
355 Signature sign = Signature.getInstance(algorithm);
356 sign.initVerify(publicKey);
357 in = new FileInputStream(file);
358 byte[] cache = new byte[FILE_CACHE_SIZE];
359 int nRead = 0;
360 while ((nRead = in.read(cache)) != -1) {
361 sign.update(cache, 0, nRead);
362 }
363 return sign.verify(signature);
364 } catch (InvalidKeyException e) {
365 throw new GboatSecurityException("无效的公钥", e);
366 } catch (NoSuchAlgorithmException e) {
367 throw new GboatSecurityException("算法 [" + algorithm + "] 不存在,或当前 JDK 不支持该算法。", e);
368 } catch (FileNotFoundException e) {
369 throw new GboatSecurityException("校验的文件 [" + file.getAbsolutePath() + "] 不存在。", e);
370 } catch (Exception e) {
371 throw new GboatSecurityException("文件签名校验失败", e);
372 } finally {
373 IOUtils.closeQuietly(in);
374 }
375 }
376
377
378
379
380
381
382
383
384 public static boolean verify(String base64String, PublicKey publicKey, byte[] signature){
385 return verify(base64String, publicKey, signature, MD5_WITH_RSA);
386 }
387
388
389
390
391
392
393
394
395
396 public static boolean verify(String data, PublicKey publicKey, byte[] signature, String algorithm){
397 return verify(data.getBytes(), publicKey, signature, algorithm);
398 }
399
400
401
402
403
404
405
406
407 public static boolean verify(File file, PublicKey publicKey, String base64Singature){
408 return verify(file, publicKey, base64Singature, MD5_WITH_RSA);
409 }
410
411
412
413
414
415
416
417
418
419 public static boolean verify(File file, PublicKey publicKey, String base64Singature, String algorithm) {
420 return verify(file, publicKey, Base64.decodeBase64(base64Singature), algorithm);
421 }
422
423
424
425
426
427
428
429
430 public static boolean verify(String base64String, PublicKey publicKey, String base64Singature){
431 return verify(base64String, publicKey, base64Singature, MD5_WITH_RSA);
432 }
433
434
435
436
437
438
439
440
441
442 public static boolean verify(String data, PublicKey publicKey, String base64Singature, String algorithm){
443 return verify(data, publicKey, Base64.decodeBase64(base64Singature), algorithm);
444 }
445
446
447
448
449
450
451
452
453 public static boolean verify(byte[] data, RSAPublicKeySpec publicKeySpec, byte[] signature) {
454 return verify(data, publicKeySpec, signature, MD5_WITH_RSA);
455 }
456
457
458
459
460
461
462
463
464
465 public static boolean verify(byte[] data, RSAPublicKeySpec publicKeySpec, byte[] signature, String algorithm) {
466 return verify(data, RSAUtil.getPublicKey(publicKeySpec), signature, algorithm);
467 }
468
469
470
471
472
473
474
475
476 public static boolean verify(File file, RSAPublicKeySpec publicKeySpec, byte[] signature){
477 return verify(file, publicKeySpec, signature, MD5_WITH_RSA);
478 }
479
480
481
482
483
484
485
486
487
488 public static boolean verify(File file, RSAPublicKeySpec publicKeySpec, byte[] signature, String algorithm) {
489 return verify(file, RSAUtil.getPublicKey(publicKeySpec), signature, algorithm);
490 }
491
492
493
494
495
496
497
498
499 public static boolean verify(String base64String, RSAPublicKeySpec publicKeySpec, byte[] signature){
500 return verify(base64String, publicKeySpec, signature, MD5_WITH_RSA);
501 }
502
503
504
505
506
507
508
509
510
511 public static boolean verify(String base64String, RSAPublicKeySpec publicKeySpec, byte[] signature, String algorithm){
512 return verify(base64String, RSAUtil.getPublicKey(publicKeySpec), signature, algorithm);
513 }
514
515
516
517
518
519
520
521
522 public static boolean verify(File file, RSAPublicKeySpec publicKeySpec, String base64Singature){
523 return verify(file, publicKeySpec, base64Singature, MD5_WITH_RSA);
524 }
525
526
527
528
529
530
531
532
533
534 public static boolean verify(File file, RSAPublicKeySpec publicKeySpec, String base64Singature, String algorithm) {
535 return verify(file, RSAUtil.getPublicKey(publicKeySpec), base64Singature, algorithm);
536 }
537
538
539
540
541
542
543
544
545 public static boolean verify(String data, RSAPublicKeySpec publicKeySpec, String base64Singature){
546 return verify(data, publicKeySpec, base64Singature, MD5_WITH_RSA);
547 }
548
549
550
551
552
553
554
555
556
557 public static boolean verify(String data, RSAPublicKeySpec publicKeySpec, String base64Singature, String algorithm){
558 return verify(data, RSAUtil.getPublicKey(publicKeySpec), base64Singature, algorithm);
559 }
560
561
562
563
564
565
566
567
568 public static boolean verify(byte[] data, String publicKey, byte[] signature) {
569 return verify(data, publicKey, signature, MD5_WITH_RSA);
570 }
571
572
573
574
575
576
577
578
579
580 public static boolean verify(byte[] data, String publicKey, byte[] signature, String algorithm) {
581 return verify(data, RSAUtil.getPublicKey(publicKey), signature, algorithm);
582 }
583
584
585
586
587
588
589
590
591 public static boolean verify(File file, String publicKey, byte[] signature){
592 return verify(file, publicKey, signature, MD5_WITH_RSA);
593 }
594
595
596
597
598
599
600
601
602
603 public static boolean verify(File file, String publicKey, byte[] signature, String algorithm) {
604 return verify(file, RSAUtil.getPublicKey(publicKey), signature, algorithm);
605 }
606
607
608
609
610
611
612
613
614 public static boolean verify(String base64String, String publicKey, byte[] signature){
615 return verify(base64String, publicKey, signature, MD5_WITH_RSA);
616 }
617
618
619
620
621
622
623
624
625
626 public static boolean verify(String base64String, String publicKey, byte[] signature, String algorithm){
627 return verify(base64String, RSAUtil.getPublicKey(publicKey), signature, algorithm);
628 }
629
630
631
632
633
634
635
636
637 public static boolean verify(File file, String publicKey, String base64Singature){
638 return verify(file, publicKey, base64Singature, MD5_WITH_RSA);
639 }
640
641
642
643
644
645
646
647
648
649 public static boolean verify(File file, String publicKey, String base64Singature, String algorithm) {
650 return verify(file, RSAUtil.getPublicKey(publicKey), base64Singature, algorithm);
651 }
652
653
654
655
656
657
658
659
660 public static boolean verify(String data, String publicKey, String base64Singature){
661 return verify(data, publicKey, base64Singature, MD5_WITH_RSA);
662 }
663
664
665
666
667
668
669
670
671
672 public static boolean verify(String data, String publicKey, String base64Singature, String algorithm){
673 return verify(data, RSAUtil.getPublicKey(publicKey), base64Singature, algorithm);
674 }
675
676 }