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

private static String SHA1(String text) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");

    byte[] sha1hash = new byte[40];
    md.update(text.getBytes());//from w  w  w.j  a  v  a 2 s  .  co  m
    sha1hash = md.digest();

    return convertToHex(sha1hash);
}

From source file:Main.java

public static String md5(String input) {
    String res = "";
    try {/*w  w w  .  j av a 2s. co m*/
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(input.getBytes());
        byte[] md5 = algorithm.digest();
        String tmp = "";
        for (int i = 0; i < md5.length; i++) {
            tmp = (Integer.toHexString(0xFF & md5[i]));
            if (tmp.length() == 1) {
                res += "0" + tmp;
            } else {
                res += tmp;
            }
        }
    } catch (NoSuchAlgorithmException ex) {
    }
    return res;
}

From source file:Main.java

/**
 * Print hash key// w w w.  j a  v  a2 s.  c o  m
 */
public static void printHashKey(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(TAG, PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d(TAG, "keyHash: " + keyHash);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

From source file:Main.java

/**
 * @param s String to SHA-1 hash//from   www.  j a  va 2 s .  c o m
 * @return SHA-1 hashed string
 */
public static String sha1(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create SHA-1 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static final String MD5(String paramString) {
    char[] arrayOfChar1 = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };
    try {/* w w w . j a  v a2  s  .c  om*/
        byte[] arrayOfByte1 = paramString.getBytes();
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.update(arrayOfByte1);
        byte[] arrayOfByte2 = localMessageDigest.digest();
        int i = arrayOfByte2.length;
        char[] arrayOfChar2 = new char[i * 2];
        int j = 0;
        int k = 0;
        while (true) {
            if (j >= i)
                return new String(arrayOfChar2);
            int m = arrayOfByte2[j];
            int n = k + 1;
            arrayOfChar2[k] = arrayOfChar1[(0xF & m >>> 4)];
            k = n + 1;
            arrayOfChar2[n] = arrayOfChar1[(m & 0xF)];
            j++;
        }
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getMD5(String mykey) {
    try {/*from  w w w.  j a v  a2  s  . c o  m*/
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(mykey.getBytes(Charset.forName("UTF-8")));
        return bytesToHex(digest.digest()).toLowerCase();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "MD5 hash exception: " + e.toString());
        return null;
    }
}

From source file:Main.java

/**
 * @param s String to SHA-256 hash/*from  ww w.jav a  2 s.  c om*/
 * @return SHA-256 hashed string
 */
public static String sha256(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create SHA-256 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-256");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * encode By MD5/*w  w w.  j  av  a2  s.c o  m*/
 * 
 * @param str
 * @return String
 */
public static String md5(String str) {
    if (str == null) {
        return null;
    }
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(str.getBytes());
        return new String(encodeHex(messageDigest.digest()));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ykun.commons.utils.security.MD5Utils.java

/**
 * MD532??//from  w w  w  .  ja  va 2 s . com
 *
 * @param str
 * @return
 */
public static String md5(String str) {
    StringBuilder result = new StringBuilder();
    try {
        MessageDigest md5 = MessageDigest.getInstance(DIGEST);
        md5.update(str.getBytes(CHARSET_UTF8));
        byte[] b = md5.digest();
        for (int i = 0; i < b.length; ++i) {
            int x = b[i] & 0xFF;
            int h = x >>> 4;
            int l = x & 0x0F;
            result.append((char) (h + ((h < 10) ? '0' : 'a' - 10)));
            result.append((char) (l + ((l < 10) ? '0' : 'a' - 10)));
        }
    } catch (Exception e) {
        logger.error("md5 error:", e);
        throw new RuntimeException(e);
    }
    return result.toString();
}

From source file:Main.java

public static String SHA1(String str) {
    try {//from  w w  w .ja va2  s  .com
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
        digest.update(str.getBytes());
        byte messageDigest[] = digest.digest();
        // Create Hex String
        StringBuffer hexString = new StringBuffer();

        for (byte i : messageDigest) {
            String shaHex = Integer.toHexString(i & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();

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