Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

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

Prototype

public byte[] digest(byte[] input) 

Source Link

Document

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

Usage

From source file:com.myb.portal.util.Digests.java

/**
 * , ?md5sha1.//from   w w  w  .j  a v a  2 s  . co  m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        //         throw Exceptions.unchecked(e);
    }
    return salt;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.authenticate.Authenticator.java

/**
 * Apply MD5 to this string, and encode as a string of hex digits. Just
 * right for storing passwords in the database, or hashing the password
 * link.//www .  j  a va2  s . c o m
 */
public static String applyMd5Encoding(String raw) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(raw.getBytes());
        char[] hexChars = Hex.encodeHex(digest);
        return new String(hexChars).toUpperCase();
    } catch (NoSuchAlgorithmException e) {
        // This can't happen with a normal Java runtime.
        throw new RuntimeException(e);
    }
}

From source file:com.thinkgem.jeesite.modules.sso.util.Digests.java

/**
 * , ?md5sha1.// w  ww. j a  va  2 s. c o m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        return "".getBytes();
    }
}

From source file:SecureHash.java

/**
 * Get the hash of the supplied content, using the digest identified by the supplied name.
 * /*from  w w  w.j a v a2s .  c o m*/
 * @param digestName the name of the hashing function (or {@link MessageDigest message digest}) that should be used
 * @param content the content to be hashed; may not be null
 * @return the hash of the contents as a byte array
 * @throws NoSuchAlgorithmException if the supplied algorithm could not be found
 */
public static byte[] getHash(String digestName, byte[] content) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance(digestName);
    assert digest != null;
    return digest.digest(content);
}

From source file:com.googlecode.osde.internal.utils.OpenSocialUtil.java

public static ApplicationInformation createApplicationInformation(IFile file)
        throws CoreException, ParserException {
    try {/*from   w w w.j av a2s .c om*/
        Parser<Module> parser = ParserFactory.gadgetSpecParser();
        Module module = parser.parse(file.getContents());

        String path = file.getFullPath().toPortableString();
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] hash = digest.digest(path.getBytes("UTF-8"));
        String appId = Gadgets.toHexString(hash);
        String consumerKey = "osde:" + path;
        digest.reset();
        hash = digest.digest(consumerKey.getBytes("UTF-8"));
        String consumerSecret = Gadgets.toHexString(hash);
        ApplicationInformation info = new ApplicationInformation();
        info.setAppId(appId);
        info.setModule(module);
        info.setPath(file.getFullPath().toPortableString());
        info.setConsumerKey(consumerKey);
        info.setConsumerSecret(consumerSecret);
        return info;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.googlecode.osde.internal.utils.OpenSocialUtil.java

public static ApplicationInformation createApplicationInformation(String url)
        throws CoreException, ParserException, IOException {
    try {//from  w  w  w.  jav  a2  s . com
        Parser<Module> parser = ParserFactory.gadgetSpecParser();
        Module module = parser.parse(new URL(url).openStream());

        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] hash = digest.digest(url.getBytes("UTF-8"));
        String appId = Gadgets.toHexString(hash);
        String consumerKey = "osde:" + url;
        digest.reset();
        hash = digest.digest(consumerKey.getBytes("UTF-8"));
        String consumerSecret = Gadgets.toHexString(hash);
        ApplicationInformation info = new ApplicationInformation();
        info.setAppId(appId);
        info.setModule(module);
        info.setPath(url);
        info.setConsumerKey(consumerKey);
        info.setConsumerSecret(consumerSecret);
        return info;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    } catch (MalformedURLException e) {
        throw new IllegalStateException(e);
    }
}

From source file:net.cloudkit.enterprises.infrastructure.utilities.DigestsHelper.java

/**
 * , ?md5sha1.//w  ww . ja v a 2 s. co m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw ExceptionHelper.unchecked(e);
    }
}

From source file:com.anybuy.util.DigestUtil.java

/**
 * , ?md5sha1./*ww  w. j  ava 2s  .c o m*/
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw ExceptionUtil.unchecked(e);
    }
}

From source file:com.google.gerrit.server.mail.send.FromAddressGeneratorProvider.java

private static String hashOf(String data) {
    try {/*  w  w  w.  ja  v  a  2  s .  c o m*/
        MessageDigest hash = MessageDigest.getInstance("MD5");
        byte[] bytes = hash.digest(data.getBytes(UTF_8));
        return Base64.encodeBase64URLSafeString(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("No MD5 available", e);
    }
}

From source file:Main.java

public static String getHash(String stringToHash) {

    MessageDigest digest = null;
    try {/*w ww .j  a va  2s  . c  om*/
        digest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    byte[] result = null;

    try {
        result = digest.digest(stringToHash.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    StringBuilder sb = new StringBuilder();

    for (byte b : result) {
        sb.append(String.format("%02X", b));
    }

    String messageDigest = sb.toString();
    return messageDigest;
}