简体   繁体   中英

Generate MD5 hash in Java -Output 16 character

I have created MD5 hash.Its working fine now.I want output in 16 character.Current Code is returning 32 characters.

This is my code:

    try {
            String text = "Hello World";
            MessageDigest msg = MessageDigest.getInstance("MD5");
            msg.update(text.getBytes(), 0, text.length());
            String digest1 = new BigInteger(1, msg.digest()).toString(16);
            System.out.println("MD5: " + digest1.length());
            System.out.println("MD5: " + digest1);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(AndroidActivationView.class.getName()).log(Level.SEVERE, null, ex);
        }

Where I want to change it.

How can i get the 16 character output.?

Thanks in advance;

Cut the string to length: digest1 = digest1.substring(0, 16);

MD5 outputs 16 bytes. If you encode it in hexadecimal, its 32 characters. If you encode it in base-64 it's 24 characters. Base-85 will squeeze it into 20 characters. There's no well-known encoding that is one character per byte.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM