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:edu.cwru.apo.Auth.java

public static Hex md5(String in) {
    MessageDigest digest;
    try {//from w w  w  .j  a  v  a 2s.  c  o  m
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());

        byte messageDigest[] = digest.digest();
        return new Hex(messageDigest);

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:de.alpharogroup.crypto.sha.Hasher.java

/**
 * Hash.//from  w  ww  . jav 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(final String hashIt, final String salt, final HashAlgorithm hashAlgorithm,
        final Charset charset) throws NoSuchAlgorithmException {
    final MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithm());
    messageDigest.reset();
    messageDigest.update(salt.getBytes(charset));
    return new String(messageDigest.digest(hashIt.getBytes(charset)), charset);
}

From source file:Main.java

public static String hashBytes(byte[] input, String algo) {
    try {//from   w w  w  . j a v a 2s.  co  m
        MessageDigest md = MessageDigest.getInstance(algo);
        byte[] hashBytes = md.digest(input);
        String hash = toHexString(hashBytes);

        md.reset();
        return hash;
    } catch (NoSuchAlgorithmException e) {
        Log.e("FDroid", "Device does not support " + algo + " MessageDisgest algorithm");
        return null;
    }
}

From source file:org.osgl.util.Codec.java

/**
 * Build an hexadecimal MD5 hash for a String
 * @param value The String to hash/*w w w  .j  ava 2s . co m*/
 * @return An hexadecimal Hash
 */
public static String hexMD5(String value) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(value.getBytes("utf-8"));
        byte[] digest = messageDigest.digest();
        return byteToHexString(digest);
    } catch (Exception ex) {
        throw new UnexpectedException(ex);
    }
}

From source file:org.apache.cloudstack.utils.security.DigestHelper.java

public static String getPaddedDigest(String algorithm, String inputString) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance(algorithm);
    String checksum;//from  ww  w.  j  a va  2s  .  c  o m
    digest.reset();
    BigInteger pwInt = new BigInteger(1, digest.digest(inputString.getBytes()));
    return getPaddedDigestString(digest, pwInt);
}

From source file:Main.java

/**
 * generates MD5 of input string//  ww w . j  a  v a 2  s  . co m
 *
 * @param input string to be hashed
 * @return MD5 hash of string
 */
public static String getMD5(String input) {

    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.reset();
    m.update(input.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.toString(16);
    //zero pad to get the full 32 chars.
    while (hashtext.length() < 32) {
        hashtext = "0" + hashtext;
    }
    return hashtext;
}

From source file:org.linagora.linshare.core.utils.HashUtils.java

/**
 * hash an array of byte with a given salt
 * @param b array of byte to digest/*from w ww.  ja v  a2 s .co  m*/
 * @param salt can be null. if not it is appended to the array of bytes before doing the digest.
 * @param HashAlgo may be MD5, SHA1 ...
 * @return a base 64 string, with the encoded salt at the end if exist.
 */
private static String hashWithBase64(byte[] b, byte[] salt, String HashAlgo) {
    byte[] res = null;

    MessageDigest md;
    try {
        md = MessageDigest.getInstance(HashAlgo);
        md.reset();
        md.update(b);
        if (salt != null)
            md.update(salt);
        res = md.digest();

    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    if (salt == null) {
        return Base64Utils.encodeBytes(res);
    } else {
        res = ArrayUtils.addAll(res, salt);
        return Base64Utils.encodeBytes(res);
    }
}

From source file:com.codedx.burp.security.InvalidCertificateDialogStrategy.java

public static byte[] getSHA1(byte[] input) {
    try {//from w  w w .ja v  a2s . c om
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();
        return md.digest(input);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:support.AuthManager.java

public static String md5Custom(String st) {
    MessageDigest messageDigest = null;
    byte[] digest = new byte[0];

    try {/*from  w w w .  java2s  . co m*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(st.getBytes(StandardCharsets.UTF_8));
        digest = messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    BigInteger bigInt = new BigInteger(1, digest);
    String md5Hex = bigInt.toString(16);

    while (md5Hex.length() < 32) {
        md5Hex = "0" + md5Hex;
    }

    return md5Hex;
}

From source file:org.projectforge.common.Crypt.java

private static String encode(final String s, final String alg) {
    try {//from w ww  .  ja v  a  2s.c o m
        final MessageDigest md = MessageDigest.getInstance(alg);
        md.reset();
        md.update(s.getBytes());
        final byte[] d = md.digest();

        String ret = "";

        for (int val : d) {
            final char[] hex = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
                    'D', 'E', 'F' };
            if (val < 0) {
                val = 256 + val;
            }
            final char hi = hex[val / 16];
            final char lo = hex[val % 16];
            ret = hi + "" + lo + ret;
        }
        return md.getAlgorithm() + '{' + ret + '}';
    } catch (final NoSuchAlgorithmException ex) {
        log.fatal(ex);
        return "NONE{" + s + "}";
    }
}