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:ch.unibe.cde.geonet.kernel.security.Md5PasswordEncoder.java

@Override
public boolean matches(CharSequence rawSequence, String encodedPassword) {
    try {// w w w  . j ava 2 s .co  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(rawSequence.toString().getBytes("UTF-8"));
        return ("md5:" + Md5PasswordEncoder.byteToHex(md.digest())).equals(encodedPassword);
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
        Log.error(Log.JEEVES, ex);
    }
    return false;
}

From source file:org.zaproxy.zap.extension.fuzz.payloads.processor.AbstractStringHashProcessor.java

@Override
public DefaultPayload process(DefaultPayload payload) {
    MessageDigest messageDigest = getMessageDigest();
    messageDigest.reset();
    messageDigest.update(payload.getValue().getBytes(getCharset()));
    payload.setValue(new String(HEX_ASCII.encodeHex(messageDigest.digest(), !upperCase)));
    return payload;
}

From source file:com.ocpsoft.socialpm.util.crypt.MD5PasswordEncryptor.java

@Override
public String encodePassword(final String password, final Object salt) {
    try {//  w  w  w.j  a  v a2s. c om
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(salt.toString().getBytes());
        byte[] passwordHash = digest.digest(password.getBytes());

        Base64 encoder = new Base64();
        byte[] encoded = encoder.encode(passwordHash);
        return new String(encoded);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.owasp.webgoat.lessons.Encoding.java

public static String hashSHASalt(int iterationNb, String str) {
    byte[] b = str.getBytes();
    MessageDigest md = null;
    byte[] input = null;

    try {//  w  w w.j  a va  2 s.  c  o  m
        md = MessageDigest.getInstance("SHA-256");
        input = md.digest(str.getBytes("UTF-8"));
        md.update(salt);
        for (int i = 0; i < iterationNb; i++) {
            md.reset();
            input = md.digest(input);
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // it's got to be there
        e.printStackTrace();
    }
    return (base64Encode(input));
}

From source file:org.eurekastreams.server.service.actions.strategies.HashGeneratorStrategy.java

/**
 * Hashs an input./*from  ww  w  .ja  va  2s .c  o  m*/
 * 
 * @param inInput
 *            the input string.
 * @return the output string.
 */
public String hash(final String inInput) {
    Random generator = new Random();
    String input = inInput + Integer.toString(generator.nextInt());

    try {
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(input.getBytes());
        byte[] messageDigest = algorithm.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(TWO_FIFTY_FIVE & messageDigest[i]));
        }

        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        log.error("hash failed");
        return null;
    }
}

From source file:netinf.common.security.impl.SignatureAlgorithmImpl.java

@Override
public String hash(String originalString, String hashFunction) throws NoSuchAlgorithmException {
    MessageDigest algorithm = MessageDigest.getInstance(hashFunction);
    algorithm.reset();
    algorithm.update(originalString.getBytes());
    byte[] messageDigest = algorithm.digest();
    return Utils.hexStringFromBytes(messageDigest);
}

From source file:org.apache.ws.security.message.token.UsernameToken.java

/**
 * This static method generates a derived key as defined in WSS Username
 * Token Profile./*from ww  w . j  a  v a  2  s  . c  o  m*/
 * 
 * @param password The password to include in the key generation
 * @param salt The Salt value
 * @param iteration The Iteration value. If zero (0) is given the method uses the
 *                  default value
 * @return Returns the derived key a byte array
 * @throws WSSecurityException
 */
public static byte[] generateDerivedKey(byte[] password, byte[] salt, int iteration)
        throws WSSecurityException {
    if (iteration == 0) {
        iteration = DEFAULT_ITERATION;
    }

    byte[] pwSalt = new byte[salt.length + password.length];
    System.arraycopy(password, 0, pwSalt, 0, password.length);
    System.arraycopy(salt, 0, pwSalt, password.length, salt.length);

    MessageDigest sha = null;
    try {
        sha = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        if (DO_DEBUG) {
            LOG.debug(e.getMessage(), e);
        }
        throw new WSSecurityException(WSSecurityException.FAILURE, "noSHA1availabe", null, e);
    }
    sha.reset();

    //
    // Make the first hash round with start value
    //
    byte[] K = sha.digest(pwSalt);
    //
    // Perform the 1st up to iteration-1 hash rounds
    //
    for (int i = 1; i < iteration; i++) {
        K = sha.digest(K);
    }
    return K;
}

From source file:com.imaginea.kodebeagle.base.util.Utils.java

@NotNull
public String getDigestAsString(final String trimmedFileName)
        throws UnsupportedEncodingException, NoSuchAlgorithmException {
    MessageDigest crypt = MessageDigest.getInstance("SHA-1");
    crypt.reset();
    crypt.update(trimmedFileName.getBytes(StandardCharsets.UTF_8));
    // We think 10 chars is safe enough to rely on.
    return Hex.encodeHexString(crypt.digest()).substring(0, 10);
}

From source file:com.cloud.server.auth.MD5UserAuthenticator.java

@Override
public String encode(final String password) {
    try {//from   w  w w.  j  a v a  2  s. c o  m
        final MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        BigInteger pwInt = new BigInteger(1, md5.digest(password.getBytes()));
        String pwStr = pwInt.toString(16);
        int padding = 32 - pwStr.length();
        StringBuilder sb = new StringBuilder(32);
        for (int i = 0; i < padding; i++) {
            sb.append('0'); // make sure the MD5 password is 32 digits long
        }
        sb.append(pwStr);
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new CloudRuntimeException("Unable to hash password", e);
    }

}

From source file:com.sun.socialsite.util.Utilities.java

/**
 * Encode a string using algorithm specified in web.xml and return the
 * resulting encrypted password. If exception, the plain credentials
 * string is returned/*from w ww .j a  v a2s.c  o 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) {
        mLogger.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 (int i = 0; i < encodedPassword.length; i++) {
        if ((encodedPassword[i] & 0xff) < 0x10) {
            buf.append("0");
        }

        buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
    }

    return buf.toString();
}