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:ua.naiksoftware.chars.Sender.java

public static final String md5(final String s) {
    Log.d(tag, "md5 begin");
    try {// w  ww . j a  v a  2s.c  om
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();
        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2) {
                h = "0" + h;
            }
            hexString.append(h);
        }
        Log.d(tag, "md5 end with hash=" + hexString);
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        Log.e(tag, "md5 algorithm excepton", e);
        e.printStackTrace();
    }
    Log.d(tag, "md5 end with empty hash");
    return "";
}

From source file:com.magic.util.HashCrypt.java

/**
 * ?hashType/* w  w  w  .  j a v a2s.co m*/
 * @param str
 * @param hashType
 * @return
 */
public static String getDigestHash(String str, String hashType) {
    if (str == null)
        return null;
    try {
        MessageDigest messagedigest = MessageDigest.getInstance(hashType);
        byte[] strBytes = str.getBytes();

        messagedigest.update(strBytes);
        byte[] digestBytes = messagedigest.digest();
        char[] digestChars = Hex.encodeHex(digestBytes);

        return "{" + hashType + "}" + new String(digestChars, 0, digestChars.length);
    } catch (Exception e) {
        throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e);
    }
}

From source file:com.pfarrell.crypto.HmacUtil.java

/**
 * calculate the sha of the argument, return hex encoded value
 * @param message to hash/*from w  w  w .  j a  v a  2  s.  co m*/
 * @return hexified result
 */
public static String sha(byte[] message) {
    Preconditions.checkNotNull(message);
    byte[] gas = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.update(message);
        gas = digest.digest();

    } catch (Exception e) {
        System.out.println("WebUtils.sha256 - caught exception: " + e.toString());
    }
    return hexify(gas);
}

From source file:com.njmd.framework.utils.EncodeUtil.java

/**
 * MD5?/* ww w  . j  a  v  a  2 s  . com*/
 */
public static String encodeMD5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    try {
        byte[] strTemp = s.getBytes();
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        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;
    }
}

From source file:Main.java

public static String md5Three(String clientId, String pwd, String timestamp) {
    clientId = clientId == null ? "" : clientId;
    pwd = pwd == null ? "" : pwd;
    timestamp = timestamp == null ? "" : timestamp;
    while (timestamp.length() < 10) {
        timestamp = "0" + timestamp;
    }//from  w  ww .  j a va 2  s.c om
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    md.update(clientId.getBytes());
    md.update(new byte[7]);
    md.update(pwd.getBytes());
    md.update(timestamp.getBytes());

    return byteArrayToHexString(md.digest());
}

From source file:Main.java

public static String hashString(String input) {
    MessageDigest md5;
    try {/*from   w w w.  j a v  a  2  s  . c  o  m*/
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return String.valueOf(input.hashCode());
    }
    md5.update(input.getBytes());
    byte[] hash = md5.digest();
    return toHex(hash);
}

From source file:lucee.commons.digest.Hash.java

public static String hash(byte[] data, String algorithm, char[] encoding) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.reset();/*from  w w w  . j av a 2s.c  om*/
    md.update(data);
    return new String(enc(md.digest(), encoding)); // no charset needed because all characters are below us-ascii (hex)
}

From source file:lucee.commons.digest.Hash.java

public static String hash(String str, String nonce, String algorithm, char[] encoding)
        throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.reset();/*  ww w. j  a  va 2s.  c  om*/
    md.update(toBytes(str, CharsetUtil.UTF8));
    md.update(DEL);
    md.update(toBytes(nonce, CharsetUtil.UTF8));
    return new String(enc(md.digest(), encoding)); // no charset needed because all characters are below us-ascii (hex)
}

From source file:dk.dma.msinm.user.security.SecurityUtils.java

/**
 * Hashes and Hex'es the password/*from   w  w  w  . j a va 2 s .  c  om*/
 * @param pwd the password
 * @param salt the salt
 * @param type the digest, e.g. "SHA-512"
 * @return the hashed and hex'ed password
 */
public static String encrypt(String pwd, String salt, String type) {
    try {
        MessageDigest md = MessageDigest.getInstance(type);
        md.reset();
        if (StringUtils.isNotBlank(salt)) {
            md.update(salt.getBytes("UTF-8"));
        }
        md.update(pwd.getBytes("UTF-8"));
        byte[] hash = md.digest();

        return hex(hash);
    } catch (Exception e) {
        throw new RuntimeException("Unable to create message digest", e);
    }
}

From source file:mx.gob.inadem.util.CryptoUtil.java

/**
 * <p>//from  w  ww  .  j a v a 2 s  . c om
 * Helper method that creates an RFC 2307-compliant salted, hashed password with the SHA1
 * MessageDigest algorithm. After the password is digested, the first 20
 * bytes of the digest will be the actual password hash; the remaining bytes
 * will be the salt. Thus, supplying a password <code>testing123</code>
 * and a random salt <code>foo</code> produces the hash:
 * </p>
 * <blockquote><code>{SSHA}yfT8SRT/WoOuNuA6KbJeF10OznZmb28=</code></blockquote>
 * <p>
 * In layman's terms, the formula is
 * <code>digest( secret + salt ) + salt</code>. The resulting digest is Base64-encoded.</p>
 * 
 * @param password the password to be digested
 * @param salt the random salt
 * @return the Base64-encoded password hash, prepended by <code>{SSHA}</code>.
 * @throws NoSuchAlgorithmException If your JVM is totally b0rked and does not have SHA1.
 */
protected static String getSaltedPassword(byte[] password, byte[] salt) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("SHA");
    digest.update(password);
    byte[] hash = digest.digest(salt);

    // Create an array with the hash plus the salt
    byte[] all = new byte[hash.length + salt.length];
    for (int i = 0; i < hash.length; i++) {
        all[i] = hash[i];
    }
    for (int i = 0; i < salt.length; i++) {
        all[hash.length + i] = salt[i];
    }
    byte[] base64 = Base64.encodeBase64(all);
    return SSHA + new String(base64);
}