|
| 1 | +package com.eprogrammerz.examples.general.hashGenerator; |
| 2 | + |
| 3 | +import com.google.common.io.ByteStreams; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.junit.runners.JUnit4; |
| 8 | + |
| 9 | +import javax.imageio.ImageIO; |
| 10 | +import java.awt.image.BufferedImage; |
| 11 | +import java.io.*; |
| 12 | +import java.security.MessageDigest; |
| 13 | +import java.security.NoSuchAlgorithmException; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Formatter; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +@RunWith(JUnit4.class) |
| 21 | +public class HashGenerator { |
| 22 | + public static final String HASH_ALGORITHM = "MD5"; |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testHashGenerators() throws IOException, NoSuchAlgorithmException { |
| 26 | + File input = new File("/Users/yrai/Downloads/E.tif"); |
| 27 | + |
| 28 | +// String defHashValue = getHashWithByteBuffer(input); |
| 29 | + |
| 30 | + long startTime = System.currentTimeMillis(); |
| 31 | + String handBufHashValue = getImageHashValue(input); |
| 32 | + long completeTime = System.currentTimeMillis(); |
| 33 | + System.out.println(completeTime - startTime); |
| 34 | + |
| 35 | + startTime = System.currentTimeMillis(); |
| 36 | + String hashValueWithCopy = getImageHashValueWithCopy(input); |
| 37 | + completeTime = System.currentTimeMillis(); |
| 38 | + System.out.println(completeTime - startTime); |
| 39 | + |
| 40 | + assertEquals(handBufHashValue, hashValueWithCopy); |
| 41 | + |
| 42 | + getHashWithByteBuffer(input); |
| 43 | + } |
| 44 | + |
| 45 | + public static String getImageHashValue(File input) throws IOException, NoSuchAlgorithmException { |
| 46 | + InputStream is = new FileInputStream(input); |
| 47 | + |
| 48 | + MessageDigest messageDigest = null; |
| 49 | + try { |
| 50 | + messageDigest = MessageDigest.getInstance(HASH_ALGORITHM); |
| 51 | + } catch (NoSuchAlgorithmException e) { |
| 52 | + log.error("Invalid hash algorithm: {}", HASH_ALGORITHM); |
| 53 | + } |
| 54 | + |
| 55 | + final byte[] buffer = new byte[1024]; |
| 56 | + for (int read = 0; (read = is.read(buffer)) != -1;) { |
| 57 | + messageDigest.update(buffer, 0, read); |
| 58 | + } |
| 59 | + // Convert the byte to hex format |
| 60 | + try (Formatter formatter = new Formatter()) { |
| 61 | + for (final byte b : messageDigest.digest()) { |
| 62 | + formatter.format("%02x", b); |
| 63 | + } |
| 64 | + return formatter.toString(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static String getImageHashValueWithCopy(File input) throws IOException, NoSuchAlgorithmException { |
| 69 | + InputStream is = new FileInputStream(input); |
| 70 | + |
| 71 | + MessageDigest messageDigest = null; |
| 72 | + try { |
| 73 | + messageDigest = MessageDigest.getInstance(HASH_ALGORITHM); |
| 74 | + } catch (NoSuchAlgorithmException e) { |
| 75 | + log.error("Invalid hash algorithm: {}", HASH_ALGORITHM); |
| 76 | + } |
| 77 | + |
| 78 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 79 | + ByteStreams.copy(is, out); |
| 80 | + |
| 81 | + byte[] data = out.toByteArray(); |
| 82 | + |
| 83 | + messageDigest.update(data); |
| 84 | + // Convert the byte to hex format |
| 85 | + try (Formatter formatter = new Formatter()) { |
| 86 | + for (final byte b : messageDigest.digest()) { |
| 87 | + formatter.format("%02x", b); |
| 88 | + } |
| 89 | + return formatter.toString(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static String getHashWithByteBuffer(File input) throws IOException, NoSuchAlgorithmException { |
| 94 | + BufferedImage buffImg = ImageIO.read(input); |
| 95 | + |
| 96 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 97 | + ImageIO.write(buffImg, "tif", outputStream); |
| 98 | + byte[] data = outputStream.toByteArray(); |
| 99 | + |
| 100 | + MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); |
| 101 | + md.update(data); |
| 102 | + byte[] hash = md.digest(); |
| 103 | + |
| 104 | + String hashValue = null; |
| 105 | + |
| 106 | + try (Formatter formatter = new Formatter()) { |
| 107 | + for (final byte b : hash) { |
| 108 | + formatter.format("%02x", b); |
| 109 | + } |
| 110 | + hashValue = formatter.toString(); |
| 111 | + } |
| 112 | + return hashValue; |
| 113 | + } |
| 114 | +} |
0 commit comments