Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

In this page you can find the example usage for java.security MessageDigest digest.

Prototype

public byte[] digest() 

Source Link

Document

Completes the hash computation by performing final operations such as padding.

Usage

From source file:Main.java

public static boolean verifyChecksum(byte[] bytesWithChecksumm) {
    try {//w  w  w  . j  a  v a  2s. c  om
        if (bytesWithChecksumm == null || bytesWithChecksumm.length < 5) {
            return false;
        }
        MessageDigest digestSha = MessageDigest.getInstance("SHA-256");
        digestSha.update(bytesWithChecksumm, 0, bytesWithChecksumm.length - 4);
        byte[] first = digestSha.digest();
        byte[] calculatedDigest = digestSha.digest(first);
        boolean checksumValid = true;
        for (int i = 0; i < 4; i++) {
            if (calculatedDigest[i] != bytesWithChecksumm[bytesWithChecksumm.length - 4 + i]) {
                checksumValid = false;
            }
        }
        return checksumValid;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getBytesDigestEncrypt(byte[] bytes, String algorithm) {
    if (algorithm.equals(MD5) || algorithm.equals(SHA1) || algorithm.equals(SHA256) || algorithm.equals(SHA384)
            || algorithm.equals(SHA512)) {
        MessageDigest digest = null;
        try {/* ww w  .  j  av  a2s  .  com*/
            digest = MessageDigest.getInstance(algorithm);
            digest.update(bytes);
            return bytes2String(digest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static byte[] computeSHA1(ByteBuffer convertme, int offset, int len) {
    int oldp = convertme.position();
    int oldl = convertme.limit();
    try {//  w ww.j  a  v a2s  .co  m
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        convertme.position(offset);
        convertme.limit(len);
        md.update(convertme);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        convertme.limit(oldl);
        convertme.position(oldp);
    }
    return new byte[20];
}

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

private static String hashPassword(String password) {
    try {/*from w  ww . ja  v  a2  s  . com*/
        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:com.buddycloud.friendfinder.HashUtils.java

public static String encodeSHA256(String provider, String contactId)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update((provider + ":" + contactId).getBytes("UTF-8"));
    byte[] digest = md.digest();
    return Base64.encodeBase64String(digest);
}

From source file:com.sammyun.util.MD5.java

/**
 * @param str// w w w.ja  v  a2  s.c  o  m
 * @return
 * @throws NoSuchAlgorithmException
 */
public static String crypt(String str) throws NoSuchAlgorithmException {
    if (str == null || str.length() == 0) {
        throw new IllegalArgumentException("String to encript cannot be null or zero length");
    }

    StringBuffer hexString = new StringBuffer();

    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(str.getBytes());
    byte[] hash = md.digest();

    for (byte aHash : hash) {
        if ((OXFF & aHash) < OX10) {
            hexString.append("0").append(Integer.toHexString((OXFF & aHash)));
        } else {
            hexString.append(Integer.toHexString(OXFF & aHash));
        }
    }

    return hexString.toString();
}

From source file:Main.java

/**
 * md5//from w  ww  . j av  a  2s .com
 * 
 * @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:Main.java

public static String MD5(String paramString) {
    if (paramString == null)
        return null;
    try {/*from  w ww .j  a  v  a  2 s  .  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:Main.java

private static byte[] computeHash(String x) {
    java.security.MessageDigest d = null;
    try {//from   w w w .j a va  2  s .  c  o m
        d = java.security.MessageDigest.getInstance("SHA-1");
        d.reset();
        d.update(x.getBytes());
        return d.digest();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static boolean verifyChecksum(byte[] bytesWithChecksumm) {
    try {/*from  w ww . j a  v  a2s . c  om*/
        if (bytesWithChecksumm == null || bytesWithChecksumm.length < 5) {
            return false;
        }
        MessageDigest digestSha = MessageDigest.getInstance("SHA-256");
        digestSha.update(bytesWithChecksumm, 0, bytesWithChecksumm.length - 4);
        byte[] first = digestSha.digest();
        byte[] calculatedDigest = digestSha.digest(first);
        boolean checksumValid = true;
        for (int i = 0; i < 4; i++) {
            if (calculatedDigest[i] != bytesWithChecksumm[bytesWithChecksumm.length - 4 + i]) {
                checksumValid = false;
            }
        }
        return checksumValid;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}