1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.common.crypto;
21
22 import javax.crypto.Cipher;
23 import javax.crypto.Mac;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class HmacSha1 extends KeyObject {
45 private static final String CANNOT_BE_USED_FOR_HMAC_SHA1 =
46 "Cannot be used for HmacSha1";
47 private static final int KEY_SIZE = 128;
48 private static final String ALGO = "HmacSHA1";
49 private static final String INSTANCE = ALGO;
50 public static final String EXTENSION = "hs1";
51
52 @Override
53 public final String getAlgorithm() {
54 return ALGO;
55 }
56
57 @Override
58 public final String getInstance() {
59 return INSTANCE;
60 }
61
62 @Override
63 public final int getKeySize() {
64 return KEY_SIZE;
65 }
66
67 @Override
68 public final String getFileExtension() {
69 return EXTENSION;
70 }
71
72 @Override
73 public final Cipher toCrypt() {
74 throw new IllegalArgumentException(CANNOT_BE_USED_FOR_HMAC_SHA1);
75 }
76
77 @Override
78 public final byte[] crypt(final byte[] plaintext) throws Exception {
79 final Mac mac = Mac.getInstance(ALGO);
80 mac.init(secretKey);
81 return mac.doFinal(plaintext);
82 }
83
84 @Override
85 public final Cipher toDecrypt() {
86 throw new IllegalArgumentException(CANNOT_BE_USED_FOR_HMAC_SHA1);
87 }
88
89 @Override
90 public final byte[] decrypt(final byte[] ciphertext) throws Exception {
91 throw new IllegalArgumentException(CANNOT_BE_USED_FOR_HMAC_SHA1);
92 }
93
94 }