Example usage for java.security MessageDigest reset

List of usage examples for java.security MessageDigest reset

Introduction

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

Prototype

public void reset() 

Source Link

Document

Resets the digest for further use.

Usage

From source file:org.chililog.server.common.CryptoUtils.java

/**
 * <p>//ww  w .j a  va  2 s .c  o m
 * From a password, a number of iterations and a salt, returns the corresponding hash.
 * </p>
 * <p>
 * If the salt is to be appended, this convention is used: <code>base64(hash(plainTextValue + salt)+salt)</code>
 * </p>
 * <p>
 * If the salt is NOT to be appended, this convention is used: <code>base64(hash(plainTextValue + salt))</code>
 * </p>
 * 
 * @param plainTextValue
 *            String The password to encrypt
 * @param salt
 *            byte[] The salt. If null, one will be created on your behalf.
 * @param appendSalt
 *            True if the salt is to be appended to hashed value. In this way, for convenience, the salt can be kept
 *            with the hash. Use this only if the hash is to be kept internal to this app. If the hash is to be sent
 *            to external systems, set this to false and store the hash internally.
 * @return String The hash password
 * @throws ChiliLogException
 *             if SHA-512 is not supported or UTF-8 is not a supported encoding algorithm
 */
public static String createSHA512Hash(String plainTextValue, byte[] salt, boolean appendSalt)
        throws ChiliLogException {
    try {
        if (plainTextValue == null) {
            throw new NullArgumentException("plainTextValue");
        }
        if (salt == null) {
            throw new NullArgumentException("salt");
        }

        // Convert plain text into a byte array.
        byte[] plainTextBytes = plainTextValue.getBytes("UTF-8");

        // Allocate array, which will hold plain text and salt.
        byte[] plainTextWithSaltBytes = new byte[plainTextBytes.length + salt.length];

        // Copy plain text bytes into resulting array.
        for (int i = 0; i < plainTextBytes.length; i++) {
            plainTextWithSaltBytes[i] = plainTextBytes[i];
        }

        // Append salt bytes to the resulting array.
        if (appendSalt) {
            for (int i = 0; i < salt.length; i++) {
                plainTextWithSaltBytes[plainTextBytes.length + i] = salt[i];
            }
        }

        // Create hash
        MessageDigest digest = MessageDigest.getInstance("SHA-512");
        digest.reset();
        byte[] hashBytes = digest.digest(plainTextWithSaltBytes);

        // Create array which will hold hash and original salt bytes.
        byte[] hashWithSaltBytes = new byte[hashBytes.length + salt.length];

        // Copy hash bytes into resulting array.
        for (int i = 0; i < hashBytes.length; i++) {
            hashWithSaltBytes[i] = hashBytes[i];
        }

        // Append salt bytes to the result.
        for (int i = 0; i < salt.length; i++) {
            hashWithSaltBytes[hashBytes.length + i] = salt[i];
        }

        // Convert hash to string
        Base64 encoder = new Base64(1000, new byte[] {}, false);
        return encoder.encodeToString(hashWithSaltBytes);
    } catch (Exception ex) {
        throw new ChiliLogException(ex, "Error attempting to hash passwords. " + ex.getMessage());
    }
}

From source file:com.ikanow.infinit.e.processing.generic.aggregation.AssociationAggregationUtils.java

private static String md5checksum(String toHash) {
    try {//from w w w  .j  a  v a  2 s . 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:me.j360.dubbo.modules.util.text.HashUtil.java

/**
 * , ?md5sha1./*w w  w . j ava2s . c  om*/
 */
private static byte[] digest(@NotNull byte[] input, MessageDigest digest, byte[] salt, int iterations) {
    // ?
    if (salt != null) {
        digest.update(salt);
    }

    // 
    byte[] result = digest.digest(input);

    // >1
    for (int i = 1; i < iterations; i++) {
        digest.reset();
        result = digest.digest(result);
    }

    return result;
}

From source file:net.mybox.mybox.Common.java

/**
 * Encrypt a raw password with a salt/*from  w w  w . java2s .  c  om*/
 * @param password String The password to encrypt
 * @param salt The salt
 * @return The encrypted password
 */
public static String encryptPassword(String password, String salt)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {

    byte[] bSalt = base64ToByte(salt);

    MessageDigest digest = null;

    digest = MessageDigest.getInstance("SHA-1");

    digest.reset();
    digest.update(bSalt);
    byte[] input = null;

    input = digest.digest(password.getBytes("UTF-8"));

    for (int i = 0; i < 5; i++) { // hash 5 times for good measure
        digest.reset();
        input = digest.digest(input);
    }

    String output = byteToBase64(input);
    return output;
}

From source file:sk.lazyman.gizmo.util.GizmoUtils.java

public static String toSha1(String value) {
    if (value == null) {
        return null;
    }//  www . j  av a 2s  .  com

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();
        md.update(value.getBytes(StandardCharsets.UTF_8));

        char[] array = Hex.encode(md.digest());
        return new String(array);
    } catch (NoSuchAlgorithmException e) {
        new RuntimeException(e);
    }

    return null;
}

From source file:org.zanata.dao.AccountDAO.java

public static String createSaltedApiKey(String username) {
    try {/*w  w w .  ja va 2 s.  c  om*/
        byte[] salt = new byte[16];
        SecureRandom.getInstance("SHA1PRNG").nextBytes(salt);
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] name = username.getBytes("UTF-8");

        // add salt
        byte[] salted = new byte[name.length + salt.length];
        System.arraycopy(name, 0, salted, 0, name.length);
        System.arraycopy(salt, 0, salted, name.length, salt.length);

        // generate md5 digest
        md5.reset();
        byte[] digest = md5.digest(salted);

        return new String(PasswordUtil.encodeHex(digest));

    } catch (Exception exc) {
        throw new RuntimeException(exc);
    }

}

From source file:com.photon.phresco.service.util.ServerUtil.java

/**
 * Returns one way hashed string using SALT
 * //from  w  ww  . j a  v  a  2s . com
 * @param userName
 * @param password
 * @return
 * @throws PhrescoException
 */
public static String encodeUsingHash(String userName, String password) throws PhrescoException {
    String salt = password + userName;
    StringBuffer stringBuffer = new StringBuffer();
    byte[] bytes = salt.getBytes();
    MessageDigest msgDigest;
    try {
        msgDigest = MessageDigest.getInstance("MD5");
        msgDigest.reset();
        msgDigest.update(bytes);
        byte messageDigests[] = msgDigest.digest();
        for (int i = 0; i < messageDigests.length; i++) {
            stringBuffer.append(Integer.toHexString(MAGICNUMBER.HEXADECIMAL & messageDigests[i]));
        }
    } catch (NoSuchAlgorithmException e) {
        throw new PhrescoException(e);
    }
    return stringBuffer.toString();
}

From source file:com.kingen.util.Digests.java

/**
 * , ?md5sha1.//from  w  w w  . j  a v a 2s. co  m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) { // 1-1
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:org.getobjects.foundation.UData.java

/**
 * Calculates an MD5 hash over the given byte array and returns it as a
 * byte array.//from   w ww  .  j  av a2  s  .  c  o m
 *
 * @param _p - a byte array
 * @return the MD5 hash, or null on error
 */
public static byte[] md5DataHashForData(final byte[] _p) {
    if (_p == null)
        return null;

    try {
        // TODO: cache digest in thread local variable?
        final MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(_p);
        final byte[] res = md5.digest();
        md5.reset(); // TBD: superflous?
        return res;
    } catch (NoSuchAlgorithmException e) {
        log.error("did not find MD5 hash generator", e);
        return null;
    }
}

From source file:com.ironchain.common.kits.DigestKit.java

/**
 * , ?md5sha1./*from   w  w w. j a v  a 2s .c  om*/
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw ExceptionKit.unchecked(e);
    }
}