View Javadoc
1   package net.sumaris.core.util.crypto;
2   
3   /*
4    * #%L
5    * UCoin Java :: Core Shared
6    * %%
7    * Copyright (C) 2014 - 2016 EIS
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import java.io.UnsupportedEncodingException;
26  import java.security.MessageDigest;
27  import java.security.NoSuchAlgorithmException;
28  
29  /**
30   * Neither org.apache.commons.codec.digest.DigestUtils nor org.apache.commons.codec.binary.Hex
31   * take the ENCODING into account, they both use the system's default encoding which is wrong
32   * in a web environment.
33   * <p/>
34   * @see: https://github.com/MyMalcom/malcom-lib-android/blob/master/src/main/java/com/malcom/library/android/utils/DigestUtils.java
35   * @author Malcom Ventures, S.L.
36   * @since 2012
37   */
38  public class DigestUtils {
39      private static final char[] HEXITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
40      private static final String SHA1_ALGORITHM = "SHA1";
41      private static final String UTF_8 = "UTF-8";
42  
43      /**
44       * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order.
45       * The returned array will be double the length of the passed array, as it takes two characters to represent any
46       * given byte.
47       *
48       * @param data a byte[] to convert to Hex characters
49       * @return A char[] containing hexidecimal characters
50       * @see: http://stackoverflow.com/questions/332079/in-java-how-do-i-convert-a-byte-array-to-a-string-of-hex-digits-while-keeping-le
51       */
52      public static String encodeHex(byte[] data) {
53          char[] out = new char[data.length << 1]; // == new char[data.length * 2];
54          for (int i = 0, j = 0; i < data.length; i++) {
55              out[j++] = HEXITS[(0xF0 & data[i]) >>> 4]; // HEXITS[(data[i] & 0xFF) / 16];
56              out[j++] = HEXITS[0x0F & data[i]]; // HEXITS[(data[i] & 0xFF) % 16];
57          }
58          return new String(out);
59      }
60  
61      /**
62       * Genera a SHA1 fingerprint from the given message
63       *
64       * @param message a message to encodeinto SHA-1
65       * @return a SHA1 fingerprint
66       */
67      public static String sha1Hex(String message) {
68          return sha1Hex(message, UTF_8);
69      }
70  
71      public static String sha1Hex(String message, String encoding) {
72          try {
73              MessageDigest md = getSHA1Instance();
74              return encodeHex(md.digest(message.getBytes(encoding)));
75          } catch (UnsupportedEncodingException e) {
76              throw new RuntimeException(e);
77          }
78      }
79  
80      public static MessageDigest getSHA1Instance() {
81          try {
82              return MessageDigest.getInstance(SHA1_ALGORITHM);
83          } catch (NoSuchAlgorithmException e) {
84              throw new RuntimeException(e);
85          }
86      }
87  }