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 md5(final String s) {
    try {//from   w  w w .  jav a 2 s.  c o  m
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        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) {
    }
    return "";
}

From source file:Main.java

public static String md5(String str) {
    try {//w  ww  .jav a 2  s .  c  om
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());

        byte[] b = md.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            int v = (int) b[i];
            v = v < 0 ? 0x100 + v : v;
            String cc = Integer.toHexString(v);
            if (cc.length() == 1)
                sb.append('0');
            sb.append(cc);
        }

        return sb.toString();
    } catch (Exception e) {
    }
    return "";
}

From source file:Main.java

public static final String encryptMd5String(String s) {
    try {//ww  w . j ava  2  s. c  o m
        byte[] btInput = s.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < md.length; i++) {
            int val = ((int) md[i]) & 0xff;
            if (val < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(val));
        }
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String hashImgUrl(String imgUrl) throws NoSuchAlgorithmException {
    String imgKey = null;/*  ww w .ja  v a  2 s .  c  o  m*/
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.reset();
    m.update(imgUrl.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    imgKey = bigInt.toString(16);
    while (imgKey.length() < 32)
        imgKey = "0" + imgKey;
    return imgKey;
}

From source file:Main.java

public static void printHashKey(Context context) {
    try {/*from w  w  w  .jav  a  2  s . co m*/
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("HASH KEY:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

From source file:Main.java

public static String getMd5Digest(String pInput) {
    if (pInput == null)
        return "";

    try {/* w ww. j a  v a  2 s.  c  o m*/
        MessageDigest lDigest = MessageDigest.getInstance("MD5");
        lDigest.update(getBytes(pInput));
        BigInteger lHashInt = new BigInteger(1, lDigest.digest());
        return String.format("%1$032X", lHashInt);
    } catch (NoSuchAlgorithmException lException) {
        throw new RuntimeException(lException);
    }
}

From source file:Main.java

public static String MD5Crypto(String str) {
    try {/* ww  w  . j  a v a 2 s .  c  o  m*/
        // Create MD5 Hash
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(str.getBytes());
        byte messageDigest[] = digest.digest();
        return toHexString(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

public final static String getMessageDigest(byte[] buffer) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    try {//from   w  w  w  .j  av a  2 s  .c o m
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(buffer);
        byte[] md = mdTemp.digest();
        int j = md.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String sha1(String string) {
    byte[] hash;//from  w w  w  . j av  a 2s .c o  m
    try {
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        sha1.update(string.getBytes("UTF-8"));
        hash = sha1.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, SHA-1 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, SHA-1 UTF-8 should be supported?", e);
    }
    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

private static StringBuffer getMd5Buffer(String text) {
    StringBuffer buf = new StringBuffer();
    try {/*w w  w.  jav  a2s. c  o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(text.getBytes());
        byte[] b = md.digest();
        int i;
        buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return buf;
}