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

org.bardframework.commons.utils.DigestUtils Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version
package org.bardframework.commons.utils;

import lombok.experimental.UtilityClass;

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

@UtilityClass
public class DigestUtils {

    private static final String SHA512 = "SHA512";
    private static final String SHA = "SHA";

    public static String sha512(String data)
            throws NoSuchAlgorithmException {
        return digest(SHA512, data.getBytes(StandardCharsets.UTF_8));
    }

    public static String sha(String data)
            throws NoSuchAlgorithmException {
        return digest(SHA, data);
    }

    public static byte[] sha(byte[] data)
            throws NoSuchAlgorithmException {
        return rawDigest(SHA, data);
    }

    public static String digest(String alg, String data)
            throws NoSuchAlgorithmException {
        return digest(alg, data.getBytes(StandardCharsets.UTF_8));
    }

    public static String digest(String alg, byte[] data)
            throws NoSuchAlgorithmException {
        return EncodingUtils.hexEncode(rawDigest(alg, data));
    }

    public static byte[] rawDigest(String alg, byte[] data)
            throws NoSuchAlgorithmException {
        return MessageDigest.getInstance(alg).digest(data);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy