Java MD5 byteArrayMD5(byte[] entity)

Here you can find the source of byteArrayMD5(byte[] entity)

Description

Computes the MD5 of a byte array.

License

Open Source License

Parameter

Parameter Description
entity the byte array to checksum.

Return

the MD5 hex string.

Declaration

public static String byteArrayMD5(byte[] entity) 

Method Source Code


//package com.java2s;
/*//  w  ww.  ja v a  2 s. c  o m
 * Copyright 2015 EMC Corporation. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 * http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import javax.xml.bind.DatatypeConverter;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**
     * Computes the MD5 of a byte array.
     * @param entity the byte array to checksum.
     * @return the MD5 hex string.
     */
    public static String byteArrayMD5(byte[] entity) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            // Should never happen
            throw new RuntimeException("Could not load MD5", e);
        }
        return toHexString(md5.digest(entity));
    }

    /**
     * Converts a byte array to a hex string.
     * @param arr the array to convert.
     * @return the array's contents as a string of hex characters.
     */
    public static String toHexString(byte[] arr) {
        return DatatypeConverter.printHexBinary(arr);
    }
}

Related

  1. core_md5(int[] K, int F)
  2. decodeMd5(String source)
  3. decryMd5(String source)
  4. encryptmd5(String str)