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:acp.sdk.SecureUtil.java

/**
 * md5./*from w  ww  . j a v a  2 s . c om*/
 * 
 * @param datas
 *            ?
 * @return 
 */
public static byte[] md5(byte[] datas) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance(ALGORITHM_MD5);
        md.reset();
        md.update(datas);
        return md.digest();
    } catch (Exception e) {
        LogUtil.writeErrorLog("MD5", e);
        return null;
    }
}

From source file:acp.sdk.SecureUtil.java

/**
 * sha1./*w  w w  .  j av a2 s . com*/
 * 
 * @param datas
 *            ?
 * @return 
 */
public static byte[] sha1(byte[] data) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance(ALGORITHM_SHA1);
        md.reset();
        md.update(data);
        return md.digest();
    } catch (Exception e) {
        LogUtil.writeErrorLog("SHA1", e);
        return null;
    }
}

From source file:org.dcm4chex.archive.hsm.VerifyTar.java

public static Map<String, byte[]> verify(InputStream in, String tarname, byte[] buf,
        ArrayList<String> objectNames) throws IOException, VerifyTarException {
    TarInputStream tar = new TarInputStream(in);
    try {/*from  www . j a v  a  2 s  .  c  o m*/
        log.debug("Verify tar file: {}", tarname);
        TarEntry entry = tar.getNextEntry();
        if (entry == null)
            throw new VerifyTarException("No entries in " + tarname);
        String entryName = entry.getName();
        if (!"MD5SUM".equals(entryName))
            throw new VerifyTarException("Missing MD5SUM entry in " + tarname);
        BufferedReader dis = new BufferedReader(new InputStreamReader(tar));

        HashMap<String, byte[]> md5sums = new HashMap<String, byte[]>();
        String line;
        while ((line = dis.readLine()) != null) {
            char[] c = line.toCharArray();
            byte[] md5sum = new byte[16];
            for (int i = 0, j = 0; i < md5sum.length; i++, j++, j++) {
                md5sum[i] = (byte) ((fromHexDigit(c[j]) << 4) | fromHexDigit(c[j + 1]));
            }
            md5sums.put(line.substring(34), md5sum);
        }
        Map<String, byte[]> entries = new HashMap<String, byte[]>(md5sums.size());
        entries.putAll(md5sums);
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        while ((entry = tar.getNextEntry()) != null) {
            entryName = entry.getName();
            log.debug("START: Check MD5 of entry: {}", entryName);
            if (objectNames != null && !objectNames.remove(entryName))
                throw new VerifyTarException(
                        "TAR " + tarname + " contains entry: " + entryName + " not in file list");
            byte[] md5sum = (byte[]) md5sums.remove(entryName);
            if (md5sum == null)
                throw new VerifyTarException("Unexpected TAR entry: " + entryName + " in " + tarname);
            digest.reset();
            in = new DigestInputStream(tar, digest);
            while (in.read(buf) > 0)
                ;
            if (!Arrays.equals(digest.digest(), md5sum)) {
                throw new VerifyTarException("Failed MD5 check of TAR entry: " + entryName + " in " + tarname);
            }
            log.debug("DONE: Check MD5 of entry: {}", entryName);
        }
        if (!md5sums.isEmpty())
            throw new VerifyTarException("Missing TAR entries: " + md5sums.keySet() + " in " + tarname);
        if (objectNames != null && !objectNames.isEmpty())
            throw new VerifyTarException(
                    "Missing TAR entries from object list: " + objectNames.toString() + " in " + tarname);
        return entries;
    } finally {
        tar.close();
    }
}

From source file:com.wxsoft.framework.util.unionpay.SecureUtil.java

/**
 * md5./* w w  w  .  j ava2 s. co m*/
 * 
 * @param datas
 *            ?
 * @return 
 */
public static byte[] md5(byte[] datas) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance(ALGORITHM_MD5);
        md.reset();
        md.update(datas);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("MD5", e);
        return null;
    }
}

From source file:com.wxsoft.framework.util.unionpay.SecureUtil.java

/**
 * sha1./*from w w w . ja v  a2 s.  c  o  m*/
 * 
 * @param datas
 *            ?
 * @return 
 */
public static byte[] sha1(byte[] data) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance(ALGORITHM_SHA1);
        md.reset();
        md.update(data);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("SHA1", e);
        return null;
    }
}

From source file:hudson.Util.java

/**
 * Converts a string into 128-bit AES key.
 * @since 1.308//from   w  w  w . ja v  a  2 s . c  o  m
 */
public static SecretKey toAes128Key(String s) {
    try {
        // turn secretKey into 256 bit hash
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.reset();
        digest.update(s.getBytes("UTF-8"));

        // Due to the stupid US export restriction JDK only ships 128bit version.
        return new SecretKeySpec(digest.digest(), 0, 128 / 8, "AES");
    } catch (NoSuchAlgorithmException e) {
        throw new Error(e);
    } catch (UnsupportedEncodingException e) {
        throw new Error(e);
    }
}

From source file:com.trsst.Common.java

/**
 * Calculates the SHA-256 hash of the given byte range, and then hashes the
 * resulting hash again. This is standard procedure in Bitcoin. The
 * resulting hash is in big endian form. Borrowed from bitcoinj.
 *//*from  ww  w  .  jav a  2s.co  m*/
private static final byte[] doubleDigest(byte[] input, int offset, int length) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        log.error("Should never happen: could not find SHA-256 MD algorithm", e);
        return null;
    }
    digest.reset();
    digest.update(input, offset, length);
    byte[] first = digest.digest();
    return digest.digest(first);
}

From source file:org.ligoj.app.http.security.PasswordGeneration.java

@Test
public void test() throws NoSuchAlgorithmException {
    final MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();

    // user//w  w w  .  j a  va 2  s.  co  m
    System.out.println(Base64.encodeBase64String(digest.digest("password".getBytes(StandardCharsets.UTF_8))));

}

From source file:Gestores.GestorHash.java

public String md5(String plaintext) {
    String hashtext = "";
    try {/*from   w w  w  .j av  a  2s  .  co m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(plaintext.getBytes());
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        hashtext = bigInt.toString(16);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(GestorHash.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hashtext;
}

From source file:ch.unibe.cde.geonet.kernel.security.Md5PasswordEncoder.java

@Override
public String encode(CharSequence cs) {
    try {/*from ww  w  . j  a  v  a2s  . co  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(cs.toString().getBytes("UTF-8"));
        return "md5:" + Md5PasswordEncoder.byteToHex(md.digest());
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
        Log.error(Log.JEEVES, ex);
    }
    return null;
}