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:de.bluepair.sci.client.SHAUtils.java

public static String sha512(byte[] data) {
    MessageDigest md = getDigest();
    if (md == null || data == null) {
        return "";
    }//from  ww  w  .  j a v a 2s .  c om
    md.update(data);
    final byte[] sha1hash = md.digest();
    return Hex.encodeHexString(sha1hash);
}

From source file:com.slimsmart.common.util.code.EncodeUtil.java

public static String securityEncode(String input)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest m = MessageDigest.getInstance("SHA-256");
    m.update(input.getBytes("UTF8"));
    byte s[] = m.digest();
    return hex(s);
}

From source file:com.facebook.stetho.websocket.WebSocketHandler.java

private static String generateServerKey(String clientKey) {
    try {//from w  w w .  j  a  va2  s. c  o m
        String serverKey = clientKey + SERVER_KEY_GUID;
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        sha1.update(Utf8Charset.encodeUTF8(serverKey));
        return Base64.encodeToString(sha1.digest(), Base64.NO_WRAP);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.meshpoint.anode.util.ModuleUtils.java

public static String getResourceUriHash(String id) {
    try {/*from www. ja  v a2s. c o  m*/
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        sha.update(id.getBytes("iso-8859-1"));
        return digest2Hex(sha.digest());
    } catch (Exception e) {
        return null;
    }
}

From source file:com.bstek.dorado.util.StringAliasUtils.java

public static String getMD5(byte[] source) throws NoSuchAlgorithmException {
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
    md.update(source);
    byte tmp[] = md.digest(); // MD5  128 ? 16 
    char str[] = new char[16 * 2]; // ? 16 ?? 16
    // ? 32 //from   w  w w .j av a  2  s  . c o  m
    int k = 0; // ??
    for (int i = 0; i < 16; i++) { //  MD5 ??? 16
        // ?
        byte byte0 = tmp[i]; // ? i 
        str[k++] = HEX_DIGITALS[byte0 >>> 4 & 0xf]; // ? 4 ??
        str[k++] = HEX_DIGITALS[byte0 & 0xf]; // ? 4 ??
    }
    return new String(str); // ???
}

From source file:com.ecyrd.jspwiki.util.CryptoUtil.java

/**
 *  Compares a password to a given entry and returns true, if it matches.
 *  /*from  ww  w  .j av a 2 s .  c o  m*/
 *  @param password The password in bytes.
 *  @param entry The password entry, typically starting with {SSHA}.
 *  @return True, if the password matches.
 *  @throws NoSuchAlgorithmException If there is no SHA available.
 *  @throws UnsupportedEncodingException If no UTF-8 encoding is available 
 */
public static boolean verifySaltedPassword(byte[] password, String entry)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    // First, extract everything after {SSHA} and decode from Base64
    if (!entry.startsWith(SSHA)) {
        throw new IllegalArgumentException("Hash not prefixed by {SSHA}; is it really a salted hash?");
    }
    byte[] challenge = Base64.decodeBase64(entry.substring(6).getBytes("UTF-8"));

    // Extract the password hash and salt
    byte[] passwordHash = extractPasswordHash(challenge);
    byte[] salt = extractSalt(challenge);

    // Re-create the hash using the password and the extracted salt
    MessageDigest digest = MessageDigest.getInstance("SHA");
    digest.update(password);
    byte[] hash = digest.digest(salt);

    // See if our extracted hash matches what we just re-created
    return Arrays.equals(passwordHash, hash);
}

From source file:com.algodefu.yeti.md5.MD5HashGenerator.java

public static String generateKeyByString(String string) {
    MessageDigest m = null;
    try {// www.j a  va 2 s  .c o m
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.reset();
    m.update(string.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.toString(16);
    while (hashtext.length() < 32) {
        hashtext = "0" + hashtext;
    }
    return hashtext;
}

From source file:com.ecyrd.jspwiki.util.CryptoUtil.java

/**
 * <p>/*from   w ww .ja  va 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);

    String saltedString = null;
    try {
        saltedString = SSHA + new String(base64, "UTF8");
    } catch (UnsupportedEncodingException e) {
        log.fatal("You do not have UTF-8!?!");
    }
    return saltedString;
}

From source file:com.liferay.util.Encryptor.java

public static String digest(String algorithm, String text) {
    MessageDigest mDigest = null;

    try {/*from   w ww  .  j  a  va 2 s .c  om*/
        mDigest = MessageDigest.getInstance(algorithm);

        mDigest.update(text.getBytes(ENCODING));
    } catch (NoSuchAlgorithmException nsae) {
        Logger.error(Encryptor.class, nsae.getMessage(), nsae);

    } catch (UnsupportedEncodingException uee) {
        Logger.error(Encryptor.class, uee.getMessage(), uee);
    }

    byte raw[] = mDigest.digest();

    return Base64.encode(raw);
}

From source file:gov.medicaid.services.util.Util.java

/**
 * Hashes the given string using the given salt value.
 * @param value the value to be hashed/*from ww w. j  av  a2 s .c o  m*/
 * @param salt the salt for the hash
 * @return Base 64 encoded SHA hash of the salted value
 */
public static String hash(String value, String salt) {
    try {
        String saltedValue = value + salt;
        MessageDigest sha = MessageDigest.getInstance("SHA");
        sha.update(saltedValue.getBytes());
        byte[] hash = sha.digest();
        return new String(Base64.encodeBase64(hash));
    } catch (NoSuchAlgorithmException e) {
        throw new PortalServiceConfigurationException("No valid encryption algorithm was found.", e);
    }
}