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.mobilehelix.appserver.push.PushManager.java

private String getUniqueID(String clientid, String userid, Long appID)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    digest.reset();
    digest.update(clientid.getBytes("utf8"));
    digest.update(userid.getBytes("utf8"));
    digest.update(appID.toString().getBytes("utf8"));

    // Add in a random 8 bytes salt.
    byte saltb[] = new byte[8];
    srandom.nextBytes(saltb);//from w w  w  . j  a  v  a 2  s  . co  m
    digest.update(saltb);

    byte[] res = digest.digest();
    return new String(Hex.encodeHex(res, true));
}

From source file:org.ejbca.core.protocol.cmp.CmpPbeVerifyer.java

public boolean verify(String raAuthenticationSecret)
        throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException {
    lastUsedRaSecret = raAuthenticationSecret;
    boolean ret = false;
    // Verify the PasswordBased protection of the message
    if (!pAlg.getAlgorithm().equals(CMPObjectIdentifiers.passwordBasedMac)) {
        errMsg = INTRES.getLocalizedMessage("cmp.errorunknownprotalg", pAlg.getAlgorithm().getId());
        LOG.error(errMsg);/*from   w w  w.j a  v  a2 s. c  o m*/
        return ret;
    } else {
        if (iterationCount > 10000) {
            LOG.info("Received message with too many iterations in PBE protection: " + iterationCount);
            throw new InvalidKeyException("Iteration count can not exceed 10000");
        }
        byte[] raSecret = raAuthenticationSecret.getBytes();
        byte[] basekey = new byte[raSecret.length + salt.length];
        System.arraycopy(raSecret, 0, basekey, 0, raSecret.length);
        System.arraycopy(salt, 0, basekey, raSecret.length, salt.length);
        // Construct the base key according to rfc4210, section 5.1.3.1
        MessageDigest dig = MessageDigest.getInstance(owfOid, "BC");
        for (int i = 0; i < iterationCount; i++) {
            basekey = dig.digest(basekey);
            dig.reset();
        }
        // HMAC/SHA1 is normal 1.3.6.1.5.5.8.1.2 or 1.2.840.113549.2.7 
        Mac mac = Mac.getInstance(macOid, "BC");
        SecretKey key = new SecretKeySpec(basekey, macOid);
        mac.init(key);
        mac.reset();
        mac.update(protectedBytes, 0, protectedBytes.length);
        byte[] out = mac.doFinal();
        // My out should now be the same as the protection bits
        byte[] pb = protection.getBytes();
        ret = Arrays.equals(out, pb);
    }
    return ret;
}

From source file:org.jhub1.agent.randoms.Randoms.java

public String sha1(String str) {
    MessageDigest cript = null;
    try {//w  ww .  j  ava  2s  .  c om
        cript = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    cript.reset();
    try {
        cript.update(str.getBytes("utf8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new String(Hex.encodeHex(cript.digest()));
}

From source file:org.opensafety.hishare.util.implementation.EncryptionImpl.java

public byte[] hashPassword(String password, byte[] salt) throws CryptographyException {
    try {//from  www.  j  ava2s.c o m
        MessageDigest md = MessageDigest.getInstance(passwordHashAlgorithm);
        md.reset();
        md.update(salt);
        return md.digest(password.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
        throw new CryptographyException(e.getMessage());
    }
}

From source file:com.google.nigori.client.HashMigoriDatastore.java

private byte[] generateHash(byte[] value, byte[] idBytes) throws NigoriCryptographyException {
    byte[] toHash = new byte[value.length + idBytes.length];
    System.arraycopy(value, 0, toHash, 0, value.length);
    System.arraycopy(idBytes, 0, toHash, value.length, idBytes.length);

    MessageDigest crypt;
    try {//ww w.  ja  v  a 2 s. c om
        crypt = MessageDigest.getInstance(NigoriConstants.A_REVHASH);

        crypt.reset();
        crypt.update(toHash);
        byte[] hashBytes = crypt.digest();
        byte[] revBytes = new byte[HASH_SIZE + idBytes.length];
        System.arraycopy(hashBytes, 0, revBytes, 0, HASH_SIZE);
        System.arraycopy(idBytes, 0, revBytes, HASH_SIZE, idBytes.length);
        return revBytes;
    } catch (NoSuchAlgorithmException e) {
        throw new NigoriCryptographyException(e);
    }
}

From source file:fi.okm.mpass.idp.authn.impl.AbstractSpringSocialOAuth2Identity.java

/**
 * Returns redirect url for authentication.
 * //from   ww w .  ja va 2  s.c  o  m
 * @param httpRequest
 *            the request
 * 
 * @return redirect url
 */
public String getRedirectUrl(HttpServletRequest httpRequest) {
    log.trace("Entering");
    if (httpRequest == null) {
        log.trace("Leaving");
        return null;
    }
    OAuth2Parameters params = new OAuth2Parameters();
    if (scope != null) {
        params.setScope(scope);
    }
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.reset();
        md.update(httpRequest.getSession().getId().getBytes());
        String digest = new String(Hex.encode(md.digest()));
        params.setState(digest);
    } catch (NoSuchAlgorithmException e) {
        log.error("Unable to generate state");
        log.error("Something bad happened " + e.getMessage());
        log.trace("Leaving");
        return null;
    }
    params.setRedirectUri(httpRequest.getRequestURL().toString());
    String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, params);
    log.trace("Leaving");
    return authorizeUrl;

}

From source file:org.craftercms.commons.crypto.SimpleDigestTest.java

@Test
public void testDigest() throws Exception {
    SimpleDigest digest = new SimpleDigest();

    String hash = digest.digestBase64(CLEAR_TEXT);
    String salt = digest.getBase64Salt();

    MessageDigest actualDigest = MessageDigest.getInstance(SimpleDigest.DEFAULT_ALGORITHM);

    actualDigest.update(Base64.decodeBase64(salt));

    byte[] hashedBytes = actualDigest.digest(CLEAR_TEXT.getBytes("UTF-8"));

    for (int i = 0; i < SimpleDigest.DEFAULT_ITERATIONS; i++) {
        actualDigest.reset();

        hashedBytes = actualDigest.digest(hashedBytes);
    }/*from  ww w  . j a  va 2  s.c  o  m*/

    assertArrayEquals(hashedBytes, Base64.decodeBase64(hash));
}

From source file:com.intelligentz.appointmentz.controllers.register.java

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    MessageDigest messageDigest = null;
    try {/*from w  w  w .  ja va2s .  c  om*/
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(register.class.getName()).log(Level.SEVERE, "Error:{0}", ex.toString());
    }
    messageDigest.reset();
    messageDigest.update(req.getParameter("form-password").getBytes(Charset.forName("UTF8")));
    final byte[] resultByte = messageDigest.digest();
    final String password = new String(Hex.encodeHex(resultByte));

    String userName = req.getParameter("form-hospital-id");
    String id = req.getParameter("form-id");
    //String password = req.getParameter("form-password");
    String hospitalName = req.getParameter("form-hospital-name");
    register(id, userName, password, hospitalName, req, res);

}

From source file:com.dtolabs.rundeck.core.resources.URLResourceModelSource.java

private String hashURL(final String url) {
    try {// w  w  w  .j  a  va2s. c  o  m
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(url.getBytes(Charset.forName("UTF-8")));
        return new String(Hex.encodeHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return Integer.toString(url.hashCode());
}

From source file:com.intelligentz.appointmentz.controllers.authenticate.java

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    MessageDigest messageDigest = null;
    try {//from w ww .  j  av a  2s  .co  m
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(authenticate.class.getName()).log(Level.SEVERE, "Error:{0}", ex.toString());
    }
    messageDigest.reset();
    messageDigest.update(req.getParameter("form-password").getBytes(Charset.forName("UTF8")));
    final byte[] resultByte = messageDigest.digest();
    final String password = new String(Hex.encodeHex(resultByte));

    String userName = req.getParameter("form-username");
    //String password = req.getParameter("form-password");
    authenticate(userName, password, req, res);

}