Example usage for java.security MessageDigest reset

List of usage examples for java.security MessageDigest reset

Introduction

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

Prototype

public void reset() 

Source Link

Document

Resets the digest for further use.

Usage

From source file:org.craftercms.cstudio.share.forms.impl.FormUtils.java

public static String getMd5ForFile(InputStream input) {
    String result = null;//  w  w  w  .j a v  a2 s .c om
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");

        md.reset();
        byte[] bytes = new byte[1024];
        int numBytes;
        while ((numBytes = input.read(bytes)) != -1) {
            md.update(bytes, 0, numBytes);
        }
        byte[] digest = md.digest();
        result = new String(Hex.encodeHex(digest));
        input.reset();
    } catch (NoSuchAlgorithmException e) {
        logger.error("Error while creating MD5 digest", e);
    } catch (IOException e) {
        logger.error("Error while reading input stream", e);
    }
    return result;
}

From source file:Main.java

public static String md5Summary(String str) {

    if (str == null) {
        return null;
    }//  w w  w.j  a v a  2  s  .  c  o m

    MessageDigest messageDigest = null;

    try {
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("utf-8"));
    } catch (NoSuchAlgorithmException e) {

        return str;
    } catch (UnsupportedEncodingException e) {
        return str;
    }

    byte[] byteArray = messageDigest.digest();

    StringBuffer md5StrBuff = new StringBuffer();

    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        else
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
    }

    return md5StrBuff.toString();
}

From source file:cn.crawin.msg.gateway.http.MessageDigestUtil.java

/**
 * MD5??Base64???//from w  w  w  .j a v  a 2 s.com
 *
 * @return
 */
public static String base64AndMD5(byte[] bytes) {
    if (bytes == null) {
        throw new IllegalArgumentException("bytes can not be null");
    }
    try {
        final MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(bytes);
        final Base64 base64 = new Base64();
        final byte[] enbytes = base64.encode(md.digest());
        return new String(enbytes);
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("unknown algorithm MD5");
    }
}

From source file:com.ikanow.infinit.e.api.authentication.PasswordEncryption.java

public static String md5checksum(String toHash) {
    try {/* ww w.ja  v a  2s. co  m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(toHash.getBytes(Charset.forName("UTF8")));
        byte[] digest = m.digest();
        return new String(Hex.encodeHex(digest));
    } catch (Exception ex) {
        return toHash;
    }
}

From source file:com.cisco.oss.foundation.directory.utils.ObfuscatUtil.java

/**
 * Compute the the hash value for the String.
 *
 * @param passwd//from w w w. ja  v  a2s  .  com
 *         the password String
 * @return
 *         the Hash digest byte.
 * @throws NoSuchAlgorithmException
 *         the NoSuchAlgorithmException.
 */
public static byte[] computeHash(String passwd) throws NoSuchAlgorithmException {
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(passwd.getBytes());
    return md.digest();
}

From source file:dk.dma.msinm.user.security.SecurityUtils.java

/**
 * Hashes and Hex'es the password//from   w ww.  ja  v a  2 s. c o m
 * @param pwd the password
 * @param salt the salt
 * @param type the digest, e.g. "SHA-512"
 * @return the hashed and hex'ed password
 */
public static String encrypt(String pwd, String salt, String type) {
    try {
        MessageDigest md = MessageDigest.getInstance(type);
        md.reset();
        if (StringUtils.isNotBlank(salt)) {
            md.update(salt.getBytes("UTF-8"));
        }
        md.update(pwd.getBytes("UTF-8"));
        byte[] hash = md.digest();

        return hex(hash);
    } catch (Exception e) {
        throw new RuntimeException("Unable to create message digest", e);
    }
}

From source file:org.sunriseframework.util.Hash.java

public static String generateHash(byte[] input, String algorithm) {
    MessageDigest algo;
    String passwordHash = null;/*ww  w .jav a  2  s.  c  o m*/
    try {
        algo = MessageDigest.getInstance(algorithm); //MD5, SHA-1
        algo.reset();
        algo.update(input);
        Base64 encoder = new Base64();
        passwordHash = new String(encoder.encode(algo.digest()));
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return passwordHash;

}

From source file:com.kumbaya.dht.Keys.java

public static KUID of(String key) {
    try {//from ww w . ja v a2  s .  c o m
        MessageDigest md = MessageDigest.getInstance("SHA1");
        KUID result = KUID.createWithBytes(md.digest(key.getBytes("UTF-8")));
        md.reset();
        return result;
    } catch (UnsupportedEncodingException e) {
        logger.warn("failed to encode key: " + key, e);
        return null;
    } catch (NoSuchAlgorithmException e) {
        logger.warn("failed to encode key: " + key, e);
        return null;
    }
}

From source file:edu.sjsu.cohort6.esp.common.CommonUtils.java

private static String generateMD5Hash(String plaintext) throws NoSuchAlgorithmException {
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.reset();
    m.update(plaintext.getBytes());// www  .  ja va  2  s . co  m
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String 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;
    }
    return hashtext;
}

From source file:net.sourceforge.jaulp.crypto.sha.Hasher.java

/**
 * Hash./*from   ww  w  .j a  v a2s .c o m*/
 *
 * @param hashIt
 *            the hash it
 * @param salt
 *            the salt
 * @param hashAlgorithm
 *            the hash algorithm
 * @param charset
 *            the charset
 * @return the string
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the MessageDigest object fails.
 */
public static String hash(String hashIt, String salt, HashAlgorithm hashAlgorithm, Charset charset)
        throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithm());
    messageDigest.reset();
    messageDigest.update(salt.getBytes(charset));
    return new String(messageDigest.digest(hashIt.getBytes(charset)), charset);
}