Example usage for java.security MessageDigest clone

List of usage examples for java.security MessageDigest clone

Introduction

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

Prototype

public Object clone() throws CloneNotSupportedException 

Source Link

Document

Returns a clone if the implementation is cloneable.

Usage

From source file:lucee.commons.digest.Hash.java

public static String hash(String str, String algorithm, int numIterations, char[] encoding)
        throws NoSuchAlgorithmException {
    try {//from   w  ww .java2s .  c o  m
        MessageDigest md = MessageDigest.getInstance(algorithm), mdc;
        for (int i = 0; i < numIterations; i++) {
            mdc = (MessageDigest) md.clone();
            mdc.reset();
            mdc.update(toBytes(str, CharsetUtil.UTF8));
            str = new String(enc(mdc.digest(), encoding));
        }
        return str;
    } catch (CloneNotSupportedException e) {
    }

    // if not possible to clone the MessageDigest create always a new instance
    for (int i = 0; i < numIterations; i++) {
        str = hash(str, algorithm, encoding, CharsetUtil.UTF8);
    }
    return str;
}

From source file:com.squarespace.gibson.GibsonUtils.java

/**
 * Creates and returns a {@link MessageDigestFactory} for the given algorithm.
 *///w w  w  .j  av  a  2s  .c  o m
private static MessageDigestFactory newMessageDigestFactory(final String algorithm) {

    final MessageDigest md = DigestUtils.getDigest(algorithm);

    return new MessageDigestFactory() {
        @Override
        public MessageDigest newMessageDigest() {
            // It is faster to clone a MessageDigest than to look it up via SPI.
            try {
                return (MessageDigest) md.clone();
            } catch (CloneNotSupportedException err) {
                LOG.warn(Gibson.MARKER, "CloneNotSupportedException: {}", algorithm, err);
            }

            return DigestUtils.getDigest(algorithm);
        }
    };
}