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(String str) {
    if (TextUtils.isEmpty(str)) {
        return null;
    }//from w w w .j a v a 2 s. c om
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(str.getBytes());
        return encodeHex(messageDigest.digest());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static final String md5(final String s) {
    try {//from w ww. ja v  a2s .  c o m
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.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

private static String md5(String input) {
    final String MD5 = "MD5";
    try {//ww  w. jav  a2 s  .  c  om
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
        digest.update(input.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

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

From source file:Main.java

/**
 * Get current certificate fingerprint//from   w w  w .ja v a  2  s  .  c o  m
 *
 * @param ctx         context of application
 * @param packageName your package name
 * @return Base64 packed SHA fingerprint of your packet certificate
 */
public static String[] getCertificateFingerprint(Context ctx, String packageName) {
    try {
        if (ctx == null || ctx.getPackageManager() == null)
            return null;
        PackageInfo info = ctx.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        assert info.signatures != null;
        String[] result = new String[info.signatures.length];
        int i = 0;
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            //                result[i++] = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            result[i++] = toHex(md.digest());
        }
        return result;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String md5(String s) {
    try {/*from w  w  w  .  jav  a 2  s. c  o  m*/
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.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

/**
 * Get current certificate fingerprint// www .  j  a  va  2 s.  c o m
 *
 * @param ctx         context of application
 * @param packageName your package name
 * @return Base64 packed SHA fingerprint of your packet certificate
 */
public static String[] getCertificateFingerprint(Context ctx, String packageName) {
    try {
        if (ctx == null || ctx.getPackageManager() == null)
            return null;
        @SuppressLint("PackageManagerGetSignatures")
        PackageInfo info = ctx.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        assert info.signatures != null;
        String[] result = new String[info.signatures.length];
        int i = 0;
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            result[i++] = toHex(md.digest());
        }
        return result;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * encode By MD5//from   w ww  .jav  a2  s .  co m
 * 
 * @param str
 * @return String
 */
public static String md5(String str) {
    if (str == null) {
        return null;
    }
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(str.getBytes());
        return new String(encodeHex(messageDigest.digest()));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:helper.Digester.java

/**
 * @param str the input string/*ww w. j a  va 2 s.co m*/
 * @return the digested string
 * @throws NoSuchAlgorithmException encryption algorithm not installed 
 */
public static String digest(String str) throws NoSuchAlgorithmException {
    Configuration config = Play.application().configuration();

    String salt = config.getString("application.secret");

    String saltedStr = str + salt;

    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(saltedStr.getBytes());

    byte byteData[] = md.digest();

    byte[] base64 = Base64.encodeBase64(byteData);

    String result = new String(base64, Charset.defaultCharset());

    return result;
}

From source file:Main.java

public static String md5Encoder(String content, boolean isCapital) {
    try {/*from ww  w. j  av a2s.c  o m*/
        byte[] strTemp = content.getBytes();
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        byte[] mds = mdTemp.digest();
        int j = mds.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (byte md : mds) {
            if (isCapital) {
                str[k++] = CAPITAL_HEX_DIGITS[md >> 4 & 0xf];
                str[k++] = CAPITAL_HEX_DIGITS[md & 0xf];
            } else {
                str[k++] = LOWER_HEX_DIGITS[md >> 4 & 0xf];
                str[k++] = LOWER_HEX_DIGITS[md & 0xf];
            }
        }
        return new String(str);
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static String MD5(String source) {
    String resultHash = null;//from   ww w  .  j  a va  2  s  .co  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();
}