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:jp.go.nict.langrid.management.web.utility.StringUtil.java

/**
 * /* w w  w  .  jav a 2 s.  c o  m*/
 * 
 */
public static String getUniqueString() {
    MessageDigest sha;
    try {
        sha = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
    sha.reset();
    sha.update(String.valueOf(Math.random()).getBytes());
    byte[] hash = sha.digest();
    StringBuffer sb = new StringBuffer();
    int cnt = hash.length;
    for (int i = 0; i < cnt; i++) {
        int d = hash[i];
        if (d < 0) {
            d += 256;
        }
        sb.append(Integer.toHexString(d & 0x0F));
    }
    return sb.toString();
}

From source file:controladores.ControladorArchivos.java

public static String getDigest(InputStream is, MessageDigest md, int byteArraySize)
        throws NoSuchAlgorithmException, IOException {

    md.reset();
    byte[] bytes = new byte[byteArraySize];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        md.update(bytes, 0, numBytes);/*from w w  w .  j  a  v a2  s  . c  om*/
    }
    byte[] digest = md.digest();
    String result = new String(Hex.encodeHex(digest));
    return result;
}

From source file:org.glite.slcs.caclient.impl.CMPRequest.java

private static byte[] makeProtection(String secret, int iterCount, String owfAlgId, String macAlgId,
        DEROctetString salt, PKIMessage message) {
    byte[] saltBytes = salt.getOctets();
    byte[] sharedSecret = secret.getBytes();
    byte[] firstKey = new byte[sharedSecret.length + saltBytes.length];
    for (int i = 0; i < sharedSecret.length; i++) {
        firstKey[i] = sharedSecret[i];//from  w ww.  j  a v a 2 s. c o m
    }
    for (int i = 0; i < saltBytes.length; i++) {
        firstKey[sharedSecret.length + i] = saltBytes[i];
    }
    // Construct the base key according to rfc4210, section 5.1.3.1
    MessageDigest dig = null;
    Mac mac = null;
    try {
        dig = MessageDigest.getInstance(owfAlgId, "BC");
        for (int i = 0; i < iterCount; i++) {
            firstKey = dig.digest(firstKey);
            dig.reset();
        }
        mac = Mac.getInstance(macAlgId, "BC");
        SecretKey key = new SecretKeySpec(firstKey, macAlgId);
        mac.init(key);
    } catch (Exception e) {
        log.error("Error while calculating PKIMessage protection", e);
    }
    mac.reset();
    byte[] protectedBytes = message.getProtectedBytes();
    mac.update(protectedBytes, 0, protectedBytes.length);
    return mac.doFinal();
}

From source file:com.lightbox.android.bitmap.BitmapUtils.java

private static String getMD5String(byte[] data) {
    MessageDigest md5 = null;
    try {/*from  w  w  w . ja v  a2s . co  m*/
        md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        md5.update(data);
        byte[] md5Hash = md5.digest();
        Formatter formatter = new Formatter();
        for (byte b : md5Hash) {
            formatter.format("%02x", b);
        }
        return formatter.toString();
    } catch (NoSuchAlgorithmException e1) {
        Log.w(TAG, e1);
    }

    return "";
}

From source file:li.barter.utils.Utils.java

/**
 * Makes an SHA1 Hash of the given string
 *
 * @param string The string to shash//  w w  w . ja  v a  2 s .  com
 * @return The hashed string
 * @throws NoSuchAlgorithmException
 */
public static String sha1(final String string) throws NoSuchAlgorithmException {
    final MessageDigest digest = MessageDigest.getInstance("SHA-1");
    digest.reset();
    final byte[] data = digest.digest(string.getBytes());
    return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data));
}

From source file:controller.InputSanitizer.java

private static String getDigest(InputStream is, MessageDigest md, int byteArraySize)
        throws NoSuchAlgorithmException, IOException {

    md.reset();
    byte[] bytes = new byte[byteArraySize];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        md.update(bytes, 0, numBytes);/*from  www.  j  av  a2  s . c  o  m*/
    }
    byte[] digest = md.digest();
    String result = new String(Hex.encodeHex(digest));
    return result;
}

From source file:info.magnolia.cms.security.SecurityUtil.java

/**
 * Message Digesting function.// w  w  w  . j  av  a 2  s .com
 *
 */
public static String getDigest(String data, String algorithm) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.reset();
    return new String(md.digest(data.getBytes()));
}

From source file:info.magnolia.cms.security.SecurityUtil.java

/**
 * Message Digesting function./*from   w ww  .j  a  v  a  2  s. co m*/
 *
 */
public static byte[] getDigest(byte[] data, String algorithm) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.reset();
    return md.digest(data);
}

From source file:CryptPassword.java

/**
 * Generates the MD5 hash from a given string.
 * //from  w ww .ja  v a2  s .co  m
 * @param inputString
 *            is the input String
 * @return the MD5 from the string used.
 */
public static String getMD5Hash(String inputString) {
    byte buf[] = inputString.getBytes();
    StringBuffer hexString = new StringBuffer();
    try {
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(buf);
        byte[] digest = algorithm.digest();
        for (int i = 0; i < digest.length; i++) {
            hexString.append(pad(Integer.toHexString(0xFF & digest[i]), 2));

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hexString.toString();
}

From source file:test.other.T_encrypt_password.java

public static String encodePassword(String password, String algorithm) {
    byte[] unencodedPassword = password.getBytes();

    MessageDigest md = null;

    try {/*from ww w. j  a va2 s  .  com*/
        // first create an instance, given the provider
        md = MessageDigest.getInstance(algorithm);
    } catch (Exception e) {
        //            mLogger.error("Exception: " + e);l
        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();
    System.err.println("bytes:" + encodedPassword);
    StringBuffer buf = new StringBuffer();

    for (int i = 0; i < encodedPassword.length; i++) {
        if ((encodedPassword[i] & 0xff) < 0x10) {
            buf.append("0");
        }

        buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
    }
    System.out.println(Base64.encodeToString(encodedPassword).getBytes().length);
    System.err.println("final : " + buf.toString().getBytes().length + "   " + buf.toString());
    return buf.toString();
}