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

public static String MD5(String source) {
    String resultHash = null;//from www  .ja  v a 2 s .c  o  m
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        md5.update(source.getBytes("UTF-8"));
        byte[] result = md5.digest();
        StringBuffer buf = new StringBuffer(result.length * 2);
        for (int i = 0; i < result.length; i++) {
            int intVal = result[i] & 0xff;
            if (intVal < 0x10) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(intVal));
        }
        resultHash = buf.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultHash.toString();
}

From source file:Main.java

public static String str2md5(String str) {
    try {/*  w w w .  ja  va 2 s .c  o m*/
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(str.getBytes());
        byte[] bytes = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            hexString.append(HEX_DIGITS[b >> 4 & 0xf]);
            hexString.append(HEX_DIGITS[b & 0xf]);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String md5(String input) {
    String res = "";
    try {/*w  w  w. ja v a  2s  . co  m*/
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        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) {
                res += "0" + tmp;
            } else {
                res += tmp;
            }
        }
    } catch (NoSuchAlgorithmException ex) {
    }
    return res;
}

From source file:com.playonlinux.utils.Checksum.java

private static byte[] getDigest(InputStream inputStream, MessageDigest messageDigest) throws IOException {

    messageDigest.reset();
    byte[] bytes = new byte[BLOCK_SIZE];
    int numBytes;
    while ((numBytes = inputStream.read(bytes)) != -1) {
        messageDigest.update(bytes, 0, numBytes);
    }// ww  w.ja  v  a  2 s .c o m
    return messageDigest.digest();
}

From source file:Main.java

public static String signature(String source) {

    try {/*from   ww  w.  jav  a 2  s . co m*/
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.reset();
        md.update(source.getBytes());

        byte[] mdbytes = md.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
            String hex = Integer.toHexString(0xff & mdbytes[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();

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

From source file:org.bimserver.utils.Hashers.java

public static String getSha256Hash(String password) {
    try {/*from  www  .j  a  v a  2  s .  c  om*/
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.reset();
        return new String(Hex.encodeHex(digest.digest(password.getBytes(Charsets.UTF_8))));
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("", e);
    }
    return null;
}

From source file:Main.java

public static String md5(String input) {
    String res = "";

    try {/*ww  w . ja  va 2 s.c  o  m*/
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        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) {
                res += "0" + tmp;
            } else {
                res += tmp;
            }
        }
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }

    return res;
}

From source file:HashUtil.java

public static String getMD5(final String data) {
    try {//  www. j  a  v a 2  s .c om
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(data.getBytes());
        BigInteger bigInt = new BigInteger(1, m.digest());
        String hashtext = bigInt.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return e.getMessage();
    }
}

From source file:Main.java

public static String MD5(String paramString) {
    if (paramString == null)
        return null;
    try {/*from   w w  w  .  j  ava 2s  .  c o m*/
        byte[] arrayOfByte1 = paramString.getBytes();
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.reset();
        localMessageDigest.update(arrayOfByte1);
        byte[] arrayOfByte2 = localMessageDigest.digest();
        StringBuffer localStringBuffer = new StringBuffer();
        for (int i = 0; i < arrayOfByte2.length; i++) {
            Object[] arrayOfObject = new Object[1];
            arrayOfObject[0] = Byte.valueOf(arrayOfByte2[i]);
            localStringBuffer.append(String.format("%02X", arrayOfObject));
        }
        String str = localStringBuffer.toString();
        return str;
    } catch (Exception localException) {
    }
    return paramString.replaceAll("[^[a-z][A-Z][0-9][.][_]]", "");
}

From source file:com.apexxs.neonblack.utilities.MD5Utilities.java

public static String getMD5Hash(String s) {
    String md5Hash = StringUtils.EMPTY;
    try {/*w w w. j a  v  a2  s  .  c  o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        byte[] bytes = md.digest(s.getBytes("UTF-8"));
        md5Hash = String.format("%032x", new BigInteger(1, bytes));
    } catch (Exception ex) {
        logger.error("Error generating MD5 hash from " + s + "\n" + ex.getMessage());
    }
    return md5Hash;
}