Example usage for java.security MessageDigest update

List of usage examples for java.security MessageDigest update

Introduction

In this page you can find the example usage for java.security MessageDigest update.

Prototype

public void update(byte[] input, int offset, int len) 

Source Link

Document

Updates the digest using the specified array of bytes, starting at the specified offset.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL u = new URL("http://www.google.com");
    InputStream in = u.openStream();
    MessageDigest sha = MessageDigest.getInstance("SHA");
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
        sha.update(data, 0, bytesRead);
    }/*from w  w w .  ja  va2  s .  c o m*/
    byte[] result = sha.digest();
    System.out.println(Arrays.toString(result));
    System.out.println(new BigInteger(result));
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    InputStream fis = new FileInputStream("a.exe");

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;//from  w w  w. j a  va2s.  c o  m
    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);
        }
    } while (numRead != -1);
    fis.close();

    byte[] b = complete.digest();
    String result = "";
    for (int i = 0; i < b.length; i++) {
        result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    System.out.println(result);

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("Usage: Masher filename");
        return;//w  w  w.ja  v a  2  s  .c o  m
    }
    MessageDigest md = MessageDigest.getInstance("MD5");
    FileInputStream in = new FileInputStream(args[0]);
    byte[] buffer = new byte[8192];
    int length;
    while ((length = in.read(buffer)) != -1)
        md.update(buffer, 0, length);
    byte[] raw = md.digest();
    BASE64Encoder encoder = new BASE64Encoder();
    String base64 = encoder.encode(raw);
    System.out.println(base64);
}

From source file:Main.java

public static String convertToMD5(String toEnc) {
    try {//from  w ww  . ja  v  a 2 s.  c  o  m
        MessageDigest mdEnc = MessageDigest.getInstance("MD5");
        mdEnc.update(toEnc.getBytes(), 0, toEnc.length());
        return new BigInteger(1, mdEnc.digest()).toString(16);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static byte[] computeSHA256(byte[] convertme, int offset, int len) {
    try {/*from  w ww .  j av a 2 s.  co  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(convertme, offset, len);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String toMd5(String input) {
    String output = input;/*from   w w w  . j  a v  a2 s. co m*/
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(input.getBytes(), 0, input.length());
        output = new BigInteger(1, md5.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return output;
}

From source file:Main.java

public static String hashMD5(String s) {
    try {/*  w  w w  .  j  a v  a2s  . c  om*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(), 0, s.length());
        String result = new BigInteger(1, m.digest()).toString();
        while (result.length() < 32) {
            result = "0" + result;
        }
        return result;
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:Main.java

private static String hashAlgorithm(String hash, String text)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance(hash);
    md.update(text.getBytes("iso-8859-1"), 0, text.length());

    return convertToHex(md.digest());
}

From source file:Main.java

public static String makeSHA1HashBase64(byte[] bytes) {
    try {//from www . j  a  v a  2s .  co m
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(bytes, 0, bytes.length);
        byte[] sha1hash = md.digest();
        return Base64.encodeToString(sha1hash, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Computes the SHA1 Hash of given UTF-8 data.
 *
 * @param message/*from   w ww. j  a v a  2s .c  o m*/
 * @return
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 */
public static String SHA1(String message) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    byte[] data = message.getBytes("UTF-8");
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.update(data, 0, data.length);
    byte[] digest = md.digest();
    return bytesToHex(digest);
}