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

static public String md5(String str) {
    MessageDigest algorithm = null;
    try {/*from w w w  .j a  v a  2 s .c  o m*/
        algorithm = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (algorithm != null) {
        algorithm.reset();
        algorithm.update(str.getBytes());
        byte[] bytes = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            if (Integer.toHexString(0xFF & b).length() == 1)
                hexString.append("0").append(Integer.toHexString(0xFF & b));
            else
                hexString.append(Integer.toHexString(0xFF & b));
        }
        return hexString.toString();
    }
    return "";

}

From source file:zebrinho.domain.User.java

public static String genHash(String password) {
    try {// www . jav a  2s .com
        MessageDigest cript = MessageDigest.getInstance("SHA-1");
        cript.reset();
        cript.update(("salt:" + password + ":cenas").getBytes("utf8"));
        return new String(Hex.encodeHex(cript.digest()));
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:dk.netarkivet.common.utils.ChecksumCalculator.java

public static byte[] digestInputStream(InputStream instream, String algorithm) {
    byte[] buffer = new byte[Constants.IO_BUFFER_SIZE];
    MessageDigest messageDigest = getMessageDigest(algorithm);
    messageDigest.reset();
    int bytesRead;
    try {/* ww w  . j a v  a 2  s  . c  o  m*/
        while ((bytesRead = instream.read(buffer)) != -1) {
            messageDigest.update(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new IOFailure("Error making a '" + algorithm + "' digest on the inputstream", e);
    }
    return messageDigest.digest();
}

From source file:org.casbah.provider.SSLeayEncoder.java

private static DESedeKeySpec calculateKeyFromPassKey(byte[] keypass, byte[] salt)
        throws IOException, GeneralSecurityException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] digest = null;
    while (baos.size() < 24) {
        md.reset();
        if (digest != null) {
            md.update(digest);//from w  w  w  .  j a  v a  2 s  . co m
        }
        md.update(keypass);
        digest = md.digest(salt);
        baos.write(digest);
    }
    DESedeKeySpec result = new DESedeKeySpec(baos.toByteArray());
    return result;
}

From source file:com.seleniumtests.util.StringUtility.java

public static String md5(final String str) {

    if (str == null) {
        return null;
    }/*from w w  w .j ava2s. c o  m*/

    MessageDigest messageDigest = null;

    try {
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
        logger.error(e);
        return str;
    }

    byte[] byteArray = messageDigest.digest();

    return toHexString(byteArray);
}

From source file:me.j360.dubbo.modules.util.text.HashUtil.java

private static MessageDigest get(ThreadLocal<MessageDigest> messageDigest) {
    MessageDigest instance = messageDigest.get();
    instance.reset();
    return instance;
}

From source file:de.itsvs.cwtrpc.controller.token.DefaultXsrfTokenService.java

protected static String getMd5HexString(byte[] bytes) {
    final MessageDigest md;
    final StringBuilder value;

    md = md5ThreadLocal.get();/*  www. j a  va 2s  .  c o m*/
    md.reset();
    md.update(bytes);

    value = new StringBuilder();
    for (byte b : md.digest()) {
        final int val = b & 0xff;

        if (val < 16) {
            value.append('0');
        }
        value.append(Integer.toHexString(val));
    }

    return value.toString();
}

From source file:dk.netarkivet.common.utils.ChecksumCalculator.java

/**
 * Calculates a digest on an InputStream, throwing away the data itself. Throws Alert if there is an error reading
 * from the stream/*from  www  .  j  av  a  2 s  .c o  m*/
 *
 * @param instream An <code>InputStream</code> to calculate the digest on. The contents of the stream will be
 * consumed by this call, but the stream will not be closed.
 * @param algorithm digest algorithm to use
 * @return The calculated digest as a string.
 */
private static String calculateDigest(final InputStream instream, final String algorithm) {
    final byte[] buffer = new byte[Constants.IO_BUFFER_SIZE];
    final MessageDigest messageDigest = getMessageDigest(algorithm);
    messageDigest.reset();
    int bytesRead;
    try {
        while ((bytesRead = instream.read(buffer)) != -1) {
            messageDigest.update(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new IOFailure("Error making a '" + algorithm + "' digest on the inputstream", e);
    }
    return toHex(messageDigest.digest());
}

From source file:rusch.megan6server.TextFileAuthentication.java

public static String md5(String s) throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    messageDigest.reset();
    messageDigest.update(s.getBytes(Charset.forName("UTF8")));
    byte[] resultByte = messageDigest.digest();
    return new String(Hex.encodeHex(resultByte));
}

From source file:com.dtolabs.rundeck.core.execution.commands.ScriptURLCommandInterpreter.java

private static String hashURL(final String url) {
    try {/*from   ww w  .ja va  2s  . c o  m*/
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(url.getBytes(Charset.forName("UTF-8")));
        return new String(Hex.encodeHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return Integer.toString(url.hashCode());
}