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

/**
 * Generate the SHA1 hash of a message /*from   w  ww . j  a  va2  s . co m*/
 * @param message
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
public static byte[] SHA1(byte[] message) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md;
    md = MessageDigest.getInstance("SHA-1");
    md.update(message);
    return md.digest();
}

From source file:Main.java

public static String md5L(String input) {
    try {/*  w w  w  . j a  v  a2 s . c o  m*/
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(input.getBytes());
        byte[] md = mdInst.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte aMd : md) {
            String shaHex = Integer.toHexString(aMd & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static Bitmap createIdenticon(String data) {
    try {/*w w  w  . j a  va 2s  . com*/
        MessageDigest dig = MessageDigest.getInstance("MD5");
        dig.update(data.getBytes());
        return createIdenticon(dig.digest());
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:Main.java

public static String MD5(final String s) {
    final String MD5 = "MD5";
    try {//from   w w w .j  a  va2  s  .  c om
        MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        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 "";
}

From source file:Main.java

public static void printKeyHash(Activity pActivity) {
    // Add code to print out the key hash
    try {/*  w ww. ja  v  a2 s  .c o  m*/
        PackageInfo info = pActivity.getPackageManager().getPackageInfo(pActivity.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("KeyHash:", e.toString());
    } catch (NoSuchAlgorithmException e) {
        Log.d("KeyHash:", e.toString());
    }
}

From source file:Main.java

/**
 * Prints your current certificate signature to the Logcat. Use this method to obtain your certificate signature.
 *
 * @param context The application context.
 *//*ww  w . j ava  2  s  . co  m*/

public static void getCertificateSignature(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);

        // The APK is signed with multiple signatures, probably it was tampered.
        if (packageInfo.signatures.length > 1) {
            return;
        }

        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");

            md.update(signature.toByteArray());

            Log.d("TAMPERING_PROTECTION", "**" + Base64.encodeToString(md.digest(), Base64.DEFAULT) + "**");
        }
    } catch (Exception exception) {
        Log.d("TAMPERING_PROTECTION", exception.getStackTrace().toString());
    }
}

From source file:Main.java

private static final String md5(final String s) {
    final String MD5 = "MD5";
    try {/*from   w  ww .ja va2s  .c  om*/
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
        digest.update(s.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 "";
}

From source file:Main.java

public static String hashKeyForDisk(String key) {
    String cacheKey;//from   www  .  j  a  v a  2 s . c om
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(key.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}

From source file:cn.edu.bit.whitesail.utils.MD5Signature.java

public static String calculate(byte[] byteArray) {
    StringBuffer result = null;/* w  w  w  .  j av  a  2s  . co  m*/
    if (byteArray == null)
        return null;
    try {
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(byteArray);
        result = new StringBuffer(new BigInteger(1, m.digest()).toString(16));
        for (int i = 0; i < 32 - result.length(); i++)
            result.insert(0, '0');
    } catch (NoSuchAlgorithmException ex) {
        LOG.fatal("MD5 Hashing Failed,System is going down");
        System.exit(1);
    }
    return result.toString();
}

From source file:Main.java

public static String md5(final String string) {
    try {/*from w w  w  .  jav  a  2 s  .  c  o m*/
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(string.getBytes());
        byte messageDigest[] = digest.digest();

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

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