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 getSHA256(String mykey) {
    try {/*from  ww w  .j av  a2  s  . c o  m*/
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA256");
        digest.update(mykey.getBytes(Charset.forName("UTF-8")));
        return bytesToHex(digest.digest()).toLowerCase();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "SHA hash exception: " + e.toString());
        return null;
    }
}

From source file:Main.java

public static String getMd5Digest(String pInput) {
    try {//  w w w .jav a2  s  .co 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

/**
 * A double check about app signature that was passed by MainActivity as facetID.
 * @param facetId a string value composed by app hash. I.e. android:apk-key-hash:Lir5oIjf552K/XN4bTul0VS3GfM
 * @param context Application Context//from w  ww.j a  v  a2  s. c o  m
 * @return true if the signature executed on runtime matches if signature sent by MainActivity
 */
private static boolean checkAppSignature(String facetId, Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature sign : packageInfo.signatures) {
            byte[] sB = sign.toByteArray();
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.update(sign.toByteArray());
            String currentSignature = Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT);
            if (currentSignature.toLowerCase().contains(facetId.split(":")[2].toLowerCase())) {
                return true;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static String hexdigest(byte[] paramArrayOfByte) {
    final char[] hexDigits = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
    try {/*  ww w . j  av  a 2  s .c  om*/
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.update(paramArrayOfByte);
        byte[] arrayOfByte = localMessageDigest.digest();
        char[] arrayOfChar = new char[32];
        for (int i = 0, j = 0;; i++, j++) {
            if (i >= 16) {
                return new String(arrayOfChar);
            }
            int k = arrayOfByte[i];
            arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
            arrayOfChar[++j] = hexDigits[(k & 0xF)];
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String SHA1(String text) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(text.getBytes());

    byte byteData[] = md.digest();

    //convert the byte to hex format method 1
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }//  w w  w  .j ava 2 s .c  om

    //System.out.println("Hex format : " + sb.toString());

    //convert the byte to hex format method 2
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        String hex = Integer.toHexString(0xff & byteData[i]);
        if (hex.length() == 1)
            hexString.append('0');
        hexString.append(hex);
    }
    return hexString.toString();
}

From source file:Main.java

/**
 * Gets the key hash of application package's certificate signature.
 * @since   0.1.1//  w ww. j a v  a 2s  . c o  m
 * @param   aContext The context from which the package manager is retrieved to get the signature's information.
 * @return   The list of signatures embedded to the application package.
 */
public static List<String> getKeyHash(Context aContext) {
    try {
        PackageInfo info = aContext.getPackageManager().getPackageInfo(getPackageName(aContext),
                PackageManager.GET_SIGNATURES);

        List<String> keyHashList = new ArrayList<String>(info.signatures.length);

        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            keyHashList.add(Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
        return keyHashList;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static String hexdigest(byte[] paramArrayOfByte) {
    final char[] hexDigits = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
    try {/*from ww  w.  j  a  v a2  s.c  o m*/
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.update(paramArrayOfByte);
        byte[] arrayOfByte = localMessageDigest.digest();
        char[] arrayOfChar = new char[32];
        for (int i = 0, j = 0;; i++, j++) {
            if (i >= 16) {
                return new String(arrayOfChar);
            }
            int k = arrayOfByte[i];
            arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
            arrayOfChar[++j] = hexDigits[(k & 0xF)];
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

private static String generateSHA256(String offThis) {
    String result = "";
    try {/*from w  w w  .ja  va  2s  .  c o m*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(offThis.getBytes());
        byte mdBytes[] = md.digest();
        result = bytesToHex(mdBytes);
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String getMD5Str(String password) {
    String strResult = "";
    MessageDigest md5;
    try {/*w  ww  . ja v  a 2s . com*/
        md5 = MessageDigest.getInstance("MD5");
        md5.update(password.getBytes("UTF-8"));
        byte[] bzpassword_1 = md5.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bzpassword_1.length; ++i) {
            sb.append(String.format("%02x", bzpassword_1[i]));
        }
        md5.update(sb.toString().getBytes("UTF-8"));
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String str2md5(String str) {
    try {//from   ww  w . j  ava  2s  .  c om
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(str.getBytes());
        byte[] bytes = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            hexString.append(HEX_DIGITS[b >> 4 & 0xf]);
            hexString.append(HEX_DIGITS[b & 0xf]);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}