Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

In this page you can find the example usage for java.security MessageDigest getInstance.

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:ca.qc.adinfo.rouge.util.MD5.java

public static String hash(String value) {

    try {// w w  w . j  a v a 2s .co m

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(value.getBytes("iso-8859-1"), 0, value.length());
        md5hash = md.digest();

        return Hex.encodeHexString(md5hash);

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getMD5String(String str) {
    MessageDigest messageDigest = null;
    try {//  w  w  w  .  j av  a 2s .c om
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (Exception e) {
        return null;
    }

    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (byte b : byteArray) {
        if ((0xFF & b) < 0x10)
            md5StrBuff.append("0");
        md5StrBuff.append(Integer.toHexString(0xFF & b));
    }
    return md5StrBuff.toString();
}

From source file:Main.java

public static String getBytesDigestEncrypt(byte[] bytes, String algorithm) {
    if (algorithm.equals(MD5) || algorithm.equals(SHA1) || algorithm.equals(SHA256) || algorithm.equals(SHA384)
            || algorithm.equals(SHA512)) {
        MessageDigest digest = null;
        try {/*from  www.j  a  v  a 2s.  co m*/
            digest = MessageDigest.getInstance(algorithm);
            digest.update(bytes);
            return bytes2String(digest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static String md5L(String input) {
    try {/*  w w w. j ava  2 s  .co m*/
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(input.getBytes());
        byte[] md = mdInst.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte aMd : md) {
            String shaHex = Integer.toHexString(aMd & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String getSHA(byte[] message, int length) {
    if (message == null || length <= 0) {
        return null;
    }//  w  w  w .  jav  a 2s.  c o m
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(message, 0, length);
        byte[] output = md.digest();
        String s = "";
        for (int i = 0; i < output.length; i++) {
            s += hexByte(output[i] + 128);
        }
        return s;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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

/**
 * MD532??/* w  ww .j a  v a2 s.  c o  m*/
 *
 * @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 generateMD5(String value) {
    try {//from  ww  w  . j ava  2s.co m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] bytes;
        try {
            bytes = value.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e1) {
            bytes = value.getBytes();
        }
        StringBuilder result = new StringBuilder();
        for (byte b : md.digest(bytes)) {
            result.append(Integer.toHexString((b & 0xf0) >>> 4));
            result.append(Integer.toHexString(b & 0x0f));
        }
        return result.toString();
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

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 ww  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 md5(String input) {
    String result = input;/*from   ww  w .  jav a2 s.  c  o m*/
    if (input != null) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            if ((result.length() % 2) != 0) {
                result = "0" + result;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    return result;
}

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