|
| 1 | +package com.mkyong.crypto.hash; |
| 2 | + |
| 3 | +import com.mkyong.crypto.utils.CryptoUtils; |
| 4 | + |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.charset.Charset; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.security.DigestInputStream; |
| 12 | +import java.security.MessageDigest; |
| 13 | +import java.security.NoSuchAlgorithmException; |
| 14 | + |
| 15 | +public class ShaUtils { |
| 16 | + |
| 17 | + private static final Charset UTF_8 = StandardCharsets.UTF_8; |
| 18 | + private static final String OUTPUT_FORMAT = "%-20s:%s"; |
| 19 | + |
| 20 | + public static byte[] digest(byte[] input, String algorithm) { |
| 21 | + MessageDigest md; |
| 22 | + try { |
| 23 | + md = MessageDigest.getInstance(algorithm); |
| 24 | + } catch (NoSuchAlgorithmException e) { |
| 25 | + throw new IllegalArgumentException(e); |
| 26 | + } |
| 27 | + byte[] result = md.digest(input); |
| 28 | + return result; |
| 29 | + } |
| 30 | + |
| 31 | + private static byte[] checksum(String filePath, String algorithm) { |
| 32 | + |
| 33 | + MessageDigest md; |
| 34 | + try { |
| 35 | + md = MessageDigest.getInstance(algorithm); |
| 36 | + } catch (NoSuchAlgorithmException e) { |
| 37 | + throw new IllegalArgumentException(e); |
| 38 | + } |
| 39 | + |
| 40 | + try (InputStream is = new FileInputStream(filePath); |
| 41 | + DigestInputStream dis = new DigestInputStream(is, md)) { |
| 42 | + while (dis.read() != -1) ; //empty loop to clear the data |
| 43 | + md = dis.getMessageDigest(); |
| 44 | + } catch (IOException e) { |
| 45 | + throw new IllegalArgumentException(e); |
| 46 | + } |
| 47 | + return md.digest(); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + public static String bytesToHex(byte[] bytes) { |
| 52 | + StringBuilder sb = new StringBuilder(); |
| 53 | + for (byte b : bytes) { |
| 54 | + sb.append(String.format("%02x", b)); |
| 55 | + } |
| 56 | + return sb.toString(); |
| 57 | + } |
| 58 | + |
| 59 | + public static void main(String[] args) { |
| 60 | + |
| 61 | + //String algorithm = "SHA-256"; |
| 62 | + String algorithm = "SHA3-256"; |
| 63 | + |
| 64 | + String pText = "Hello World"; |
| 65 | + System.out.println(String.format(OUTPUT_FORMAT, "Input (string)", pText)); |
| 66 | + System.out.println(String.format(OUTPUT_FORMAT, "Input (length)", pText.length())); |
| 67 | + |
| 68 | + byte[] shaInBytes = ShaUtils.digest(pText.getBytes(UTF_8), algorithm); |
| 69 | + System.out.println(String.format(OUTPUT_FORMAT, algorithm + " (hex) ", bytesToHex(shaInBytes))); |
| 70 | + |
| 71 | + System.out.println(String.format(OUTPUT_FORMAT, algorithm + " (length)", shaInBytes.length)); |
| 72 | + |
| 73 | + // get file path from resources |
| 74 | + /*String filePath = ClassLoader.getSystemResource("sha-file.txt").getFile(); |
| 75 | +
|
| 76 | + byte[] hashInBytes = checksum(filePath, algorithm); |
| 77 | + System.out.println(bytesToHex(hashInBytes));*/ |
| 78 | + |
| 79 | + // get a 16 bytes random salt. |
| 80 | + /*byte[] salt = CryptoUtils.getRandomNonce(16); |
| 81 | + byte[] pText = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 82 | +
|
| 83 | + // combine two byte arrays |
| 84 | + byte[] input = ByteBuffer.allocate(salt.length + pText.length) |
| 85 | + .put(salt) |
| 86 | + .put(pText) |
| 87 | + .array(); |
| 88 | +
|
| 89 | + // no salt, SHA3-256 |
| 90 | + System.out.println(bytesToHex(ShaUtils.digest(pText, "SHA3-256"))); |
| 91 | +
|
| 92 | + // 16 bytes salt, SHA3-256 |
| 93 | + System.out.println(bytesToHex(ShaUtils.digest(input, "SHA3-256")));*/ |
| 94 | + |
| 95 | + } |
| 96 | +} |
0 commit comments