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:eionet.gdem.utils.Utils.java

/**
 *
 * @param srcBytes/*from  w  w w.  j  a  v  a  2 s .  c  om*/
 * @param algorithm
 * @return
 * @throws Exception
 */
public static String digest(byte[] srcBytes, String algorithm) throws Exception {

    byte[] dstBytes = new byte[16];

    MessageDigest md;

    md = MessageDigest.getInstance(algorithm);

    md.update(srcBytes);
    dstBytes = md.digest();
    md.reset();

    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < dstBytes.length; i++) {
        Byte byteWrapper = new Byte(dstBytes[i]);
        int k = byteWrapper.intValue();
        String s = Integer.toHexString(k);
        if (s.length() == 1) {
            s = "0" + s;
        }
        buf.append(s.substring(s.length() - 2));
    }

    return buf.toString();
}

From source file:org.jspresso.framework.application.backend.action.security.AbstractChangePasswordAction.java

/**
 * Hashes a char array using the algorithm parametrised in the instance.
 *
 * @param newPassword/*  w w  w .ja  va2 s .co m*/
 *          the new password to hash.
 * @return the password digest.
 * @throws NoSuchAlgorithmException
 *           when the digest algorithm is not supported.
 * @throws IOException
 *           whenever an I/O exception occurs.
 */
protected String digestAndEncode(char... newPassword) throws NoSuchAlgorithmException, IOException {
    if (getDigestAlgorithm() != null) {
        MessageDigest md = MessageDigest.getInstance(getDigestAlgorithm());
        md.reset();
        md.update(new String(newPassword).getBytes(StandardCharsets.UTF_8.name()));

        byte[] digest = md.digest();
        return getPasswordStorePrefix() + encode(digest);
    }
    return new String(newPassword);
}

From source file:com.omnigon.aem.handlebars.helpers.UniqueIdHelper.java

private String generateUniqueId() {
    byte[] bytesDirectoryPath;
    MessageDigest md;
    try {//  w  ww . ja v  a  2s. c  o m
        bytesDirectoryPath = UUID.randomUUID().toString().getBytes(CharEncoding.UTF_8);
        md = MessageDigest.getInstance(MESSAGE_DIGEST_TYPE);
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        LOG.error(e.getMessage(), e);
        return StringUtils.EMPTY;
    }
    md.reset();
    md.update(bytesDirectoryPath);

    String uniqueId = DatatypeConverter.printHexBinary(md.digest());
    String formattedUniqueId = formatUniqueId(uniqueId);

    return formattedUniqueId;
}

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

protected static PKIMessage protectPKIMessage(PKIMessage msg, boolean badObjectId, String password,
        String keyId, int iterations)
        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException {
    // Create the PasswordBased protection of the message
    PKIHeaderBuilder head = CmpMessageHelper.getHeaderBuilder(msg.getHeader());
    if (keyId != null) {
        head.setSenderKID(new DEROctetString(keyId.getBytes()));
    }/* ww  w  . j  ava 2  s  .c om*/
    // SHA1
    AlgorithmIdentifier owfAlg = new AlgorithmIdentifier(new ASN1ObjectIdentifier("1.3.14.3.2.26"));
    // 567 iterations
    int iterationCount = iterations;
    ASN1Integer iteration = new ASN1Integer(iterationCount);
    // HMAC/SHA1
    AlgorithmIdentifier macAlg = new AlgorithmIdentifier(new ASN1ObjectIdentifier("1.2.840.113549.2.7"));
    byte[] salt = "foo123".getBytes();
    DEROctetString derSalt = new DEROctetString(salt);

    // Create the new protected return message
    String objectId = "1.2.840.113533.7.66.13";
    if (badObjectId) {
        objectId += ".7";
    }
    PBMParameter pp = new PBMParameter(derSalt, owfAlg, iteration, macAlg);
    AlgorithmIdentifier pAlg = new AlgorithmIdentifier(new ASN1ObjectIdentifier(objectId), pp);
    head.setProtectionAlg(pAlg);
    PKIHeader header = head.build();
    // Calculate the protection bits
    byte[] raSecret = password.getBytes();
    byte[] basekey = new byte[raSecret.length + salt.length];
    System.arraycopy(raSecret, 0, basekey, 0, raSecret.length);
    for (int i = 0; i < salt.length; i++) {
        basekey[raSecret.length + i] = salt[i];
    }
    // Construct the base key according to rfc4210, section 5.1.3.1
    MessageDigest dig = MessageDigest.getInstance(owfAlg.getAlgorithm().getId(), "BC");
    for (int i = 0; i < iterationCount; i++) {
        basekey = dig.digest(basekey);
        dig.reset();
    }
    // For HMAC/SHA1 there is another oid, that is not known in BC, but the
    // result is the same so...
    String macOid = macAlg.getAlgorithm().getId();
    PKIBody body = msg.getBody();
    byte[] protectedBytes = CmpMessageHelper.getProtectedBytes(header, body);
    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();
    DERBitString bs = new DERBitString(out);

    return new PKIMessage(header, body, bs);
}

From source file:com.eventattend.portal.bl.FaceBookBL.java

private static String MD5(String email) {
    String md5val = "";
    String[] args = { email };/*from   w w  w  . ja v a  2  s. c o m*/
    MessageDigest algorithm = null;

    try {
        algorithm = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException nsae) {
        System.out.println("Cannot find digest algorithm");
        System.exit(1);
    }
    for (String arg : args) {
        byte[] defaultBytes = arg.getBytes();
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();
        StringBuffer hexString = new StringBuffer();

        for (int i = 0; i < messageDigest.length; i++) {
            String hex = Integer.toHexString(0xFF & messageDigest[i]);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        md5val = hexString.toString();
        System.out.println("MD5 (" + arg + ") = " + md5val);
    }

    return md5val;
}

From source file:test.frames.CryptoServiceSingleton.java

/**
 * Build an hexadecimal MD5 hash for a String.
 *
 * @param value The String to hash/*w w  w.  ja v a2s  .  com*/
 * @return An hexadecimal Hash
 */
public String hexMD5(String value) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance(Hash.MD5.toString());
        messageDigest.reset();
        messageDigest.update(value.getBytes(UTF_8));
        byte[] digest = messageDigest.digest();
        return String.valueOf(Hex.encodeHex(digest));
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.googlecode.fascinator.authentication.internal.InternalAuthentication.java

/**
 * Password encryption method//ww  w.  j  a va  2 s.co  m
 * 
 * @param password Password to be encrypted
 * @return encrypted password
 * @throws AuthenticationException if fail to encrypt
 */
private String encryptPassword(String password) throws AuthenticationException {
    byte[] passwordBytes = password.getBytes();

    try {
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(passwordBytes);

        byte messageDigest[] = algorithm.digest();
        BigInteger number = new BigInteger(1, messageDigest);

        password = number.toString(16);
        if (password.length() == 31) {
            password = "0" + password;
        }
    } catch (Exception e) {
        throw new AuthenticationException("Internal password encryption failure: " + e.getMessage());
    }
    return password;
}

From source file:org.jflicks.tv.programdata.sd.json.Manage.java

public Client connect(String user, String password) throws NoSuchAlgorithmException {

    Client result = null;/*from  w  ww.  j  av  a2 s. c o  m*/

    if ((user != null) && (password != null)) {

        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();
        md.update(password.getBytes());
        String sha = new String(Hex.encodeHex(md.digest()));

        Client c = new Client();
        if (c.doToken(user, sha)) {

            if (c.doStatus()) {

                System.err.println("status ok\n");
                result = c;
            }
        }
    }

    return (result);
}

From source file:sugarcrm.SugarCRMRest.java

private String genPasswordMD5(String password) {
    String result = "";

    try {//from w w  w  .j  a v  a2  s . c o  m
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        byte[] mdbytes = md5.digest(password.getBytes());

        for (int i = 0; i <= mdbytes.length - 1; i++) {
            String t = Integer.toHexString(0xFF & mdbytes[i]);
            if (t.length() == 1) {
                t = String.format("0%s", t);
            }

            result += t;
        }
    } catch (Exception exp) {
        exp.printStackTrace();
        result = null;
    }

    print("Password MD5: " + result);
    return result;
}

From source file:com.google.reducisaurus.servlets.BaseServlet.java

private String getKeyForContents(final String filecontents) throws ServletException {
    MessageDigest sha1;
    try {//from  www. jav  a 2 s.com
        sha1 = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new ServletException(e);
    }
    byte[] hashValue = sha1.digest(filecontents.getBytes());
    sha1.reset();
    return new String(hashValue);
}