Example usage for java.security MessageDigest update

List of usage examples for java.security MessageDigest update

Introduction

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

Prototype

public final void update(ByteBuffer input) 

Source Link

Document

Update the digest using the specified ByteBuffer.

Usage

From source file:Main.java

public static String getMD5String(String str) {
    MessageDigest messageDigest = null;
    try {//from   w  w w .  ja  va2 s.com
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (Exception e) {
        return null;
    }

    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (byte b : byteArray) {
        if ((0xFF & b) < 0x10)
            md5StrBuff.append("0");
        md5StrBuff.append(Integer.toHexString(0xFF & b));
    }
    return md5StrBuff.toString();
}

From source file:club.jmint.crossing.specs.Security.java

public static String MD5(String str) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    try {/*from  www .ja  va2  s  . c  o  m*/
        byte[] btInput = str.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();

        int j = md.length;
        char mdstr[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            mdstr[k++] = hexDigits[byte0 >>> 4 & 0xf];
            mdstr[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(mdstr);
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        return null;
    }
}

From source file:com.yilang.commons.utils.util.Digests.java

private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {/*ww  w .  ja v a  2 s  .c om*/
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:com.google.api.ads.adwords.awreporting.model.util.UrlHashUtil.java

/**
 * Creates a SHA-1 Hash of the url/*from ww  w  . jav a2s . c om*/
 * 
 * @param url the url that needs to be hashed
 * @return a Stri g with a SHA-1 hash of the URL
 */
public static String createUrlHash(String url) {
    String hash = null;
    MessageDigest messageDigest;
    try {
        messageDigest = MessageDigest.getInstance("SHA-1");
        messageDigest.reset();
        messageDigest.update(url.getBytes("UTF-8"));
        final byte[] resultByte = messageDigest.digest();
        hash = new String(Hex.encodeHex(resultByte));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return hash;
}

From source file:corelyzer.util.StringUtility.java

public static String getSHASum(final String aString) {
    String hash;/*from w ww .j  a  va 2s .com*/

    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(aString.getBytes());

        Hex hex = new Hex();
        hash = new String(hex.encode(md.digest()));
    } catch (NoSuchAlgorithmException e) {
        System.err.println("---> [Error] NoSuchAlgorithmException!");
        hash = "no-sha";
    }

    return hash;
}

From source file:me.figo.internal.FigoTrustManager.java

private static String getThumbPrint(X509Certificate cert) {
    try {/* ww  w.j ava2  s.com*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] der = cert.getEncoded();
        md.update(der);
        byte[] digest = md.digest();
        return new String(Hex.encodeHex(digest, false));
    } catch (NoSuchAlgorithmException e) {
        return "";
    } catch (CertificateEncodingException e) {
        return "";
    }
}

From source file:Main.java

public static String getStringMD5(String key) {
    MessageDigest md5 = null;
    try {//from  w ww .  ja  v  a2s . c o m
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    md5.update(key.getBytes());
    //important: use Base64.URL_SAFE flag to avoid "+" and "/"
    return new String(Base64.encode(md5.digest(), Base64.URL_SAFE));
}

From source file:gov.nasa.jpl.analytics.util.CommonUtil.java

public static String hashString(String str) {
    String hash = "";
    try {//from  w w  w. j av a2s . c  o  m
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(str.getBytes("UTF-8"));
        byte[] digest = md.digest();
        hash = String.format("%064x", new java.math.BigInteger(1, digest)).toUpperCase();
    } catch (NoSuchAlgorithmException e) {
        LOG.error("Not a valid Hash Algorithm for String " + str);
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        LOG.error("Not a valid Encoding for String " + str);
        e.printStackTrace();
    }
    return hash;
}

From source file:com.moki.touch.util.UrlUtil.java

/**
 * This method is used to create a filename for cached content
 * @param url the url of the content to be cached
 * @return an MD5 hashed string/*ww  w  .  ja v  a 2s.  co m*/
 */
public static String hashUrl(String url) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(url.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

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

From source file:Main.java

public static String encode(String input) {
    try {//from  w  w  w . j  a v  a  2 s  .co  m
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] inputByteArray = input.getBytes();
        messageDigest.update(inputByteArray);
        byte[] resultByteArray = messageDigest.digest();
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] resultCharArray = new char[resultByteArray.length * 2];
        int index = 0;
        for (byte b : resultByteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        return new String(resultCharArray);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}