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:com.nunofacha.chestmaster.commands.ChestHashCommand.java

public static String getDigest(InputStream is, MessageDigest md, int byteArraySize) {

    try {//  w  ww.  j a va  2 s. c  om
        md.reset();
        byte[] bytes = new byte[byteArraySize];
        int numBytes;
        while ((numBytes = is.read(bytes)) != -1) {
            md.update(bytes, 0, numBytes);
        }
        byte[] digest = md.digest();
        String result = new String(Hex.encodeHex(digest));
        return result;
    } catch (IOException ex) {
        Logger.getLogger(ChestHashCommand.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "Failed to get hash!";
}

From source file:data.repository.pragma.utils.MD5Utils.java

public static String getDigest(InputStream is, MessageDigest md, int byteArraySize) {

    md.reset();
    byte[] bytes = new byte[byteArraySize];
    int numBytes;
    try {//  www  .j a v a 2  s.  c  om
        while ((numBytes = is.read(bytes)) != -1) {
            md.update(bytes, 0, numBytes);
        }
        byte[] digest = md.digest();
        String result = new String(Hex.encodeHex(digest));
        return result;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return null;
    }
}

From source file:org.apache.roller.weblogger.util.WSSEUtilities.java

public static synchronized String generateDigest(byte[] nonce, byte[] created, byte[] password) {
    String result = null;/*from ww w .j a  va 2  s.  c o  m*/
    try {
        MessageDigest digester = MessageDigest.getInstance("SHA");
        digester.reset();
        digester.update(nonce);
        digester.update(created);
        digester.update(password);
        byte[] digest = digester.digest();
        result = new String(base64Encode(digest));
    } catch (NoSuchAlgorithmException e) {
        result = null;
    }
    return result;
}

From source file:Main.java

public static String getMD5(String str) {
    MessageDigest messageDigest = null;
    try {/*from  w  ww.  j  a  v a2  s . c  om*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
    } catch (UnsupportedEncodingException e) {
    }
    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.substring(8, 24).toString();
}

From source file:Main.java

static long md5sum(String text) {

    byte[] defaultBytes = text.getBytes();

    try {//from ww  w. j  a va  2  s .  c o  m

        MessageDigest algorithm = MessageDigest.getInstance("MD5");

        algorithm.reset();

        algorithm.update(defaultBytes);

        byte digest[] = algorithm.digest();

        ByteBuffer buffer = ByteBuffer.wrap(digest);

        return buffer.getLong();

    } catch (NoSuchAlgorithmException e) {

        Log.e("udt", "md5 failed: " + e);

        return 0;

    }

}

From source file:Componentes.EncryptionMD5.java

public static String getStringMessageDigest(String message, String algorithm) {
    byte[] digest = null;
    byte[] buffer = message.getBytes();
    try {//from w  w  w. j av  a2s  .com
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        messageDigest.reset();
        messageDigest.update(buffer);
        digest = messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Error creando Digest");
    }
    return toHexadecimal(digest);
}

From source file:org.nuclos.common.CryptUtil.java

public static byte[] digestOf(byte[] content) {
    try {//from   ww w.j a va  2  s  . c o m
        final MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(content);
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new CommonFatalException(e.getMessage(), e);
    }
}

From source file:Main.java

public static String getMD5Str(String str) {
    MessageDigest messageDigest = null;
    try {//w w w  .  j a va2 s  .  c  om
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } 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:JavaCloud.Utils.java

public static String calcHash(String password, String seed) {
    try {/*from  ww w  .ja v a 2  s  .c o  m*/
        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();

        String tuple = password + seed;

        crypt.update(tuple.getBytes("UTF-8"));
        return new BigInteger(1, crypt.digest()).toString(16);
    } catch (Exception e) {
        return null;
    }
}

From source file:ca.ualberta.physics.cssdp.util.HashUtils.java

/**
 * From a password, a number of iterations and a salt, returns the
 * corresponding digest// ww  w  .j a v  a2  s . com
 * 
 * @param iterationNb
 *            int The number of iterations of the algorithm
 * @param password
 *            String The password to encrypt
 * @param salt
 *            byte[] The salt
 * @return byte[] The digested password
 * @throws NoSuchAlgorithmException
 *             If the algorithm doesn't exist
 */
public static byte[] getHash(int iterationNb, String password, byte[] salt) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(salt);
        byte[] input = digest.digest(password.getBytes("UTF-8"));
        for (int i = 0; i < iterationNb; i++) {
            digest.reset();
            input = digest.digest(input);
        }
        return input;
    } catch (NoSuchAlgorithmException nsa) {
        throw Throwables.propagate(nsa);
    } catch (UnsupportedEncodingException e) {
        throw Throwables.propagate(e);
    }
}