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:Main.java

public static byte[] hash(String message) {
    MessageDigest cript = null;
    try {//w  ww .j a  va2  s.co m
        cript = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    cript.reset();
    try {
        cript.update(message.getBytes("utf8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    return cript.digest();
}

From source file:com.continusec.client.ObjectHash.java

private static final byte[] hashString(String s, String r) throws ContinusecException {
    if (r != null && s.startsWith(r)) {
        try {/*ww w .  j av a 2s.c o  m*/
            return Hex.decodeHex(s.substring(r.length()).toCharArray());
        } catch (DecoderException e) {
            throw new InvalidObjectException(e);
        }
    } else {
        try {
            MessageDigest d = DigestUtils.getSha256Digest();
            d.update((byte) 'u');
            d.update(Normalizer.normalize(s, Normalizer.Form.NFC).getBytes("UTF8"));
            return d.digest();
        } catch (UnsupportedEncodingException e) {
            throw new InvalidObjectException(e);
        }
    }
}

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

public static String calculateHash(String[] envs) {
    try {//from  www.  ja  va  2 s  .com
        MessageDigest md = MessageDigest.getInstance("MD5");
        for (String e : envs) {
            md.update(e.getBytes("UTF-8"));
        }

        StringBuilder sb = new StringBuilder();
        byte[] digest = md.digest();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();
    } catch (Exception ex) {
        String errMsg = "Unable to calculate execution env. checksum! Reason: " + ex.getMessage();
        logError(errMsg);
        throw new RuntimeException(errMsg);
    }
}

From source file:com.taobao.datax.plugins.common.DBSource.java

private static String md5(String key) {
    try {/*  w  w  w.  j  a  v a 2s .c om*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(key.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuffer buf = new StringBuffer(32);
        for (byte aB : b) {
            i = aB;
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(i));
        }
        return buf.toString().substring(8, 24);
    } catch (NoSuchAlgorithmException e) {
        logger.error(ExceptionTracker.trace(e));
        return key;
    }
}

From source file:com.monitor.baseservice.utils.XCodeUtil.java

public static String sha1UrlSafeB64(byte[] bytes) {
    try {// www.  j a v a  2 s  .  c o  m
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(bytes);
        return Base64.encodeBase64URLSafeString(md.digest());
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.savor.ads.core.ApiRequestFactory.java

public static String md5(String input) {
    try {//from   w  ww .j ava  2  s  . c  om
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(input.getBytes());
        byte[] result = md5.digest();//
        return StringUtils.toHexString(result, false);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

final public static void writeStringToDigest(final String str, final MessageDigest messageDigest) {
    final int length = str.length();
    int i = 0;/*from  w w w  .ja va  2  s .com*/
    char c;
    while (i < length) {
        c = str.charAt(i++);
        if (c < 0x80) {
            messageDigest.update((byte) c);
            continue;
        }
        if ((c >= 0xD800 && c <= 0xDBFF) || (c >= 0xDC00 && c <= 0xDFFF)) {
            //No Surrogates in sun java
            messageDigest.update((byte) 0x3f);
            continue;
        }
        char ch;
        int bias;
        int write;
        if (c > 0x07FF) {
            ch = (char) (c >>> 12);
            write = 0xE0;
            if (ch > 0) {
                write |= (ch & 0x0F);
            }
            messageDigest.update((byte) write);
            write = 0x80;
            bias = 0x3F;
        } else {
            write = 0xC0;
            bias = 0x1F;
        }
        ch = (char) (c >>> 6);
        if (ch > 0) {
            write |= (ch & bias);
        }
        messageDigest.update((byte) write);
        messageDigest.update((byte) (0x80 | ((c) & 0x3F)));

    }

}

From source file:com.mnt.base.util.HashUtil.java

public static String hashBy(String source, String mdType) {

    if (source != null) {
        MessageDigest digest = getMessageDigest(mdType);

        if (digest != null) {
            digest.reset();/*from   www. j  a v  a 2 s  .c om*/
            digest.update(source.getBytes());

            return toHexString(digest.digest());
        } else {
            return source;
        }
    }

    return null;
}

From source file:uk.org.funcube.fcdw.server.processor.DataProcessor.java

private static String calculateDigest(final String hexString, final String authCode, final Integer utf)
        throws NoSuchAlgorithmException {

    String digest = null;//from  w w  w  .  jav  a2  s  .  c  o m

    final MessageDigest md5 = MessageDigest.getInstance("MD5");

    if (utf == null) {

        md5.update(hexString.getBytes());
        md5.update(":".getBytes());
        digest = convertToHex(md5.digest(authCode.getBytes()));
    } else if (utf.intValue() == 8) {
        md5.update(hexString.getBytes(Charset.forName("UTF8")));
        md5.update(":".getBytes(Charset.forName("UTF8")));
        digest = convertToHex(md5.digest(authCode.getBytes(Charset.forName("UTF8"))));
    } else {
        md5.update(hexString.getBytes(Charset.forName("UTF16")));
        md5.update(":".getBytes(Charset.forName("UTF16")));
        digest = convertToHex(md5.digest(authCode.getBytes(Charset.forName("UTF16"))));
    }

    return digest;
}

From source file:com.github.benyzhous.springboot.web.core.gateway.sign.backend.Sign.java

/**
 * MD5??Base64???//from  ww  w  .j  a v  a  2  s.c o m
 *
 * @param bytes 
 * @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();

        return new String(base64.encode(md.digest()));
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("unknown algorithm MD5");
    }
}