[ 자바] 이미지해싱하기


package com.unclebae.image.hash;

import java.io.*;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class ImageHash {

    public static final String HASHING_MD5 = "MD5";

    public String hashing(String imagePath) {

        if (imagePath == null) {
            return null;
        }

        MessageDigest md = null;

        try {
            md = MessageDigest.getInstance(HASHING_MD5);
        } catch (NoSuchAlgorithmException e) {
            System.out.printf("There is not hasing algorithm.");
            return null;
        }

        byte[] bytes = new byte[1024];
        int readBytes = 0;

        StringBuffer sb = new StringBuffer();
        try {
            InputStream resourceAsStream = new BufferedInputStream(new FileInputStream(new File(imagePath)));
            while((readBytes = resourceAsStream.read(bytes)) != -1) {
                md.update(bytes, 0, readBytes);
            }

            byte[] mdBytes = md.digest();

            for (byte bt : mdBytes) {
                sb.append(Integer.toString((bt & 0xff) + 0x100, 16).substring(1));
            }

            resourceAsStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        ImageHash hash = new ImageHash();
        String hashing = hash.hashing("/Users/kido/Documents/사진/01.jpg");
       
        System.out.println("Hash Result : " + hashing);
    }
}







Share this

Related Posts

Previous
Next Post »