Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

In this page you can find the example usage for java.security MessageDigest getInstance.

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:cn.ucloud.sdk.utils.EncoderUtils.java

private static String encode(String algorithm, String str) {
    String result = null;/*from  ww w .j a v  a2s.  com*/
    if (str == null) {
        return result;
    }
    try {
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        messageDigest.update(str.getBytes("UTF-8"));
        result = getFormattedText(messageDigest.digest());
    } catch (Exception e) {
        LogUtils.exception(logger, e);
    }
    return result;
}

From source file:eu.peppol.document.PayloadDigestCalculator.java

public static MessageDigestResult calcDigest(String algorithm, StandardBusinessDocumentHeader sbdh,
        InputStream inputStream) {
    MessageDigest messageDigest;/*ww w  . ja va2 s . c om*/

    try {
        messageDigest = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(
                "Unknown digest algorithm " + OxalisConstant.DEFAULT_DIGEST_ALGORITHM + " " + e.getMessage(),
                e);
    }

    InputStream inputStreamToCalculateDigestFrom = null;

    ManifestItem manifestItem = SbdhFastParser.searchForAsicManifestItem(sbdh);
    if (manifestItem != null) {
        // creates an FilterInputStream, which will extract the ASiC in binary format.
        inputStreamToCalculateDigestFrom = new Base64InputStream(new AsicFilterInputStream(inputStream));
    } else
        inputStreamToCalculateDigestFrom = inputStream;

    DigestInputStream digestInputStream = new DigestInputStream(
            new BufferedInputStream(inputStreamToCalculateDigestFrom), messageDigest);
    try {
        IOUtils.copy(digestInputStream, new NullOutputStream());
    } catch (IOException e) {
        throw new IllegalStateException("Unable to calculate digest for payload");
    }

    return new MessageDigestResult(messageDigest.digest(), messageDigest.getAlgorithm());
}

From source file:MessageDigestUtil.java

/**
 * MessageDisgest???????/*from  w w w. j a  va 2s  .c o  m*/
 * 
 * @return
 */
public static MessageDigest createMessageDigest() {
    try {
        return MessageDigest.getInstance(ALGORHTYM);
    } catch (NoSuchAlgorithmException e) {
        throw new ExceptionInInitializerError();
    }
}

From source file:com.nunofacha.chestmaster.commands.ChestHashCommand.java

public static String getPluginHash() {
    try {/*from   w ww.  j  a  v a2 s .c o  m*/
        String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String decodedPath = URLDecoder.decode(path, "UTF-8");
        MessageDigest md = MessageDigest.getInstance("MD5");
        String digest = getDigest(new FileInputStream(decodedPath), md, 2048);
        return digest;
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(ChestHashCommand.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(ChestHashCommand.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ChestHashCommand.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "Failed to get MD5!";

}