Java MD5 String md5Hex(String message)

Here you can find the source of md5Hex(String message)

Description

MD5 digest

License

MIT License

Parameter

Parameter Description
message a parameter

Return

the MD5 digest or null if the system does not support MD5 digest

Declaration

public static String md5Hex(String message) 

Method Source Code

//package com.java2s;
/*//from   w w  w.j a v  a  2 s  .  c  om
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013, 2014, 2015 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */

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

public class Main {
    /**
     * MD5 digest
     *
     * @param message
     * @return the MD5 digest or null if the system does not support MD5 digest
     */
    public static String md5Hex(String message) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return hex(md.digest(message.getBytes("CP1252")));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        }
        return null;
    }

    /**
     * Given a byte array, return its hexadecimal string representation
     *
     * @param array
     * @return hexadecimal string representation of byte array
     */
    public static String hex(byte[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
                    .substring(1, 3));
        }
        return sb.toString();
    }
}

Related

  1. md5file(File file)
  2. md5FromFile(File file)
  3. md5fromFile(String path)
  4. md5Hex(String data)
  5. md5Hex(String data)
  6. md5Hex(String message)
  7. md5Hex(String message)
  8. md5Hex(String message)
  9. md5Hex(String message)