Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

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

Prototype

public byte[] digest() 

Source Link

Document

Completes the hash computation by performing final operations such as padding.

Usage

From source file:Main.java

public static String keyHash(Context context) {
    String key = "";
    try {/*from  ww w. j  a va 2s  .co  m*/
        PackageInfo info = context.getPackageManager().getPackageInfo("org.tathva.triloaded.anubhava",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            key = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d("anas", key);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    return key;
}

From source file:Main.java

private static String generateSHA256(String offThis) {
    String result = "";
    try {//from w w w  . j a v a  2 s  .  co  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:com.adaptris.core.mail.attachment.MailContent.java

private static String calculateHash(byte[] b) {
    String result = null;//ww w .j  a  v  a  2  s.c o m
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(b);
        byte[] hash = md.digest();
        result = Conversion.byteArrayToBase64String(hash);
    } catch (Exception e) {
        ;
    }
    return result;
}

From source file:com.beginner.core.utils.MD5.java

public static String md5(String str) {
    try {/*from  w w w .  j ava2 s. c om*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte b[] = md.digest();

        int i;

        StringBuffer 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));
        }
        str = buf.toString();
    } catch (Exception e) {
        e.printStackTrace();

    }
    return str;
}

From source file:Main.java

public static String hashKeyForDisk(String key) {
    String cacheKey;/* www .  j a v  a  2  s. c o  m*/
    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:Main.java

public static String signature(String source) {

    try {/*ww w  .j a v a2  s  . c  o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.reset();
        md.update(source.getBytes());

        byte[] mdbytes = md.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
            String hex = Integer.toHexString(0xff & mdbytes[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();

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

From source file:Main.java

public static void printHashKey(Context context) {
    try {//from   w w w . j a  v  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 md5(String input) {
    String result = input;//from w ww. jav a  2  s  . co m
    if (input != null) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            if ((result.length() % 2) != 0) {
                result = "0" + result;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static String str2md5(String str) {
    try {//from  ww w.j  a v  a 2  s .c o m
        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);
    }
}

From source file:MainClass.java

public static byte[] makeDigest(String user, String password, long t1, double q1)
        throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(user.getBytes());//from w  w w.  j  av  a  2  s  .co  m
    md.update(password.getBytes());
    md.update(makeBytes(t1, q1));
    return md.digest();
}