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

/**
 * md5/*w w w . j ava2 s . c  om*/
 * 
 * @param str
 * @return
 */
public static String md5(String str) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
        byte[] byteArray = messageDigest.digest();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                sb.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
            } else {
                sb.append(Integer.toHexString(0xFF & byteArray[i]));
            }
        }

        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return str;
}

From source file:auguste.client.command.client.AccountCreate.java

private static String hashPassword(String password) {
    try {//from w ww . j av a  2 s. c  om
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(password.getBytes());
        return new String(Hex.encodeHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        // Algorithme indisponible
        System.out.println(e.getMessage());
        return new String();
    }
}

From source file:org.tizzit.util.MD5.java

public static final String computeHash(String stringToCompile) {
    String retVal = null;/*from   ww  w  . jav  a 2s  .c  o m*/

    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        md5.update(stringToCompile.getBytes());
        byte[] result = md5.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            hexString.append(Integer.toHexString(0xFF & result[i]));
        }
        retVal = hexString.toString();
        if (log.isDebugEnabled())
            log.debug("MD5 hash for \"" + stringToCompile + "\" is: " + retVal);
    } catch (Exception exe) {
        log.error(exe.getMessage(), exe);
    }

    return retVal;
}

From source file:MD5.java

public static String create(String content) throws MD5IsNotSupported {
    String result = "";
    try {/*w  w w.j  a  va2s .c o m*/
        byte[] defaultBytes = content.getBytes();
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        result = hexString.toString();
    } catch (NoSuchAlgorithmException ex) {
        throw new MD5IsNotSupported(ex);
    }

    assert !result.isEmpty();
    return result;
}

From source file:fm.audiobox.core.utils.MD5Checksum.java

/**
 * Calculate checksum of a File using MD5 algorithm
 *//*from  w ww . j  av  a  2s  .  c  om*/
public static String checkSum(InputStream is) throws NoSuchAlgorithmException {
    String checksum = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();

        // Using MessageDigest update() method to provide input
        byte[] buffer = new byte[8192];
        int numOfBytesRead;
        while ((numOfBytesRead = is.read(buffer)) > 0) {
            md.update(buffer, 0, numOfBytesRead);
        }
        byte[] hash = md.digest();
        checksum = new String(Hex.encodeHex(hash));
    } catch (IOException ex) {
        logger.error(ex.getMessage());
    }

    return checksum;
}

From source file:org.apache.hadoop.tools.rumen.RandomSeedGenerator.java

/**
 * Generates a new random seed.//from w ww .  j a  va  2  s.  co  m
 *
 * @param streamId a string identifying the stream of random numbers
 * @param masterSeed higher level master random seed
 * @return the random seed. Different (streamId, masterSeed) pairs result in
 *         (vastly) different random seeds.
 */
public static long getSeed(String streamId, long masterSeed) {
    MessageDigest md5 = md5Holder.get();
    md5.reset();
    //'/' : make sure that we don't get the same str from ('11',0) and ('1',10)
    // We could have fed the bytes of masterSeed one by one to md5.update()
    // instead
    String str = streamId + '/' + masterSeed;
    byte[] digest = md5.digest(str.getBytes());
    // Create a long from the first 8 bytes of the digest
    // This is fine as MD5 has the avalanche property.
    // Paranoids could have XOR folded the other 8 bytes in too. 
    long seed = 0;
    for (int i = 0; i < 8; i++) {
        seed = (seed << 8) + ((int) digest[i] + 128);
    }
    return seed;
}

From source file:org.calrissian.accumulorecipes.blobstore.support.BlobUtils.java

public static String hashBytes(byte[] bytes) {

    try {/*from   w w w. jav a2s.c  o  m*/

        final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(bytes);
        final byte[] resultByte = messageDigest.digest();

        return new String(Hex.encodeHex(resultByte));

    } catch (NoSuchAlgorithmException e) {
        logger.error("There was an exception trying to initialize the MD5 digest");
    }

    return null;

}

From source file:org.zaproxy.zap.extension.cmss.CMSSUtils.java

public static String checksum(byte[] octets) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    messageDigest.reset();
    messageDigest.update(octets);// ww w .j a v a2  s . co m
    final byte[] resultByte = messageDigest.digest();
    return new String(Hex.encodeHex(resultByte));
}

From source file:Main.java

private static String md5(String in) {
    MessageDigest digest;
    try {//from w ww. j  a v a2s .co m
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        byte[] a = digest.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String md5(final String in) {
    MessageDigest digest;
    try {/*from ww w  . ja va2s.co  m*/
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        final byte[] a = digest.digest();
        final int len = a.length;
        final StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}