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:jp.co.opentone.bsol.framework.core.util.HashUtil.java

/**
 * ??(?)??(UUID.randomUUID()).//from   www  . j a va  2 s  . c om
 * @param   parameter ????UUID??????
 * @return  ?
 */
public static String getRandomString(String parameter) {
    String result = null;
    String salt = parameter + UUID.randomUUID().toString();

    try {
        MessageDigest md = MessageDigest.getInstance(ALGORITHM);
        md.update(salt.getBytes());
        result = new String(Hex.encodeHex(md.digest()));
    } catch (NoSuchAlgorithmException e) {
        // ALGORITHM?????????????
        throw new RuntimeException(e);
    }
    return result;
}

From source file:MD5.java

public static String create(String content) throws MD5IsNotSupported {
    String result = "";
    try {/*  w  ww  .  j  a  v a 2s. c o  m*/
        byte[] defaultBytes = content.getBytes();
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        result = hexString.toString();
    } catch (NoSuchAlgorithmException ex) {
        throw new MD5IsNotSupported(ex);
    }

    assert !result.isEmpty();
    return result;
}

From source file:MD5.java

/**
 * Encodes a string//from w ww  .  j  av a2 s .  co m
 * 
 * @param str String to encode
 * @return Encoded String
 * @throws NoSuchAlgorithmException
 */
public static String crypt(String str) {
    if (str == null || str.length() == 0) {
        throw new IllegalArgumentException("String to encript cannot be null or zero length");
    }

    StringBuffer hexString = new StringBuffer();

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] hash = md.digest();

        for (int i = 0; i < hash.length; i++) {
            if ((0xff & hash[i]) < 0x10) {
                hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
            } else {
                hexString.append(Integer.toHexString(0xFF & hash[i]));
            }
        }
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("" + e);
    }

    return hexString.toString();
}

From source file:Main.java

private static String md5(String in) {
    MessageDigest digest;
    try {//from   w ww . ja v a  2 s  .c  o  m
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        byte[] a = digest.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

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

/**
 * Calculate the Merkle Tree Leaf Hash for an object (HASH(chr(0) || b)).
 * @param b the input to the leaf hash//ww w  . j  ava2 s .c  o  m
 * @return the leaf hash.
 */
public static final byte[] leafMerkleTreeHash(byte[] b) {
    MessageDigest d = DigestUtils.getSha256Digest();
    d.update((byte) 0);
    d.update(b);
    return d.digest();
}

From source file:Main.java

public static String md5(final String in) {
    MessageDigest digest;
    try {/* w  ww .j av a 2  s.  com*/
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        final byte[] a = digest.digest();
        final int len = a.length;
        final StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String encode(String str, String method) {
    MessageDigest md = null;
    String dstr = null;/* ww  w  .  j  a  v a  2s  .c om*/
    try {
        md = MessageDigest.getInstance(method);
        md.update(str.getBytes());
        dstr = new BigInteger(1, md.digest()).toString(16);
        int dstrCount = 32 - dstr.length();
        for (int i = 0; i < dstrCount; i++) {
            dstr = "0" + dstr;
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return dstr;
}

From source file:annis.utils.Utils.java

/** Hashes a string using SHA-256. */
public static String calculateSHAHash(String s) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(s.getBytes("UTF-8"));
    byte[] digest = md.digest();

    StringBuilder hashVal = new StringBuilder();
    for (byte b : digest) {
        hashVal.append(String.format("%02x", b));
    }//from w w w.j a  v  a  2 s.  c  o m

    return hashVal.toString();
}

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

/**
 * Calculate the Merkle Tree Node Hash for an existing left and right hash (HASH(chr(1) || l || r)).
 * @param l the left node hash.//  w w w . j av  a 2s . c  o  m
 * @param r the right node hash.
 * @return the node hash for the combination.
 */
public static final byte[] nodeMerkleTreeHash(byte[] l, byte[] r) {
    MessageDigest d = DigestUtils.getSha256Digest();
    d.update((byte) 1);
    d.update(l);
    d.update(r);
    return d.digest();
}

From source file:it.paolorendano.clm.util.Utils.java

/**
 * Hash password.//  ww w.j a  v  a 2  s.  c o  m
 *
 * @param password the password
 * @return the string
 */
public static String hashPassword(String password) {
    MessageDigest mdigest;
    try {
        mdigest = MessageDigest.getInstance("SHA-256");
        mdigest.update(password.getBytes("UTF-8"));
        return Base64.encodeBase64String(mdigest.digest());
    } catch (NoSuchAlgorithmException e) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error(e.getMessage(), e);
    }
    return null;
}