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:Main.java

private static byte[] computeHash(String x) {
    java.security.MessageDigest d = null;
    try {/*from   ww  w  .  j a v a2 s  . co  m*/
        d = java.security.MessageDigest.getInstance("SHA-1");
        d.reset();
        d.update(x.getBytes());
        return d.digest();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static String getMD5String(String str) {
    MessageDigest messageDigest = null;
    try {/*  w ww.ja va2s  . co  m*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (Exception e) {
        return null;
    }

    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (byte b : byteArray) {
        if ((0xFF & b) < 0x10)
            md5StrBuff.append("0");
        md5StrBuff.append(Integer.toHexString(0xFF & b));
    }
    return md5StrBuff.toString();
}

From source file:com.google.api.ads.adwords.awreporting.model.util.UrlHashUtil.java

/**
 * Creates a SHA-1 Hash of the url//  w w w.j  a v a2s  .c  o m
 * 
 * @param url the url that needs to be hashed
 * @return a Stri g with a SHA-1 hash of the URL
 */
public static String createUrlHash(String url) {
    String hash = null;
    MessageDigest messageDigest;
    try {
        messageDigest = MessageDigest.getInstance("SHA-1");
        messageDigest.reset();
        messageDigest.update(url.getBytes("UTF-8"));
        final byte[] resultByte = messageDigest.digest();
        hash = new String(Hex.encodeHex(resultByte));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return hash;
}

From source file:MainClass.java

static private String computeDigest(MessageDigest algorithm, String filename) {
    String returnValue = "";
    try {//from  w  ww.  j a v a  2  s.co  m
        algorithm.reset();
        FileInputStream fis = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DigestInputStream dis = new DigestInputStream(bis, algorithm);
        int ch;
        while ((ch = dis.read()) != -1)
            ;
        StringBuffer hexString = new StringBuffer();
        byte digest[] = algorithm.digest();
        int digestLength = digest.length;
        for (int i = 0; i < digestLength; i++) {
            hexString.append(hexDigit(digest[i]));
            hexString.append(" ");
        }
        returnValue = hexString.toString();
    } catch (IOException e) {
        System.err.println("Error generating digest for: " + filename);
    }
    return returnValue;
}

From source file:database.DBAccountManager.java

private static String hashPassword(String password) {
    String digest;//from   w  ww .  j  a  v  a2 s  .c  om
    try {
        MessageDigest md = MessageDigest.getInstance("md5");
        md.reset();
        byte[] bytes = md.digest(password.getBytes());
        digest = new BigInteger(1, bytes).toString(16);
    } catch (NoSuchAlgorithmException nsae) {
        nsae.printStackTrace();
        digest = null;
    }
    return digest;
}

From source file:org.rapidcontext.util.BinaryUtil.java

/**
 * Performs a digest hash on the specified byte array.
 *
 * @param alg            the hash algorithm (e.g. "MD5" or "SHA-256")
 * @param data           the data to hash
 *
 * @return the digest hash of the data/*from  w  ww . j  a va2  s .c o  m*/
 *
 * @throws NoSuchAlgorithmException if the hash algorithm isn't available
 */
public static byte[] hashBytes(String alg, byte[] data) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance(alg);
    digest.reset();
    digest.update(data);
    return digest.digest();
}

From source file:Main.java

public static String getMD5Value(String str, String fix, String charsetName) {
    if (str != null && fix != null) {
        String formalString = str + fix;
        try {/*w ww. j  a v  a  2  s  .  c  o m*/
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            try {
                algorithm.update(formalString.getBytes(charsetName));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                Log.e("WxException", e.getMessage(), e);
                return null;
            }
            byte messageDigest[] = algorithm.digest();

            return toHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            Log.w(TAG, e);
            Log.e("WxException", e.getMessage(), e);
        }
    }
    return null;

}

From source file:pl.nask.hsn2.service.Md5HashGenerator.java

private static String md5hashFromFile(BufferedInputStream bufferedInputStream) throws IOException {
    bufferedInputStream.reset();/*w w  w  .j a  va2s  .c  o  m*/
    String result = null;
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
        md.reset();
        try (InputStream dis = new DigestInputStream(new WhiteListFileInputStream(bufferedInputStream), md)) {
            while (dis.read() != -1) {
                // Nothing to do.
            }
            char[] md5 = Hex.encodeHex(md.digest());
            result = String.valueOf(md5);
        }
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("Could not create MD5 hash for whitelisting.\n{}", e);
        result = "";
    }
    return result;
}

From source file:wptools.cmds.DumpCerts.java

private static void dumpCert(X509Certificate cert) {
    System.out.println("Serial No.: " + formatFing(cert.getSerialNumber().toByteArray()));
    try {/*  w  w  w. j  av a  2  s.  com*/
        for (String ftype : FTYPES) {
            MessageDigest md = MessageDigest.getInstance(ftype);
            md.reset();
            System.out.format("%s: %s%n", ftype, formatFing(md.digest(cert.getEncoded())));
        }
    } catch (NoSuchAlgorithmException | CertificateException e) {
        Misc.die(e.getMessage());
    }
    System.out.println("Issued To: " + cert.getSubjectX500Principal());
    System.out.println("Issued By: " + cert.getIssuerX500Principal());
    System.out.format("Valid: from %tFT%<tT%<tz to %tFT%<tT%<tz%n%n", cert.getNotBefore(), cert.getNotAfter());
}

From source file:Main.java

public static String getMD5Str(String str) {
    MessageDigest messageDigest = null;
    try {/*from w w w.  j a v  a  2 s.  c om*/
        messageDigest = MessageDigest.getInstance("MD5");

        messageDigest.reset();

        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        System.out.println("NoSuchAlgorithmException caught!");
    } catch (UnsupportedEncodingException e) {
    }

    byte[] byteArray = messageDigest.digest();

    StringBuffer md5StrBuff = new StringBuffer();

    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        else
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
    }
    return md5StrBuff.substring(0, 24).toString().toUpperCase();
}