View Javadoc
1   package com.jsql.util.bruter;
2   
3   import com.jsql.util.StringUtil;
4   
5   import java.nio.charset.StandardCharsets;
6   import java.security.MessageDigest;
7   import java.security.NoSuchAlgorithmException;
8   import java.util.zip.CRC32;
9   import java.util.zip.Checksum;
10  
11  public class HashUtil {
12      
13      private HashUtil() {
14          // Nothing
15      }
16      
17      public static String toAdler32(String text) {
18  
19          byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
20          Checksum checksum = new java.util.zip.Adler32();
21          checksum.update(bytes,0,bytes.length);
22          
23          return String.valueOf(checksum.getValue());
24      }
25      
26      public static String toCrc16(String text) {
27          return Crc16Helper.generateCRC16(text);
28      }
29      
30      public static String toCrc64(String text) {
31          return Crc64Helper.generateCRC64(text.getBytes(StandardCharsets.UTF_8));
32      }
33      
34      public static String toMySql(String textInput) throws NoSuchAlgorithmException {
35          
36          var md = MessageDigest.getInstance("sha-1");
37          
38          var password = String.valueOf(textInput.toCharArray());
39          
40          byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8);
41          md.update(passwordBytes, 0, passwordBytes.length);
42          
43          byte[] hashSHA1 = md.digest();
44          var stringSHA1 = HashUtil.digestToHexString(hashSHA1);
45          
46          var passwordSHA1 = String.valueOf(StringUtil.hexstr(stringSHA1).toCharArray());
47          byte[] passwordSHA1Bytes = passwordSHA1.getBytes(StandardCharsets.UTF_8);
48          
49          md.update(passwordSHA1Bytes, 0, passwordSHA1Bytes.length);
50          byte[] hashSHA1SH1 = md.digest();
51          
52          return HashUtil.digestToHexString(hashSHA1SH1);
53      }
54  
55      public static String toCrc32(String textInput) {
56          
57          byte[] bytes = textInput.getBytes(StandardCharsets.UTF_8);
58          
59          Checksum checksum = new CRC32();
60          checksum.update(bytes, 0, bytes.length);
61          
62          long lngChecksum = checksum.getValue();
63          
64          return Long.toString(lngChecksum);
65      }
66  
67      public static String toMd4(String textInput) {
68          
69          MessageDigest md = new DigestMD4();
70  
71          var passwordString = String.valueOf(textInput.toCharArray());
72          byte[] passwordByte = passwordString.getBytes(StandardCharsets.UTF_8);
73          
74          md.update(passwordByte, 0, passwordByte.length);
75          byte[] encodedPassword = md.digest();
76          
77          return HashUtil.digestToHexString(encodedPassword);
78      }
79  
80      public static String toHash(String nameMethod, String textInput) throws NoSuchAlgorithmException {
81          
82          var md = MessageDigest.getInstance(nameMethod);
83          
84          var passwordString = String.valueOf(textInput.toCharArray());
85          byte[] passwordByte = passwordString.getBytes(StandardCharsets.UTF_8);
86          
87          md.update(passwordByte, 0, passwordByte.length);
88          byte[] encodedPassword = md.digest();
89          
90          return HashUtil.digestToHexString(encodedPassword);
91      }
92      
93      /**
94       * Convert a digest hash to a string representation.
95       * @param block Digest array
96       * @return Hash as a string
97       */
98      public static String digestToHexString(byte[] block) {
99          
100         var buf = new StringBuilder();
101 
102         for (byte b : block) {
103             HashUtil.byte2hex(b, buf);
104         }
105         
106         return buf.toString();
107     }
108     
109     /**
110      * Convert byte character to hexadecimal StringBuffer character.
111      * @param b Byte character to convert
112      * @param buf Hexadecimal converted character
113      */
114     private static void byte2hex(byte b, StringBuilder buf) {
115         
116         var hexChars = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
117         int high = (b & 0xf0) >> 4;
118         int low = b & 0x0f;
119         buf.append(hexChars[high]);
120         buf.append(hexChars[low]);
121     }
122 }