All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.springframework.boot.commons.util.MessageDigestUtils Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package com.github.springframework.boot.commons.util;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public final class MessageDigestUtils {

    private static final String MESSAGE_DIGEST_ALGORITHM_MD5 = "MD5";

    MessageDigestUtils() {
        throw new UnsupportedOperationException("Utility class should not be instantiated");
    }

    public static byte[] md5(final String input) {
        try {
            MessageDigest md5 = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM_MD5);
            return md5.digest(input.getBytes(StandardCharsets.UTF_8));
        } catch (NoSuchAlgorithmException e) {
			// should never happen
            return new byte[0];
        }
    }

    public static String md5Hex(final String input) {
        StringBuilder sb = new StringBuilder();
        byte[] digest = md5(input);
        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy