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:com.networknt.utility.HashUtil.java

public static String md5(String input) {

    String md5 = null;/*from  w w  w.  j a va  2 s  .co  m*/

    if (null == input)
        return null;

    try {

        //Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance("MD5");

        //Update input string in message digest
        digest.update(input.getBytes(UTF_8), 0, input.length());

        //Converts message digest value in base 16 (hex)
        md5 = new BigInteger(1, digest.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    return md5;
}

From source file:Main.java

/**
 * Returns the lowercase string representation of the file's MD5 sum.
 *//*from   w w  w. j  a  va  2  s  .c  o m*/
public static String getMD5Sum(String file) throws IOException {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        int read = 0;
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        is.close();
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        return bigInt.toString(16);
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:Main.java

public static byte[] getFileSHA1(String path) throws IOException {

    File file = new File(path);
    FileInputStream in = new FileInputStream(file);
    MessageDigest messagedigest;
    try {// www .  ja  v a  2s  .  c om
        messagedigest = MessageDigest.getInstance("SHA-1");

        byte[] buffer = new byte[1024 * 64];
        int len;

        while ((len = in.read(buffer)) > 0) {
            messagedigest.update(buffer, 0, len);
        }

        return messagedigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        throw e;
    } finally {
        in.close();
    }
    return null;
}

From source file:Main.java

public static byte[] createMD5(FileInputStream is) throws IOException {
    MessageDigest digester = null;
    try {// w  w  w  .  j  a v a2s  .co m
        digester = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[16 * 1024]; // 16k
        int length = 0;
        while ((length = is.read(buffer)) > 0) {
            digester.update(buffer, 0, length);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        is.close();
    }

    if (digester != null) {
        return digester.digest();
    }

    return null;
}

From source file:Main.java

private static byte[] createChecksum(String filepath) throws IOException, NoSuchAlgorithmException {
    InputStream fileInputStream = new FileInputStream(filepath);

    byte[] buffer = new byte[1024];
    MessageDigest md5HashInstance = MessageDigest.getInstance("MD5");
    int numberOfBytesRead;

    do {//from w w w.  j  a v  a2s  .c  o m
        numberOfBytesRead = fileInputStream.read(buffer);
        if (numberOfBytesRead > 0) {
            md5HashInstance.update(buffer, 0, numberOfBytesRead);
        }
    } while (numberOfBytesRead != -1);

    fileInputStream.close();

    return md5HashInstance.digest();
}

From source file:com.nunofacha.chestmaster.commands.ChestHashCommand.java

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

    try {/*from  w  ww  .java  2 s.co  m*/
        md.reset();
        byte[] bytes = new byte[byteArraySize];
        int numBytes;
        while ((numBytes = is.read(bytes)) != -1) {
            md.update(bytes, 0, numBytes);
        }
        byte[] digest = md.digest();
        String result = new String(Hex.encodeHex(digest));
        return result;
    } catch (IOException ex) {
        Logger.getLogger(ChestHashCommand.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "Failed to get hash!";
}

From source file:Main.java

public static byte[] getSHA1hash(byte[] dataByte) {
    MessageDigest md = null;
    byte[] sha1hash = new byte[20];
    try {//w  ww.j  av  a  2 s .  com
        md = MessageDigest.getInstance("SHA-1");
        md.update(dataByte, 0, dataByte.length);
        sha1hash = md.digest();
        return sha1hash;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return null;
}

From source file:com.distimo.sdk.Utils.java

static String md5(String s) {
    if (Utils.DEBUG) {
        Log.i(TAG, "md5()");
    }//from   ww w . j  a  va  2 s  .  c om

    String result = "";

    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(), 0, s.length());
        result = byteArrayToHexString(m.digest());
    } catch (final NoSuchAlgorithmException nsae) {
        if (Utils.DEBUG) {
            nsae.printStackTrace();
        }
    }

    return result;
}

From source file:Main.java

public static String getMD5EncryptedString(byte[] _encTarget) {
    String encTarget = "";
    try {/*from w  w  w .j a va2 s  .  com*/
        encTarget = new String(_encTarget, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        System.out.println("error converting byte[] to string");
    }
    MessageDigest mdEnc = null;
    try {
        mdEnc = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception while encrypting to md5");
    } // Encryption algorithm
    mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
    String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
    while (md5.length() < 32) {
        md5 = "0" + md5;
    }
    return md5;
}

From source file:Main.java

public static byte[] calculateMd5(String filePath) throws IOException {
    try {//  w  ww.  j  av  a 2 s . c  o  m
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[4 * 1024];
        InputStream is = new FileInputStream(new File(filePath));
        int lent;
        while ((lent = is.read(buffer)) != -1) {
            digest.update(buffer, 0, lent);
        }
        is.close();
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("MD5 algorithm not found.");
    }
}