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:net.cheesecan.cheeselobby.session.MD5Base64Hash.java

public static String encrypt(String str) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(str.getBytes());//w  w  w  .  j  a  v a 2s .c  o m
    byte[] enc = md.digest();
    return new String(new Base64().encode(enc));
}

From source file:Main.java

public static String sha1(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.update(input.getBytes("UTF-8"));
    byte[] output = md.digest();
    return bytesToHex(output);
}

From source file:Main.java

public final static String MD5(String s) throws Exception {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    byte[] strTemp = s.getBytes();
    MessageDigest mdTemp = MessageDigest.getInstance("MD5");
    mdTemp.update(strTemp);//from  w ww.  j  a v a2 s  .c o m
    byte[] md = mdTemp.digest();
    int j = md.length;
    char str[] = new char[j * 2];
    int k = 0;
    for (int i = 0; i < j; i++) {
        byte byte0 = md[i];
        str[k++] = hexDigits[byte0 >>> 4 & 0xf];
        str[k++] = hexDigits[byte0 & 0xf];
    }
    return new String(str);
}

From source file:Main.java

public static byte[] md5(byte[] bytes) {
    try {/*from  ww  w  .  j a v  a 2s  . c om*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(bytes);
        return messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:MainClass.java

public static byte[] makeDigest(byte[] mush, long t2, double q2) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(mush);//from  w ww.j  av  a  2  s  .c  o  m
    md.update(makeBytes(t2, q2));
    return md.digest();
}

From source file:Main.java

public static String generateMd5Hash(String str) {
    try {//from  ww w .  j  a v a2 s.c  o  m
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(str.getBytes("UTF8"));
        byte s[] = m.digest();
        String result = "";
        for (int i = 0; i < s.length; i++) {
            result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public final static String MD5(String s) {
    try {//from  w ww.ja  v  a  2  s.  c o  m
        byte[] btInput = s.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < md.length; i++) {
            int val = ((int) md[i]) & 0xff;
            if (val < 16)
                sb.append("0");
            sb.append(Integer.toHexString(val));
        }
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String cryptoStr2MD5(String str) {
    try {/* w ww . ja  va 2  s. c o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] bytes = md.digest();
        return byte2Hex(bytes);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:de.kaojo.security.util.SecureHashingAlgorithmHelper.java

public static String hashSHA256(String input) throws NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance(ALGORITHM);

    md.update(input.getBytes());/*from w w  w  .  java  2s  .  c o m*/
    byte[] digest = md.digest();

    byte[] encode = Base64.encodeBase64(digest);
    return new String(encode);
}

From source file:Main.java

public final static String getMessageDigest(byte[] buffer) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    try {/*ww w  .ja v a2 s  .c  o m*/
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(buffer);
        byte[] md = mdTemp.digest();
        int j = md.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    } catch (Exception e) {
        return null;
    }
}