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.codehaus.mojo.mrm.impl.digest.SHA1DigestFileEntry.java

/**
 * Generates the digest./* ww w.j a  va2 s . co m*/
 *
 * @return the digest.
 * @throws IOException if the backing entry could not be read.
 * @since 1.0
 */
private byte[] getContent() throws IOException {
    InputStream is = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        digest.reset();
        byte[] buffer = new byte[8192];
        int read;
        try {
            is = entry.getInputStream();
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
        } catch (IOException e) {
            if (is != null) {
                throw e;
            }
        }
        final String md5 = StringUtils.leftPad(new BigInteger(1, digest.digest()).toString(16), 40, "0");
        return md5.getBytes();
    } catch (NoSuchAlgorithmException e) {
        IOException ioe = new IOException("Unable to calculate hash");
        ioe.initCause(e);
        throw ioe;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:ti.modules.titanium.utils.UtilsModule.java

@Kroll.method
public String sha256(Object obj) {
    String data = convertToString(obj);
    // NOTE: DigestUtils with the version before 1.4 doesn't have the function sha256Hex,
    // so we deal with it ourselves
    try {/*from   ww w .ja  v  a2 s. co  m*/
        byte[] b = data.getBytes();
        MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
        algorithm.reset();
        algorithm.update(b);
        byte messageDigest[] = algorithm.digest();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < messageDigest.length; i++) {
            result.append(Integer.toString((messageDigest[i] & 0xff) + 0x100, 16).substring(1));
        }
        return result.toString();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "SHA256 is not a supported algorithm");
    }
    return null;
}

From source file:org.apache.xml.security.utils.SignerOutputStream.java

/** @inheritDoc */
public void write(byte[] arg0, int arg1, int arg2) {
    if (log.isDebugEnabled()) {
        log.debug("Canonicalized SignedInfo:");
        StringBuilder sb = new StringBuilder(arg2);
        for (int i = arg1; i < (arg1 + arg2); i++) {
            sb.append((char) arg0[i]);
        }/* w  w  w . j  ava  2  s  .  c  o m*/
        log.debug(sb.toString());
    }
    try {

        System.err.println("SignerOutputStream.write():" + new String(arg0, arg1, arg2));

        try {
            MessageDigest hash = MessageDigest.getInstance("SHA-256", "BC");
            hash.reset();
            byte[] toHash = new byte[arg2];
            System.arraycopy(arg0, arg1, toHash, 0, arg2);
            // toHash =
            // Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS).canonicalize(toHash);
            EbicsUtil.saveToFile(toHash, new File("toHash.bin"));
            ByteArrayOutputStream o = new ByteArrayOutputStream();
            for (int i = 0; i < toHash.length; i++) {
                byte b = toHash[i];
                // if (b != 26 && b != 10 && b != 13) {
                o.write(b);
                // }
            }
            o.close();
            // toHash = o.toByteArray();
            EbicsUtil.saveToFile(toHash, new File("toHash.bin"));
            byte[] r = hash.digest(toHash);
            // r = hash.digest(toHash);
            System.err.println("== toHash");
            System.err.println(new String(toHash));
            System.err.println(HexDump.toHex(toHash));
            System.err.println("== r");
            System.err.println(HexDump.toHex(r));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sa.update(arg0, arg1, arg2);
    } catch (XMLSignatureException e) {
        throw new RuntimeException("" + e);
    }
}

From source file:pl.psnc.synat.wrdz.zu.authentication.AuthenticationHelper.java

/**
 * Hashes the user's password with the salt.
 * //from www.  jav  a2 s . c  om
 * @param password
 *            user's password.
 * @param salt
 *            salt for a user
 * @return hashed user's password.
 * @throws CharacterCodingException
 *             when a character encoding error occurs
 */
public char[] hashPassword(char[] password, byte[] salt) throws CharacterCodingException {
    byte[] bytes = ArrayUtils.addAll(
            Charset.forName(configuration.getPasswordCharset()).encode(CharBuffer.wrap(password)).array(),
            salt);
    try {
        MessageDigest messageDigest = MessageDigest.getInstance(configuration.getPasswordDigestAlgorithm());
        messageDigest.reset();
        bytes = messageDigest.digest(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new WrdzRuntimeException(
                "Unsupported digest algorithm " + configuration.getPasswordDigestAlgorithm());
    }
    if (HEX.equalsIgnoreCase(configuration.getPasswordEncoding())) {
        return hexEncode(bytes);
    } else if (BASE64.equalsIgnoreCase(configuration.getPasswordEncoding())) {
        return base64Encode(bytes);
    } else { // no encoding specified
        return Charset.forName(configuration.getPasswordCharset()).decode(ByteBuffer.wrap(bytes)).array();
    }
}

From source file:org.kuali.continuity.admin.dao.jpa.UserLoginDAOImpl.java

private String getPasswordHash(String password) {
    if (password == null || password.trim().length() == 0)
        return password;
    try {// w ww .ja v a  2  s.  c o  m
        MessageDigest digest = MessageDigest.getInstance("SHA");
        digest.reset();
        byte[] input = digest.digest(password.getBytes());
        return Hex.encodeHexString(input).toUpperCase();
    } catch (NoSuchAlgorithmException e) {
        // TODO: refactor
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:de.fhg.igd.mapviewer.cache.FileTileCache.java

/**
 * Compute a secure hash for the given string
 * //from w ww.  j  av a 2  s  .  com
 * @param password the string
 * 
 * @return the hash of the string as HEX string
 * @throws NoSuchAlgorithmException if the SHA-1 algorithm could not be
 *             found
 */
protected String computeHash(String password) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    digest.reset();
    digest.update(password.getBytes());
    return byteArrayToHexString(digest.digest());
}

From source file:org.oscarehr.common.dao.RemoteIntegratedDataCopyDao.java

/**
 * /* w  w w  .  j ava 2  s . co  m*/
 * @param demographicNo
 * @param obj
 * @param providerNo
 * @param facilityId
 * @return Returns null if it already existed in the database.
 * @throws Exception
 */
public RemoteIntegratedDataCopy save(Integer demographicNo, Object obj, String providerNo, Integer facilityId,
        String type) throws Exception {

    if (obj == null) {
        throw new Exception("Can't save null");
    }
    if (type == null) {
        type = "";
    } else {
        type = "+" + type;
    }

    String dataType = obj.getClass().getName() + type;
    String marshalledObject = ObjectMarshalUtil.marshalToString(obj);

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.reset();
    byte[] digest = md.digest(marshalledObject.getBytes("UTF-8"));
    String signature = new String(Base64.encodeBase64(digest), MiscUtils.DEFAULT_UTF8_ENCODING);

    MiscUtils.getLogger().debug("demo :" + demographicNo + " dataType : " + dataType + " Signature: "
            + signature + " providerNo " + providerNo + " facilityId " + facilityId);

    RemoteIntegratedDataCopy remoteIntegratedDataCopy = this.findByDemoTypeSignature(facilityId, demographicNo,
            dataType, signature);

    if (remoteIntegratedDataCopy == null) {
        RemoteIntegratedDataCopy rid = new RemoteIntegratedDataCopy();
        rid.setDemographicNo(demographicNo);
        rid.setDataType(dataType);
        rid.setData(marshalledObject);
        rid.setSignature(signature);
        rid.setProviderNo(providerNo);
        rid.setFacilityId(facilityId);
        this.persist(rid);
        archiveDataCopyExceptThisOne(rid);//Set all other notes besides this one to archived.
        return rid;
    }
    return null;

}

From source file:org.sglover.alfrescoextensions.common.HasherImpl.java

private String getHash(ByteBuffer bytes, int start, int end, MessageDigest digest)
        throws NoSuchAlgorithmException {
    int saveLimit = bytes.limit();
    bytes.limit(end + 1);/*  w  ww.ja  va 2  s .  c om*/

    bytes.mark();
    bytes.position(start);

    digest.reset();
    digest.update(bytes);
    byte[] array = digest.digest();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < array.length; ++i) {
        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
    }

    bytes.limit(saveLimit);
    bytes.reset();

    return sb.toString();
}

From source file:com.ethercamp.harmony.keystore.KeystoreFormat.java

private byte[] sha3(byte[] h) throws NoSuchAlgorithmException {
    MessageDigest KECCAK = new Keccak.Digest256();
    KECCAK.reset();
    KECCAK.update(h);//from w w w. ja  v a2s .  com
    return KECCAK.digest();
}

From source file:org.gradle.api.changedetection.digest.DefaultDigesterCache.java

public MessageDigest getDigester(String cacheId) {
    if (StringUtils.isEmpty(cacheId))
        throw new IllegalArgumentException("cacheId is empty!");

    MessageDigest digester = cache.get(cacheId);
    if (digester == null) {
        digester = digesterFactory.createDigester();
        cache.put(cacheId, digester);//  w  w w.jav  a 2 s . co m
    } else {
        digester.reset();
    }

    return digester;
}