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:it.cilea.osd.jdyna.utils.HashUtil.java

public static String hashMD5(String passw) {
    String passwHash = "";
    try {/*w  w w  .j  a  v a  2s .  c o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(passw.getBytes());
        byte[] result = md.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            String tmpStr = "0" + Integer.toHexString((0xff & result[i]));
            sb.append(tmpStr.substring(tmpStr.length() - 2));
        }
        passwHash = sb.toString();
    } catch (NoSuchAlgorithmException ecc) {
        log.error("Errore algoritmo " + ecc);
    }
    return passwHash;
}

From source file:Main.java

public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    byte[] sha1hash = md.digest();
    return convertToHex(sha1hash);
}

From source file:Main.java

public static String SHA1(String text) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(text.getBytes());//  w w w.  j a va 2  s . c o  m

    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));
    }

    //System.out.println("Hex format : " + sb.toString());

    //convert the byte to hex format method 2
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        String hex = Integer.toHexString(0xff & byteData[i]);
        if (hex.length() == 1)
            hexString.append('0');
        hexString.append(hex);
    }
    return hexString.toString();
}

From source file:Main.java

private static final String md5(final String s) {
    final String MD5 = "MD5";
    try {/*from w w w. j  a v  a  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
        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 "";
}

From source file:helper.Digester.java

/**
 * @param str the input string/* ww  w .jav a 2s .  c  o  m*/
 * @return the digested string
 * @throws NoSuchAlgorithmException encryption algorithm not installed 
 */
public static String digest(String str) throws NoSuchAlgorithmException {
    Configuration config = Play.application().configuration();

    String salt = config.getString("application.secret");

    String saltedStr = str + salt;

    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(saltedStr.getBytes());

    byte byteData[] = md.digest();

    byte[] base64 = Base64.encodeBase64(byteData);

    String result = new String(base64, Charset.defaultCharset());

    return result;
}

From source file:Main.java

public static final String md5(final String s) {
    try {/*from  w w w. j a v a  2  s.co 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);
        }
        return hexString.toString();

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

From source file:Main.java

public static String getHashKey(Context context) {
    // Add code to print out the key hash
    try {//from  w w w  .jav a2 s .c  o  m
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            return Base64.encodeToString(md.digest(), Base64.DEFAULT);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
    return null;
}

From source file:Main.java

public static String getMd5Digest(String pInput) {
    if (pInput == null)
        return "";

    try {//  w  w  w  . j  ava  2s  .c o m
        MessageDigest lDigest = MessageDigest.getInstance("MD5");
        lDigest.update(getBytes(pInput));
        BigInteger lHashInt = new BigInteger(1, lDigest.digest());
        return String.format("%1$032X", lHashInt);
    } catch (NoSuchAlgorithmException lException) {
        throw new RuntimeException(lException);
    }
}

From source file:Main.java

public static String md5(String s) {
    try {/*from  w  w w.  ja  v a 2s . 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);
        }
        return hexString.toString();

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

}

From source file:Main.java

public static String md5(String str) {
    if (TextUtils.isEmpty(str)) {
        return null;
    }/*  w w  w  .  j a  v  a 2s. c  o  m*/
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(str.getBytes());
        return encodeHex(messageDigest.digest());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}