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:com.elogiclab.vosao.plugin.FlickrUtils.java

private static String digest(String text) {
    MessageDigest algorithm = null;
    StringBuilder sig = new StringBuilder();
    try {/* ww  w .j  a v  a 2  s. com*/
        algorithm = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException exc) {
        throw new RuntimeException(exc);
    }
    algorithm.reset();
    algorithm.update(text.getBytes());
    byte messageDigest[] = algorithm.digest();
    for (int i = 0; i < messageDigest.length; i++) {
        String c = Integer.toHexString(0xFF & messageDigest[i]);
        if (c.length() == 1) {
            sig.append("0");
        }
        sig.append(c);
    }
    return sig.toString();
}

From source file:org.aludratest.cloud.impl.user.LocalUserDatabaseImpl.java

private static String calculateHash(String userName, String password) throws NoSuchAlgorithmException {
    try {/*from ww w  .j a  v  a2s  . c  om*/
        // calculate SHA1 hash
        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();
        crypt.update((userName + "/" + password).getBytes("UTF-8"));
        byte[] data = crypt.digest();

        // encode as BASE64
        return new String(Base64.encodeBase64(data), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // no UTF-8??
        throw new RuntimeException(e);
    }
}

From source file:$.Digests.java

/**
     * , ?md5sha1./*from w  ww  .  java2  s  . 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++) {
                digest.reset();
                result = digest.digest(result);
            }
            return result;
        } catch (GeneralSecurityException e) {
            throw Exceptions.unchecked(e);
        }
    }

From source file:com.omnigon.aem.handlebars.helpers.UniqueId.java

public static String generateUniqueId(String directoryPath) {
    byte[] bytesDirectoryPath = null;
    MessageDigest md = null;
    try {//from   w  w  w .j  av a2s.c  om
        bytesDirectoryPath = directoryPath.getBytes(CharEncoding.UTF_8);
        md = MessageDigest.getInstance(MESSAGE_DIGEST_TYPE);
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        logger.error(e.getMessage(), e);
        return StringUtils.EMPTY;
    }
    md.reset();
    md.update(bytesDirectoryPath);
    String uniqueId = DatatypeConverter.printHexBinary(md.digest());
    return StringUtils.substring(uniqueId, 0, UNIQUE_ID_LENGTH);
}

From source file:com.corngo.base.support.utils.security.Digests.java

/**
 * , ?md5sha1.//  ww w  .ja v  a2s . c o 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++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:de.eod.jliki.users.utils.PasswordHashUtility.java

/**
 * Generates a hash string (url save) from the given data.<br/>
 * @param data the data to hash/*w w w  . ja v a2 s . co  m*/
 * @return a string in url save format
 */
public static String generateHashForUrl(final String data) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance(ALGORITHM);
    } catch (final NoSuchAlgorithmException e) {
        LOGGER.fatal("Hash algorithm not found: " + ALGORITHM, e);
        return "";
    }
    digest.reset();
    byte[] hashBytes;
    try {
        hashBytes = digest.digest(data.getBytes(ENCODING));
    } catch (final UnsupportedEncodingException e) {
        LOGGER.fatal("Character encoding not supported: " + ENCODING, e);
        return "";
    }

    final String hashString = Base64.encodeBase64URLSafeString(hashBytes);
    return hashString;
}

From source file:org.icelib.Hash.java

public static String hash(String text, String username, String salt, boolean server) {
    try {//from  w w  w . j  a  v  a2s  .  c o  m
        MessageDigest instance = MessageDigest.getInstance("MD5");
        byte[] hashedPw = instance.digest(text.getBytes("ASCII"));
        final String authString = username + ":" + encode(hashedPw) + ":" + salt;
        String encoded = encode(instance.digest(authString.getBytes("ASCII")));
        if (server) {
            instance.reset();
            instance.update(encoded.getBytes("ASCII"));
            instance.update(salt.getBytes("ASCII"));
            encoded = encode(instance.digest());
        }
        return encoded;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.collectionspace.chain.csp.webui.misc.Generic.java

/**
 * Function to create a hash for the record traverser functionality
 * @param csid/*from  w  ww .jav  a  2 s.com*/
 * @return
 * @throws UIException 
 */
public static String createHash(String csid) throws UIException {
    try {
        byte[] buffer = csid.getBytes();
        byte[] result = null;
        StringBuffer buf = null;
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        result = new byte[md5.getDigestLength()];
        md5.reset();
        md5.update(buffer);
        result = md5.digest(tokensalt.getBytes());

        //create hex string from the 16-byte hash 
        buf = new StringBuffer(result.length * 2);
        for (int i = 0; i < result.length; i++) {
            int intVal = result[i] & 0xff;
            if (intVal < 0x10) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(intVal).toUpperCase());
        }
        return buf.toString().substring(0, 32);
    } catch (NoSuchAlgorithmException e) {
        throw new UIException("There were problems with the algorithum");
    }
}

From source file:com.gisgraphy.util.StringUtil.java

/**
 * Encode a string using algorithm specified in web.xml and return the
 * resulting encrypted password. If exception, the plain credentials string
 * is returned//  w  w  w  .ja  va2  s.co m
 * 
 * @param password
 *                Password or other credentials to use in authenticating
 *                this username
 * @param algorithm
 *                Algorithm used to do the digest
 * @return encypted password based on the algorithm.
 */
public static String encodePassword(String password, String algorithm) {
    byte[] unencodedPassword = password.getBytes();

    MessageDigest md = null;

    try {
        // first create an instance, given the provider
        md = MessageDigest.getInstance(algorithm);
    } catch (Exception e) {
        log.error("Exception: " + e);

        return password;
    }

    md.reset();

    // call the update method one or more times
    // (useful when you don't know the size of your data, eg. stream)
    md.update(unencodedPassword);

    // now calculate the hash
    byte[] encodedPassword = md.digest();

    StringBuffer buf = new StringBuffer();

    for (byte anEncodedPassword : encodedPassword) {
        if ((anEncodedPassword & 0xff) < 0x10) {
            buf.append("0");
        }

        buf.append(Long.toString(anEncodedPassword & 0xff, 16));
    }

    return buf.toString();
}

From source file:de.eod.jliki.users.utils.PasswordHashUtility.java

/**
 * Builds a hash from a password.<br/>
 * @param password the password//from   www.ja  va  2 s.com
 * @param salt the salt
 * @return the passwords hash
 */
public static String hashPassword(final String password, final byte[] salt) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance(ALGORITHM);
    } catch (final NoSuchAlgorithmException e) {
        LOGGER.fatal("Hash algorithm not found: " + ALGORITHM, e);
        return "";
    }
    digest.reset();
    digest.update(salt);
    byte[] hashBytes;
    try {
        hashBytes = digest.digest(password.getBytes(ENCODING));
    } catch (final UnsupportedEncodingException e) {
        LOGGER.fatal("Character encoding not supported: " + ENCODING, e);
        return "";
    }

    for (int i = 0; i < NUM_ITERAITONS; i++) {
        digest.reset();
        hashBytes = digest.digest(hashBytes);
    }

    final String hashString = Base64.encodeBase64String(hashBytes);
    return hashString;
}