package com.github.booknara.cryptographic;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

/**
 * @author : Daehee Han(@daniel_booknara) on 4/27/16.
 */
public class CryptographicHashFunction {

    public static void main(String[] args) {
        String input = new String("input_string");

        MessageDigest digest;
        try {
            // MD5
            digest = MessageDigest.getInstance("MD5");
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes("UTF-8"))));
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes("UTF-8"))));
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));

            // SHA-1
            digest = MessageDigest.getInstance("SHA-1");
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));

            // SHA-256
            digest = MessageDigest.getInstance("SHA-256");
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));

            // SHA-384
            digest = MessageDigest.getInstance("SHA-384");
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));

            // SHA-512
            digest = MessageDigest.getInstance("SHA-512");
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));
            System.out.printf(digest.getAlgorithm() + " (%s) = %s \n", input, bytesTooHex(getHashValue(digest, input.getBytes())));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static byte[] getHashValue(MessageDigest digest, byte[] bytes) {
        digest.update(bytes, 0, bytes.length);
        return digest.digest();
    }

    public static String bytesTooHex(byte[] hashValue) {
        System.out.println("Length of byte : " +hashValue.length);
        Formatter formatter = new Formatter();
        for (int i = 0; i < hashValue.length; i++) {
            formatter.format("%02x", hashValue[i]);
        }

        return formatter.toString();
    }
}

@@ -565,32 +565,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -710,32 +710,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -855,32 +855,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -993,32 +993,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -1210,32 +1210,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -1348,32 +1348,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -1569,32 +1569,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -1707,32 +1707,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -1928,32 +1928,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -2066,32 +2066,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -2287,32 +2287,38 @@
 rithm() + %22 (%25s)
+ value
  = %25s %5Cn%22, input
@@ -2433,16 +2433,22 @@
 + %22 (%25s)
+ value
  = %25s %5Cn
@@ -2950,16 +2950,21 @@
 ngth of 
+hash 
 byte : %22

Update printed messages