View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.crypto;
21  
22  import javax.crypto.Cipher;
23  import javax.crypto.Mac;
24  
25  /**
26   * This class handles methods to crypt (not decrypt) messages with HmacSha1
27   * algorithm (very efficient:
28   * 136000/s).<br>
29   * <br>
30   * Usage:<br>
31   * <ul>
32   * <li>Create a HmacSha1 object: HmacSha1 key = new HmacSha1();</li>
33   * <li>Create a key:
34   * <ul>
35   * <li>Generate: key.generateKey();<br>
36   * The method key.getSecretKeyInBytes() allow getting the key in Bytes.</li>
37   * <li>From an external source: key.setSecretKey(arrayOfBytes);</li>
38   * </ul>
39   * </li>
40   * <li>To crypt a String in a Base64 format: String myStringCrypt =
41   * key.cryptToString(myString);</li>
42   * </ul>
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  }