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