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:octavio.server.User.java

/**
 * Retourne la chaine de caractres fournie en paramtre hashe avec
 * l'algorithme configur./*  ww w  . j  av a2  s  . c  o m*/
 *
 * @param password Mot de passe  hasher
 *
 * @return Mot de passe hash ou chane vide si algorithme indisponible
 */
public static String hashPassword(String password) {
    try {
        MessageDigest digest = MessageDigest.getInstance(Configuration.get("hash_algorithm", "SHA-1"));
        digest.reset();
        digest.update(password.getBytes());
        return new String(Hex.encodeHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        Log.debug(e);
        return password;
    }
}

From source file:com.all.dht.util.Digest.java

public static byte[] getSha1(byte[] bytes) {
    byte[] sha1 = null;
    try {/*from  www. j a  v  a 2  s  .c om*/
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(bytes);
        sha1 = md.digest();
        md.reset();
    } catch (Exception e) {
        LOG.fatal("Could not generate SHA1.", e);
    }
    return sha1;
}

From source file:com.linuxbox.enkive.archiver.AbstractMessageArchivingService.java

public static String calculateMessageId(Message message) throws CannotArchiveException {
    String messageUUID = null;/*from   w w  w . jav a 2 s .  c  om*/
    try {
        MessageDigest sha1calc = MessageDigest.getInstance("SHA-1");
        sha1calc.reset();
        messageUUID = new String(
                (new Hex()).encode(sha1calc.digest(message.getReconstitutedEmail().getBytes())));
    } catch (NoSuchAlgorithmException e) {
        throw new CannotArchiveException("Could not calculate UUID for message", e);
    } catch (IOException e) {
        throw new CannotArchiveException("Could not calculate UUID for message", e);
    }
    return messageUUID;
}

From source file:com.yilang.commons.utils.util.Digests.java

private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {//from   w w  w . ja v  a 2  s. co  m
        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:EncryptUtil.java

/**
 * Encode a string using algorithm specified in web.xml and return the
 * resulting encrypted password. If exception, the plain credentials string
 * is returned/*  ww  w.  j  a v  a  2  s  .  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 encode(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) {
        //logger.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();

    StringBuilder buf = new StringBuilder();

    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();
}

From source file:org.jessezhu.starriver.util.Digests.java

public static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {// w  ww  .  j  a va2 s.  c o  m
        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.exam.mserver.common.persistence.util.Digests.java

/**
 * , ?md5sha1.//from   w w w . j av  a2 s.  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) {
        throw new RuntimeException(e);
    }
}

From source file:com.idea.quickstart.common.security.Digests.java

/**
 * , ?md5sha1./*from w w w  .ja  v  a 2 s.  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) {
    }
    return null;
}

From source file:com.pfw.popsicle.common.Digests.java

/**
 * , ?md5sha1./* w ww.  j  a  va 2 s  . 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) {
        return null;
    }
}

From source file:com.cmri.bpt.common.util.DigestUtil.java

/**
 * , ?md5sha1./*from w w  w. ja  v a 2s.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) {
        throw ExceptionUtil.asUnChecked(e);
    }
}