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 void printOutMyHashKey(Context context, String packageName) {
    try {/*  w  ww . j  av  a  2  s. c  o m*/
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName,
                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) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Get current certificate fingerprint//from   w  ww. 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;
        @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

public static String getPwdHash(String password, String salt) {
    try {/*ww  w  .  j  ava  2s . c  o m*/
        String temp = password + salt;
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        md.update(temp.getBytes("UTF-8")); // Change this to "UTF-16" if needed
        byte[] digest = md.digest();
        return String.format("%0" + (digest.length * 2) + 'x', new BigInteger(1, digest));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.netflix.exhibitor.core.s3.S3Utils.java

public static byte[] md5(byte[] buffer, int length) {
    try {//from   w w  w.  jav a2  s .c o m
        MessageDigest mdigest = MessageDigest.getInstance("MD5");
        mdigest.update(buffer, 0, length);
        return mdigest.digest();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Sha1.java

public static String sha1(String text) {
    try {// ww  w  .ja  va2s.co  m
        MessageDigest md = MessageDigest.getInstance("SHA-1");

        md.update(text.getBytes("iso-8859-1"), 0, text.length());

        return convertToHex(md.digest());
    } catch (Exception e) {
        throw new Exception(format("Problem hashing %s", text), e);
    }
}

From source file:Main.java

private static String getCertificateSHA1(X509Certificate certificate)
        throws NoSuchAlgorithmException, CertificateEncodingException {
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
    byte[] der = certificate.getEncoded();
    messageDigest.update(der);/*w ww . j  a v  a 2  s. c om*/
    byte[] digest = messageDigest.digest();
    return hexify(digest);
}

From source file:Main.java

private static String md5(String origin) {
    try {/*from  w w w . java 2 s .c  o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(origin.getBytes("UTF-8"));
        BigInteger bi = new BigInteger(1, md.digest());

        return bi.toString(16);
    } catch (Exception e) {
        return getUuid();
    }
}

From source file:Main.java

public static String hashKey(String key) {
    String cacheKey;//from  w ww.  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:cn.edu.bit.whitesail.utils.MD5Signature.java

public static String calculate(byte[] byteArray) {
    StringBuffer result = null;/*from ww w. j a va2 s.  c  o  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

/**
 * @param s String to MD5 hash//www.j av a2  s .  co m
 * @return MD5 hashed string
 */
public static String md5(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}