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();
    }
}
freedev
  • 25,946
  • 8
  • 108
  • 125
moon
  • 3
  • 5
  • As `0111001101101000011001` isn't anything valid in Java, what do you actually want it to be? An integer number encoded in binary? Something else? – fvu Mar 17 '17 at 14:07
  • binary > hash > hex – moon Mar 17 '17 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)? – fvu Mar 17 '17 at 14:19
  • Possible duplicate of [How can I generate an MD5 hash?](http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash) – freedev Mar 17 '17 at 14:25
  • @fvu the binary value that I've posted is just an example of the binary value – moon Mar 17 '17 at 14:27
  • @freedev i want to generate from a binary to hash, not string to hash. – moon Mar 17 '17 at 14:28
  • @moon I added that piece of code just to print something at end of conversion... `getMD5` just return the md5 – freedev Mar 17 '17 at 14:30
  • You seem to be missing my point: are the 0's and 1's in your example representing individual bits? – fvu Mar 17 '17 at 14:36
  • yes, it's representing individual bits – moon Mar 17 '17 at 14:39

1 Answers1

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));
      }
    }
}
freedev
  • 25,946
  • 8
  • 108
  • 125
  • is there an example? the return hash having error too. sorry, I'm new to such program – moon Mar 17 '17 at 14:18
  • thus, this program is hashing the binary value that I've input and the result in hexadecimal, right? – moon Mar 17 '17 at 14:36
  • @moon just change the return value of `getMD5` function into `return tohex(hash);` – freedev Mar 17 '17 at 14:41
  • incompatible type, string cannot be converted to byte. – moon Mar 17 '17 at 14:44
  • what I currently would like to achieve is, given a binary values, I would want to use MD5 to hash it and have a hexadecimal output – moon Mar 17 '17 at 15:01
  • I input binary, why do I need to return byte array of binary? – moon Mar 17 '17 at 15:10
  • 1
    @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? – freedev Mar 17 '17 at 15:55
  • @moon May I ask if you think that my answer is right? If yes [why don't you mark it as correct](http://stackoverflow.com/help/accepted-answer)? – freedev Mar 17 '17 at 15:56