1 package com.jsql.util.bruter; 2 3 import com.jsql.util.StringUtil; 4 import org.apache.commons.codec.DecoderException; 5 import org.apache.commons.lang3.StringUtils; 6 import org.apache.commons.text.StringEscapeUtils; 7 8 import java.io.IOException; 9 import java.security.NoSuchAlgorithmException; 10 import java.util.Arrays; 11 import java.util.Optional; 12 13 public enum ActionCoder { 14 15 MD2("Md2") { 16 @Override 17 public String run(String value) throws NoSuchAlgorithmException { 18 return HashUtil.toHash(this.name, value); 19 } 20 }, 21 22 MD4("Md4") { 23 @Override 24 public String run(String value) { 25 return HashUtil.toMd4(value); 26 } 27 }, 28 29 MD5("Md5") { 30 @Override 31 public String run(String value) throws NoSuchAlgorithmException { 32 return HashUtil.toHash(this.name, value); 33 } 34 }, 35 36 SHA_1("Sha-1") { 37 @Override 38 public String run(String value) throws NoSuchAlgorithmException { 39 return HashUtil.toHash(this.name, value); 40 } 41 }, 42 43 SHA_256("Sha-256") { 44 @Override 45 public String run(String value) throws NoSuchAlgorithmException { 46 return HashUtil.toHash(this.name, value); 47 } 48 }, 49 50 SHA_384("Sha-384") { 51 @Override 52 public String run(String value) throws NoSuchAlgorithmException { 53 return HashUtil.toHash(this.name, value); 54 } 55 }, 56 57 SHA_512("Sha-512") { 58 @Override 59 public String run(String value) throws NoSuchAlgorithmException { 60 return HashUtil.toHash(this.name, value); 61 } 62 }, 63 64 MYSQL("Mysql") { 65 @Override 66 public String run(String value) throws NoSuchAlgorithmException { 67 return HashUtil.toMySql(value); 68 } 69 }, 70 71 ADLER32("Adler32") { 72 @Override 73 public String run(String value) { 74 return HashUtil.toAdler32(value); 75 } 76 }, 77 78 CRC16("Crc16") { 79 @Override 80 public String run(String value) { 81 return HashUtil.toCrc16(value); 82 } 83 }, 84 85 CRC32("Crc32") { 86 @Override 87 public String run(String value) { 88 return HashUtil.toCrc32(value); 89 } 90 }, 91 92 CRC64("Crc64") { 93 @Override 94 public String run(String value) { 95 return HashUtil.toCrc64(value); 96 } 97 }, 98 99 ENCODE_TO_HEX("Encode to Hex") { 100 @Override 101 public String run(String value) { 102 return StringUtil.toHex(value); 103 } 104 }, 105 106 ENCODE_TO_HEX_ZIPPED("Encode to Hex(zipped)") { 107 @Override 108 public String run(String value) throws IOException { 109 return StringUtil.toHexZip(value); 110 } 111 }, 112 113 ENCODE_TO_BASE64_ZIPPED("Encode to Base64(zipped)") { 114 @Override 115 public String run(String value) throws IOException { 116 return StringUtil.toBase64Zip(value); 117 } 118 }, 119 120 ENCODE_TO_BASE64("Encode to Base64") { 121 @Override 122 public String run(String value) { 123 return StringUtil.base64Encode(value); 124 } 125 }, 126 127 ENCODE_TO_BASE32("Encode to Base32") { 128 @Override 129 public String run(String value) { 130 return StringUtil.base32Encode(value); 131 } 132 }, 133 134 ENCODE_TO_BASE58("Encode to Base58") { 135 @Override 136 public String run(String value) { 137 return StringUtil.base58Encode(value); 138 } 139 }, 140 141 ENCODE_TO_BASE16("Encode to Base16") { 142 @Override 143 public String run(String value) { 144 return StringUtil.base16Encode(value); 145 } 146 }, 147 148 ENCODE_TO_HTML("Encode to Html") { 149 @Override 150 public String run(String value) { 151 return StringUtil.toHtml(value); 152 } 153 }, 154 155 ENCODE_TO_HTML_DECIMAL("Encode to Html (decimal)") { 156 @Override 157 public String run(String value) { 158 return StringUtil.decimalHtmlEncode(value, true); 159 } 160 }, 161 162 ENCODE_TO_URL("Encode to Url") { 163 @Override 164 public String run(String value) { 165 return StringUtil.toUrl(value); 166 } 167 }, 168 169 ENCODE_TO_UNICODE("Encode to Unicode") { 170 @Override 171 public String run(String value) { 172 return StringEscapeUtils.escapeJava(value); 173 } 174 }, 175 176 DECODE_FROM_HEX("Decode from Hex") { 177 @Override 178 public String run(String value) throws DecoderException { 179 return StringUtil.fromHex(value); 180 } 181 }, 182 183 DECODE_FROM_HEX_ZIPPED("Decode from Hex(zipped)") { 184 @Override 185 public String run(String value) throws IOException, DecoderException { 186 return StringUtil.fromHexZip(value); 187 } 188 }, 189 190 DECODE_FROM_BASE64_ZIPPED("Decode from Base64(zipped)") { 191 @Override 192 public String run(String value) throws IOException { 193 return StringUtil.fromBase64Zip(value); 194 } 195 }, 196 197 DECODE_FROM_BASE64("Decode from Base64") { 198 @Override 199 public String run(String value) { 200 return StringUtil.base64Decode(value); 201 } 202 }, 203 204 DECODE_FROM_BASE32("Decode from Base32") { 205 @Override 206 public String run(String value) { 207 return StringUtil.base32Decode(value); 208 } 209 }, 210 211 DECODE_FROM_BASE58("Decode from Base58") { 212 @Override 213 public String run(String value) { 214 return StringUtil.base58Decode(value); 215 } 216 }, 217 218 DECODE_FROM_BASE16("Decode from Base16") { 219 @Override 220 public String run(String value) { 221 return StringUtil.base16Decode(value); 222 } 223 }, 224 225 DECODE_FROM_HTML("Decode from Html") { 226 @Override 227 public String run(String value) { 228 return StringUtil.fromHtml(value); 229 } 230 }, 231 232 DECODE_FROM_UNICODE("Decode from Unicode") { 233 @Override 234 public String run(String value) { 235 return StringEscapeUtils.unescapeJava(value); 236 } 237 }, 238 239 DECODE_FROM_URL("Decode from Url") { 240 @Override 241 public String run(String value) { 242 return StringUtils.replaceEach( 243 StringUtil.fromUrl(value), 244 new String[] { "&", "\"", "<", ">" }, 245 new String[] { "&", """, "<", ">" } 246 ); 247 } 248 }; 249 250 protected final String name; 251 252 ActionCoder(String name) { 253 this.name = name; 254 } 255 256 public static Optional<ActionCoder> forName(String name) { 257 return Arrays.stream(ActionCoder.values()) 258 .filter(action -> name.equals(action.name)) 259 .findFirst(); 260 } 261 262 public abstract String run(String value) throws DecoderException, IOException, NoSuchAlgorithmException; 263 }