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:StringUtil.java

/**
 * Generates a hash code for a given source code. 
 * This method ignores whitespace in generating the hash code.
 * @param source/*from w  w  w . j  a  v a 2 s . co  m*/
 * @return
 */
public static String hashSourceCode(String source) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
        md.update(source.getBytes());
        return new sun.misc.BASE64Encoder().encode(md.digest());
    } catch (NoSuchAlgorithmException e) {
        //_log.error("Failed to generate hashcode.", e);
    }
    return null;
}

From source file:fr.cph.stock.security.Security.java

/**
 * Encode to sha256 the user password//ww  w .j a  v  a2s  . c  om
 * 
 * @param str
 *            the password to encode
 * @return an encoded string
 * @throws NoSuchAlgorithmException
 *             the NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 *             the UnsupportedEncodingException
 */
public static String encodeToSha256(final String str)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] hash = digest.digest(str.getBytes("UTF-8"));
    String encoded = Hex.encodeHexString(hash);
    return encoded;
}

From source file:com.amazonaws.util.Md5Utils.java

/**
 * Computes the MD5 hash of the data in the given input stream and returns
 * it as an array of bytes.//from  w w  w  .  j av  a  2 s. c  o  m
 * Note this method closes the given input stream upon completion.
 */
public static byte[] computeMD5Hash(InputStream is) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(is);
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[SIXTEEN_K];
        int bytesRead;
        while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) {
            messageDigest.update(buffer, 0, bytesRead);
        }
        return messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        // should never get here
        throw new IllegalStateException(e);
    } finally {
        try {
            bis.close();
        } catch (Exception e) {
            LogFactory.getLog(Md5Utils.class).debug("Unable to close input stream of hash candidate: " + e);
        }
    }
}

From source file:com.redhat.rhn.common.util.SHA256Crypt.java

/**
 * getSHA256MD - get SHA256 MessageDigest object instance
 * @return MessageDigest object instance
 *///w  w w.ja v a2  s  .  co m
private static MessageDigest getSHA256MD() {
    MessageDigest md;

    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new SHA256CryptException("Problem getting SHA-256 message digest");
    }

    return md;
}

From source file:Main.java

public static String getMd5Digest(String pInput) {
    try {/*  w  w  w  .  ja v  a 2s  .  com*/
        MessageDigest lDigest = MessageDigest.getInstance("MD5");
        lDigest.update(getBytes(pInput));
        BigInteger lHashInt = new BigInteger(1, lDigest.digest());
        return String.format("%1$032X", lHashInt);
    } catch (NoSuchAlgorithmException lException) {
        throw new RuntimeException(lException);
    }
}

From source file:license.regist.ReadProjectInfo.java

private static byte[] md5(String info) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < info.length(); i++)
        sb.append(Integer.valueOf(info.charAt(i)));
    return md.digest(sb.toString().getBytes());
}

From source file:cn.edu.zjnu.acm.judge.security.password.MessageDigestPasswordEncoder.java

private static MessageDigest getMessageDigest(String algorithmName) {
    try {//from  w  w  w.j a v a  2  s .  co m
        return MessageDigest.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}

From source file:DigestUtils.java

/**
 * Returns a MessageDigest for the given <code>algorithm</code>.
 * //from  www.j  a v a 2 s.  com
 * @param algorithm
 *          The MessageDigest algorithm name.
 * @return An MD5 digest instance.
 * @throws RuntimeException
 *           when a {@link java.security.NoSuchAlgorithmException} is caught,
 */
static MessageDigest getDigest(String algorithm) {
    try {
        return MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Gestores.GestorHash.java

public String md5(String plaintext) {
    String hashtext = "";
    try {/*  ww w .  j  ava 2  s  .  c o  m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(plaintext.getBytes());
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        hashtext = bigInt.toString(16);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(GestorHash.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hashtext;
}

From source file:com.topsem.common.security.utils.Digests.java

/**
 * , ?md5sha1./* w  w  w .  j  av a2  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) {
        throw Throwables.propagate(e);
    }
}