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 hexdigest(byte[] paramArrayOfByte) {
    final char[] hexDigits = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
    try {/*  w ww . j ava 2 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

public static String getSHA1Digest(final String pInput) {
    if (pInput != null) {
        try {//from w ww  . j a  va  2  s  .  c  o  m
            final MessageDigest lDigest = MessageDigest.getInstance("SHA1");
            lDigest.update(getBytes(pInput));
            final BigInteger lHashInt = new BigInteger(1, lDigest.digest());
            return String.format("%1$032X", lHashInt);
        } catch (final NoSuchAlgorithmException lException) {
            return pInput;
        }
    }
    return null;
}

From source file:baldrickv.s3streamingtool.Hash.java

public static String hash(String algo, int output_bits, byte b[], int offset, int size) {
    try {// w w w  .  j a v a  2 s .c  om
        int output_bytes = output_bits / 4; //hex = 4 bits per byte
        MessageDigest sig = MessageDigest.getInstance(algo);
        sig.update(b, offset, size);
        byte d[] = sig.digest();

        StringBuffer s = new StringBuffer(output_bytes);
        BigInteger bi = new BigInteger(1, d);
        s.append(bi.toString(16));
        while (s.length() < output_bytes) {
            s.insert(0, '0');
        }
        return s.toString();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
        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 w  w  w .j  a  v  a  2 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

public static String getMd5Digest(final String pInput) {
    if (pInput != null) {
        try {//from w w w.j a v a2  s  .  c o  m
            final MessageDigest lDigest = MessageDigest.getInstance("MD5");
            lDigest.update(getBytes(pInput));
            final BigInteger lHashInt = new BigInteger(1, lDigest.digest());
            return String.format("%1$032X", lHashInt);
        } catch (final NoSuchAlgorithmException lException) {
            return pInput;
        }
    }
    return null;

}

From source file:apidemo.APIDemo.java

public static String md5String(String str) {
    try {//from   w  ww  . ja  v a2 s . c  o m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.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));
        }

        return sb.toString();
    } catch (Exception ex) {
        return "";
    }
}

From source file:Main.java

public static void printKeyHash(Activity pActivity) {
    // Add code to print out the key hash
    try {/* w w  w .ja v a2  s.  c  om*/
        PackageInfo info = pActivity.getPackageManager().getPackageInfo(pActivity.getPackageName(),
                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) {
        Log.d("KeyHash:", e.toString());
    } catch (NoSuchAlgorithmException e) {
        Log.d("KeyHash:", e.toString());
    }
}

From source file:Main.java

public static String createPubNubSafeBase64Hash(String input) {
    try {/*w w w.  j av  a 2 s  .c o  m*/
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(input.getBytes());
        String encodedChannelName = Base64.encodeToString(messageDigest.digest(), Base64.URL_SAFE);

        //pubnub channel names cannot be more than 92 characters
        if (encodedChannelName.length() > 92) {
            encodedChannelName = encodedChannelName.substring(0, 91);
        }
        //pubnub channel names cannot have whitespace characters
        return encodedChannelName.trim();
    } catch (Exception e) {
        Log.d("X", "Error in encoding: " + e.getMessage());
        return null;
    }
}

From source file:Main.java

/**
 * Returns the Md5 hash for the given text.
 * //from   ww  w. jav  a2  s.  c  o m
 * @param text
 *            String whose Md5 hash should be returned.
 * @return Returns the Md5 hash for the given text.
 */
public static String getMd5Hash(String text) {
    StringBuffer result = new StringBuffer(32);
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(text.getBytes());
        Formatter f = new Formatter(result);

        byte[] digest = md5.digest();

        for (int i = 0; i < digest.length; i++) {
            f.format("%02x", new Object[] { new Byte(digest[i]) });
        }
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }

    return result.toString();
}

From source file:br.com.bluesoft.pronto.service.Seguranca.java

public static String encrypt(final String x) {
    try {/*from w w  w  . j ava 2s  .c  o  m*/
        final MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(x.getBytes());
        final byte[] hashMd5 = md.digest();
        final byte[] base64 = Base64.encodeBase64(hashMd5);
        return new String(base64);
    } catch (final Exception e) {
        return null;
    }
}