Example usage for java.security MessageDigest update

List of usage examples for java.security MessageDigest update

Introduction

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

Prototype

public final void update(ByteBuffer input) 

Source Link

Document

Update the digest using the specified ByteBuffer.

Usage

From source file:com.dianping.wed.cache.redis.util.TestDataUtil.java

/**
 * MD5/*from  w  w w.  j a  v  a  2 s  . c  o  m*/
 *
 * @param str ?
 * @return ?
 */
public static String toMD5(String str) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] byteDigest = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < byteDigest.length; offset++) {
            i = byteDigest[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        //32?
        return buf.toString();
        // 16?
        //return buf.toString().substring(8, 24);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.licc.common.util.Digests.java

public static String md5(String str) {
    try {//w  w  w . j a v a 2 s . c  om
        MessageDigest e = MessageDigest.getInstance("MD5");
        e.update(str.getBytes());
        byte[] b = e.digest();
        StringBuffer buf = new StringBuffer("");

        for (int offset = 0; offset < b.length; ++offset) {
            int i = b[offset];
            if (i < 0) {
                i += 256;
            }

            if (i < 16) {
                buf.append("0");
            }

            buf.append(Integer.toHexString(i));
        }
        str = buf.toString();
    } catch (Exception var6) {
        var6.printStackTrace();
    }

    return str;
}

From source file:Main.java

public static String MD5(String text) {
    StringBuffer buffer;//from w  w  w  . j a  va 2s  .c  o m
    MessageDigest digest;
    byte[] data;
    int i, n;

    buffer = new StringBuffer();

    try {
        digest = MessageDigest.getInstance("MD5");
        digest.update(text.getBytes());
        data = digest.digest();

        for (i = 0, n = data.length; i < n; ++i) {
            buffer.append(Integer.toHexString(0xff & data[i]));
        }
    } catch (Exception e) {
    }

    return buffer.toString();
}

From source file:Main.java

public static String sha512(String what_to_encode) {
    final MessageDigest sha512;
    try {//from   w w  w . j av  a  2 s .  c  o m
        sha512 = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e) {
        return "404";
    }
    sha512.update(what_to_encode.getBytes());
    byte byteData[] = sha512.digest();
    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();
}

From source file:Main.java

public static String encode(String code) {
    MessageDigest md;
    try {//from ww w . ja v a2s.c om
        md = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    md.update(code.getBytes());

    byte[] buffer = md.digest();
    StringBuffer hexString = new StringBuffer(2 * buffer.length);

    for (int i = 0; i < buffer.length; i++) {
        appendHexPair(buffer[i], hexString);
    }
    return hexString.toString();
}

From source file:com.stickyd.services.impl.UserLoginServiceImpl.java

public static String md5(String s) {
    MessageDigest md5;
    try {//from w w  w  . j a v a 2s.  c  om
        md5 = MessageDigest.getInstance("MD5");
        md5.update(s.getBytes());
        return new BigInteger(1, md5.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    }
}

From source file:cn.crawin.msg.gateway.http.MessageDigestUtil.java

/**
 * MD5??Base64???/*www. j  av a2 s.  com*/
 *
 * @return
 */
public static String base64AndMD5(byte[] bytes) {
    if (bytes == null) {
        throw new IllegalArgumentException("bytes can not be null");
    }
    try {
        final MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(bytes);
        final Base64 base64 = new Base64();
        final byte[] enbytes = base64.encode(md.digest());
        return new String(enbytes);
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("unknown algorithm MD5");
    }
}

From source file:com.ikanow.infinit.e.api.authentication.PasswordEncryption.java

public static String md5checksum(String toHash) {
    try {//from   w w w . ja va  2s. c  om
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(toHash.getBytes(Charset.forName("UTF8")));
        byte[] digest = m.digest();
        return new String(Hex.encodeHex(digest));
    } catch (Exception ex) {
        return toHash;
    }
}

From source file:com.cisco.oss.foundation.directory.utils.ObfuscatUtil.java

/**
 * Compute the the hash value for the String.
 *
 * @param passwd//from w  ww.  j  ava2 s.  c  o  m
 *         the password String
 * @return
 *         the Hash digest byte.
 * @throws NoSuchAlgorithmException
 *         the NoSuchAlgorithmException.
 */
public static byte[] computeHash(String passwd) throws NoSuchAlgorithmException {
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(passwd.getBytes());
    return md.digest();
}

From source file:com.corngo.base.support.utils.security.Digests.java

public final static String md5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    try {//ww  w .  j  av a2  s  .  co  m
        byte[] btInput = s.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.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) {
        e.printStackTrace();
        return null;
    }
}