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 byte[] generateRequestToken(byte[] countryCode, byte[] phoneNumber)
        throws NoSuchAlgorithmException {

    String signature = "MIIDMjCCAvCgAwIBAgIETCU2pDALBgcqhkjOOAQDBQAwfDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFDASBgNVBAcTC1NhbnRhIENsYXJhMRYwFAYDVQQKEw1XaGF0c0FwcCBJbmMuMRQwEgYDVQQLEwtFbmdpbmVlcmluZzEUMBIGA1UEAxMLQnJpYW4gQWN0b24wHhcNMTAwNjI1MjMwNzE2WhcNNDQwMjE1MjMwNzE2WjB8MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEUMBIGA1UEBxMLU2FudGEgQ2xhcmExFjAUBgNVBAoTDVdoYXRzQXBwIEluYy4xFDASBgNVBAsTC0VuZ2luZWVyaW5nMRQwEgYDVQQDEwtCcmlhbiBBY3RvbjCCAbgwggEsBgcqhkjOOAQBMIIBHwKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kqA4GFAAKBgQDRGYtLgWh7zyRtQainJfCpiaUbzjJuhMgo4fVWZIvXHaSHBU1t5w//S0lDK2hiqkj8KpMWGywVov9eZxZy37V26dEqr/c2m5qZ0E+ynSu7sqUD7kGx/zeIcGT0H+KAVgkGNQCo5Uc0koLRWYHNtYoIvt5R3X6YZylbPftF/8ayWTALBgcqhkjOOAQDBQADLwAwLAIUAKYCp0d6z4QQdyN74JDfQ2WCyi8CFDUM4CaNB+ceVXdKtOrNTQcc0e+t";
    String classesMd5 = "P3b9TfNFCkkzPoVzZnm+BA=="; // 2.11.395 [*]

    byte[] key2 = Base64.decode(
            "/UIGKU1FVQa+ATM2A0za7G2KI9S/CwPYjgAbc67v7ep42eO/WeTLx1lb1cHwxpsEgF4+PmYpLd2YpGUdX/A2JQitsHzDwgcdBpUf7psX1BU=",
            Base64.DEFAULT);/*from   w  w w . jav a  2 s . c  o m*/

    byte[] data = concat(
            concat(Base64.decode(signature, Base64.DEFAULT), Base64.decode(classesMd5, Base64.DEFAULT)),
            phoneNumber);

    byte[] opad = new byte[64];
    byte[] ipad = new byte[64];
    for (int i = 0; i < opad.length; i++) {
        opad[i] = 0x5C;
        ipad[i] = 0x36;
    }

    for (int i = 0; i < 64; i++) {
        opad[i] = (byte) ((opad[i] & 0xFF) ^ (key2[i] & 0xFF));
        ipad[i] = (byte) ((ipad[i] & 0xFF) ^ (key2[i] & 0xFF));
    }

    data = concat(ipad, data);

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(data);
    byte[] firstHash = md.digest();

    data = concat(opad, firstHash);
    md = MessageDigest.getInstance("SHA-1");
    md.update(data);
    byte[] secondHash = md.digest();

    return Base64.encode(secondHash, Base64.DEFAULT | Base64.NO_WRAP);
}

From source file:Main.java

public static String generateMD5Hash(String textToBeHashed) {
    try {// ww  w  .j  a  v a 2s  .  c o m
        MessageDigest messageDigest = MessageDigest.getInstance(MD5);
        messageDigest.update(textToBeHashed.getBytes());
        byte[] messageDigestByte = messageDigest.digest();
        StringBuffer MD5Hash = new StringBuffer();
        String h;
        for (int i = 0; i < messageDigestByte.length; ++i) {
            h = Integer.toHexString((0xFF & messageDigestByte[i]) | 0x100).substring(1, 3);
            MD5Hash.append(h);
        }
        return MD5Hash.toString();
    } catch (Exception e) {
        throw new RuntimeException("Couldn't generate MD5 hash for " + textToBeHashed);
    }
}

From source file:Main.java

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

From source file:com.ery.ertc.estorm.util.MD5Hash.java

/**
 * Given a byte array, returns its MD5 hash as a hex string. Only "length" number of bytes starting at "offset" within the byte array
 * are used.// w w  w.ja v  a2  s.c  o  m
 * 
 * @param key
 *            the key to hash (variable length byte array)
 * @param offset
 * @param length
 * @return MD5 hash as a 32 character hex string.
 */
public static String getMD5AsHex(byte[] key, int offset, int length) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(key, offset, length);
        byte[] digest = md.digest();
        return new String(Hex.encodeHex(digest));
    } catch (NoSuchAlgorithmException e) {
        // this should never happen unless the JDK is messed up.
        throw new RuntimeException("Error computing MD5 hash", e);
    }
}

From source file:Main.java

public static String getMD5Hash(String s) throws NoSuchAlgorithmException {
    MessageDigest m = MessageDigest.getInstance("MD5");
    byte[] data = s.getBytes();
    m.update(data, 0, data.length);/*w ww. jav a 2s  . com*/
    BigInteger i = new BigInteger(1, m.digest());
    return String.format("%1$032X", i);
}

From source file:Main.java

public static String md5(final String string) {
    try {/*w  w w  .j  ava  2s.  c  om*/
        // 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 "";
}

From source file:Main.java

public static String hashMD5Generate(String s) {
    try {//w  ww .  j  a va  2 s  .c o  m
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        Log.d(DEBUG, hexString.toString());
        return hexString.toString();

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

From source file:Main.java

public static String computeMD5(byte[] input) {
    try {//from  www . j av a  2  s  .  c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input, TYPE_NOT_CONNECTED, input.length);
        byte[] md5bytes = md.digest();
        StringBuffer hexString = new StringBuffer();
        for (int i = TYPE_NOT_CONNECTED; i < md5bytes.length; i += TYPE_WIFI) {
            String hex = Integer.toHexString(md5bytes[i] & MotionEventCompat.ACTION_MASK);
            if (hex.length() == TYPE_WIFI) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:D_common.E_ncript.java

public static String getMd5(String s) {
    try {//from   w  ww .j a v  a 2s .c om
        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 String MD5(String source) {
    String resultHash = null;/*from  ww w.j  ava 2s  .  c om*/
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        md5.update(source.getBytes("UTF-8"));
        byte[] result = md5.digest();
        StringBuffer buf = new StringBuffer(result.length * 2);
        for (int i = 0; i < result.length; i++) {
            int intVal = result[i] & 0xff;
            if (intVal < 0x10) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(intVal));
        }
        resultHash = buf.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultHash.toString();
}