Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

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

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:Main.java

public static String getCallSignature(String subAccountSid, String subAccountToken) {
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    String timestamp = format.format(new Date());
    String signature = subAccountSid + subAccountToken + timestamp;
    try {/*from w  w  w . j a  v  a  2 s . co  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        return byte2HexStr(md.digest(signature.getBytes("utf-8")));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * Encrypt the plain text with MD5 algorithm.
 *
 * @param plainText The plain text.//from  www. ja va  2 s. c  o m
 * @return The Encrypt content.
 */
public static String md5Encrypt(String plainText) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(plainText.getBytes(Charset.defaultCharset()));
        return new String(toHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:D_common.E_ncript.java

public static String getMd5(String s) {
    try {//from   w  ww  .ja  v  a2s.co m
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(), 0, s.length());
        return new BigInteger(1, m.digest()).toString(16);
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static void printHashKey(Context context) {
    try {/* w ww. ja  v a2  s  .  c om*/
        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

/**
 * Returns SHA256 hash of the public key of a given certificate.
 *
 * @param cert the cert that should be used to retrieve the public key from.
 * @return SHA256 hash of the public key.
 */// w  ww . j  a  v a2s.  co  m
public static byte[] getPublicKeySha256(Certificate cert) {
    try {
        byte[] publicKey = cert.getPublicKey().getEncoded();
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return digest.digest(publicKey);
    } catch (NoSuchAlgorithmException ex) {
        // This exception should never happen since SHA-256 is known algorithm
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String computeMD5(byte[] input) {
    try {/*  w  ww  .  ja  v  a 2s.  com*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input, 0, input.length);
        byte[] md5bytes = md.digest();

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

From source file:Main.java

/**
 * Computes the SHA1 Hash of given UTF-8 data.
 *
 * @param message/*from ww  w . j  a  v a  2s  .  co m*/
 * @return
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 */
public static String SHA1(String message) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    byte[] data = message.getBytes("UTF-8");
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.update(data, 0, data.length);
    byte[] digest = md.digest();
    return bytesToHex(digest);
}

From source file:Main.java

public static byte[] getDigestFromURL(URL u) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    InputStream in = u.openStream();
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
        md5.update(data, 0, bytesRead);/*ww w  .  j ava  2 s . co m*/
    }
    return md5.digest();
}

From source file:Main.java

private static String digest(final String value) {
    final byte[] digested;
    try {/*  ww  w.ja  va  2s.  co m*/
        digested = MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes(CHARSET));
    } catch (final NoSuchAlgorithmException e) {
        return null;
    } catch (final UnsupportedEncodingException e) {
        return null;
    }

    final String hashed = new BigInteger(1, digested).toString(16);
    final int padding = HASH_LENGTH - hashed.length();
    if (padding == 0) {
        return hashed;
    }

    final char[] zeros = new char[padding];
    Arrays.fill(zeros, '0');
    return new StringBuilder(HASH_LENGTH).append(zeros).append(hashed).toString();
}

From source file:Main.java

public static String md5Three(String clientId, String pwd, String timestamp) {
    clientId = clientId == null ? "" : clientId;
    pwd = pwd == null ? "" : pwd;
    timestamp = timestamp == null ? "" : timestamp;
    while (timestamp.length() < 10) {
        timestamp = "0" + timestamp;
    }//from   w  w  w .  ja va2  s .c o m
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    md.update(clientId.getBytes());
    md.update(new byte[7]);
    md.update(pwd.getBytes());
    md.update(timestamp.getBytes());

    return byteArrayToHexString(md.digest());
}