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.easyrec.utils.io.Text.java

/**
 * Generates a 32 char MD5 hash from a string:
 * "hallo" --> "b6834520c5cf3df80886803e1af41b47"
 *
 * @param key/*from   w  ww . j  a v a2  s.  c o m*/
 *
 */
public static String generateHash(String key) {

    // note: changing the key fucks up the DEFAULT API KEY
    // so use it with caution
    key += "use_your_key_here";

    MessageDigest md;
    try {
        md = java.security.MessageDigest.getInstance("MD5");
        md.reset();
        md.update(key.getBytes());
        byte[] bytes = md.digest();
        // buffer to write the md5 hash to
        StringBuffer buff = new StringBuffer();
        for (int l = 0; l < bytes.length; l++) {
            String hx = Integer.toHexString(0xFF & bytes[l]);
            // make sure the hex string is correct if 1 character
            if (hx.length() == 1)
                buff.append("0");
            buff.append(hx);
        }
        return buff.toString().trim();
    } catch (NoSuchAlgorithmException e) {
        logger.warn("An error occurred!", e);
    }
    return null;
}

From source file:Main.java

public static byte[] hash(String message) {
    MessageDigest cript = null;
    try {//from   w w w. ja va2  s  . c o m
        cript = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    cript.reset();
    try {
        cript.update(message.getBytes("utf8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    return cript.digest();
}

From source file:org.slc.sli.test.utils.DataUtils.java

public static final String createMd5ForFile(String file) {
    File myFile = new File(file);
    DigestInputStream dis = null;
    try {/*from   ww w.j ava 2s . com*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        dis = new DigestInputStream(new FileInputStream(myFile), md);
        byte[] buf = new byte[1024];
        while (dis.read(buf, 0, 1024) != -1) {
        }
        return Hex.encodeHexString(dis.getMessageDigest().digest());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.mnt.base.util.HashUtil.java

public static String hashBy(String source, String mdType) {

    if (source != null) {
        MessageDigest digest = getMessageDigest(mdType);

        if (digest != null) {
            digest.reset();
            digest.update(source.getBytes());

            return toHexString(digest.digest());
        } else {/*from  w w w  .j  a  va2s  . c o  m*/
            return source;
        }
    }

    return null;
}

From source file:PublisherUtil.java

/**
 * Utility for getting the MD5 hash from the provided key for sending the publishPassword.
 * //w  w w  .j a v  a2  s .  co  m
 * @param passWord
 *          The password to get an MD5 hash of
 * @return zero-padded MD5 hash of the password
 */
public static final String getPasswordKey(final String passWord) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
        md.reset(); // Reset the algorithm
        md.update(passWord.getBytes()); // Update the algorithm with the e-mail
        // Update the algorithm with a known "key" for keyed MD5
        // It basically adds the new key to the end and computes
        byte[] digest = md.digest("P3ntah0Publ1shPa55w0rd".getBytes()); //$NON-NLS-1$
        StringBuffer buf = new StringBuffer();
        String s;
        for (byte element : digest) {
            s = Integer.toHexString(0xFF & element);
            buf.append((s.length() == 1) ? "0" : "").append(s); //$NON-NLS-1$ //$NON-NLS-2$
        }
        return buf.toString(); // Return MD5 string
    } catch (NoSuchAlgorithmException ex) {
        PublisherUtil.logger.error(null, ex);
        return null;
    }
}

From source file:com.zen.androidhtmleditor.util.TextUtil.java

public static String MD5(String str, String encoding) {
    MessageDigest messageDigest = null;

    try {//from w  ww .j  av  a2 s .c o  m
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    messageDigest.reset();
    try {
        messageDigest.update(str.getBytes(encoding));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    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:com.github.benyzhous.springboot.web.core.gateway.sign.backend.Sign.java

/**
 * MD5??Base64???//  www.j  a va2  s  .com
 *
 * @param bytes 
 * @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();

        return new String(base64.encode(md.digest()));
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("unknown algorithm MD5");
    }
}

From source file:nl.strohalm.cyclos.utils.HashHandler.java

private static String digest(final String algorithm, final String salt, final String string) {
    if (string == null) {
        return null;
    }//from   w  ww  . ja  va  2s .c  o  m
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance(algorithm);
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    }
    md.reset();
    try {
        if (salt != null) {
            md.update(salt.getBytes("UTF-8"));
        }
        return toHex(md.digest(string.getBytes("UTF-8")));
    } catch (final UnsupportedEncodingException e) {
        // Never happens as UTF-8 is always supported
        return null;
    }
}

From source file:com.raphfrk.craftproxyclient.net.protocol.p17xlogin.P17xLoginProtocol.java

private static String SHA1Hash(Object[] input) {
    try {/* w w w  .j  av  a  2s. com*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();

        for (Object o : input) {
            if (o instanceof String) {
                md.update(((String) o).getBytes("ISO_8859_1"));
            } else if (o instanceof byte[]) {
                md.update((byte[]) o);
            } else {
                return null;
            }
        }

        byte[] digest = md.digest();

        BigInteger bigInt = new BigInteger(digest);

        return bigInt.toString(16);
    } catch (Exception ioe) {
        return null;
    }
}

From source file:net.dbjorge.jthumbor.ThumborUtils.java

/**
 * MD5 hashes the given input string and returns the hex digest in String form.
 *
 * Input may not be null or empty.//  w  ww .  j a va2s.  c  o  m
 */
public static String md5String(String input) {
    String result = "";
    MessageDigest algorithm;
    try {
        algorithm = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    algorithm.reset();
    algorithm.update(input.getBytes());
    byte[] md5 = algorithm.digest();
    String tmp = "";
    for (int i = 0; i < md5.length; i++) {
        tmp = (Integer.toHexString(0xFF & md5[i]));
        if (tmp.length() == 1) {
            result += "0" + tmp;
        } else {
            result += tmp;
        }
    }
    return result;
}