0

I would like to compute a simple program, where it will do a MD5, to hash the binary value that I've input.

I've tried google, all the program stated is hashing string. That's not I'm looking for. I want to hash binary and the result will give me in hexadecimal form.

Below is the code that I've tried, however, there's an error over at the return statement return hash , it state that byte[] cannot be converted to string.

can someone help me out with this? your help will greatly be appreciated. I'm new in programming crytographic algorithm.

import java.security.*;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class JavaApplication1 {

    public static String getMD5(byte[] plaintext) throws Exception{

        //init hash algorithm
        MessageDigest md = MessageDigest.getInstance("MD5");

        //compute hash
        byte[] hash = md.digest(plaintext);


        //display hash in hex
        System.out.println(tohex(hash));
        return hash;

    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        System.out.println(getMD5(0111001101101000011001)); 
    }

    public static String tohex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();

        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }

        return hexString.toString();
    }
}
9
  • As 0111001101101000011001 isn't anything valid in Java, what do you actually want it to be? An integer number encoded in binary? Something else? Commented Mar 17, 2017 at 14:07
  • binary > hash > hex Commented Mar 17, 2017 at 14:18
  • Yes, but binary WHAT? are the 0's and 1's in your example representing individual bits (meaning that what's given there is a 22 bit number)? Commented Mar 17, 2017 at 14:19
  • Possible duplicate of How can I generate an MD5 hash? Commented Mar 17, 2017 at 14:25
  • @fvu the binary value that I've posted is just an example of the binary value Commented Mar 17, 2017 at 14:27

1 Answer 1

1

I see a number of problems in your implementation, for example:

  • cannot pass to getMD5 function a byte string in that way.
  • getMD5 returns a String but you're trying to return a byte array. If you want the result as hexadecimal string you should change the return hash into return tohex(hash);
  • getMD5 declares throws Exception which is wrong and too generic, you should declare throws NoSuchAlgorithmException.

I suggest to convert the binary string in a byte array in this way:

String b = "0111001101101000011001";
byte[] bval = new BigInteger(b, 2).toByteArray();
System.out.println(getMD5(bval));

This is a shorter version:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class JavaApplication1 {

    public static void main(String[] args) throws NoSuchAlgorithmException  {
      String b = "0111001101101000011001";

      byte[] bval = new BigInteger(b, 2).toByteArray();

      MessageDigest md = MessageDigest.getInstance("MD5");
      byte[] hash = md.digest(bval);

      for (byte b1 : hash) {
        System.out.print(String.format("%02X", b1));
      }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

is there an example? the return hash having error too. sorry, I'm new to such program
thus, this program is hashing the binary value that I've input and the result in hexadecimal, right?
@moon just change the return value of getMD5 function into return tohex(hash);
incompatible type, string cannot be converted to byte.
@moon your question is not clear. May be this is the opportunity to create a new question on SO? :) BTW could you just remove all the unnecessary comments?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.