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:data.repository.pragma.utils.MD5Utils.java

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

    md.reset();//from w  w w . j  av a  2  s . c  om
    byte[] bytes = new byte[byteArraySize];
    int numBytes;
    try {
        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 e) {
        // TODO Auto-generated catch block
        return null;
    }
}

From source file:Main.java

public static String checkSum(String path) {
    String checksum = null;// w  w  w .j  a v  a 2  s  . c  o m
    try {
        FileInputStream fis = new FileInputStream(path);
        MessageDigest md = MessageDigest.getInstance("MD5");

        //Using MessageDigest update() method to provide input
        byte[] buffer = new byte[8192];
        int numOfBytesRead;
        while ((numOfBytesRead = fis.read(buffer)) > 0) {
            md.update(buffer, 0, numOfBytesRead);
        }
        byte[] hash = md.digest();
        checksum = new BigInteger(1, hash).toString(16); //don't use this, truncates leading zero
    } catch (IOException | NoSuchAlgorithmException ignored) {
    }
    assert checksum != null;
    return checksum.trim();
}

From source file:mx.com.pendulum.carga.util.Md5Converter.java

public static String calculaMD5(byte[] data) {
    InputStream is = new ByteArrayInputStream(data);
    try {//from w  w  w  .  j a v  a  2  s .co m
        byte[] buffer = new byte[1024];
        MessageDigest digest = MessageDigest.getInstance("MD5");
        int numRead = 0;
        while (numRead != -1) {
            numRead = is.read(buffer);
            if (numRead > 0) {
                digest.update(buffer, 0, numRead);
            }
        }
        byte[] md5Bytes = digest.digest();
        return convertHashToString(md5Bytes);
    } catch (Exception e) {
        return null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }

}

From source file:mx.com.pendulum.carga.util.Md5Converter.java

public static String getMD5Checksum(MultipartFile multipartFile) {
    InputStream is = null;//  ww  w. j  a  v  a 2 s . c  o  m
    try {
        is = new ByteArrayInputStream(multipartFile.getBytes());
        byte[] buffer = new byte[1024];
        MessageDigest digest = MessageDigest.getInstance("MD5");
        int numRead = 0;
        while (numRead != -1) {
            numRead = is.read(buffer);
            if (numRead > 0) {
                digest.update(buffer, 0, numRead);
            }
        }
        byte[] md5Bytes = digest.digest();
        return convertHashToString(md5Bytes);
    } catch (Exception e) {
        return null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.antelink.sourcesquare.client.scan.FileAnalyzer.java

/**
 * Computes the hash of a given file.//  ww  w . j a v a2s .com
 * 
 * @param hashType
 *            Hash that you want to compute. Usually SHA-1 or MD5 (or any
 *            other kind supported by java.security.MessageDigest).
 * @param fileToAnalyze
 *            Descriptor of the file to analyze. Must be a file otherwise
 *            you will get an exception.
 * @return the String hexadecimal representation of the file hash.
 * @throws NoSuchAlgorithmException
 *             if hashType is not a supported hash algorithm
 * @throws IOException
 *             in case of an I/O error while reading file
 */
public static String calculateHash(String hashType, File fileToAnalyze)
        throws NoSuchAlgorithmException, IOException {
    FileInputStream fis = null;
    BufferedInputStream inputStream = null;
    try {
        logger.debug("Calculating sha1 for file " + fileToAnalyze.getName());
        fis = new FileInputStream(fileToAnalyze);
        inputStream = new BufferedInputStream(fis);
        MessageDigest digest = MessageDigest.getInstance(hashType);
        byte[] buffer = new byte[1024 * 1024];
        int length = -1;
        while ((length = inputStream.read(buffer)) != -1) {
            digest.update(buffer, 0, length);
        }

        String hash = new String(Hex.encodeHex(digest.digest()));
        logger.debug("Sha1 calculated for file " + fileToAnalyze.getName());
        return hash;
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(fis);
    }
}

From source file:com.googlecode.download.maven.plugin.internal.SignatureUtils.java

static String computeSignatureAsString(File file, MessageDigest digest) throws IOException {
    InputStream fis = new FileInputStream(file);
    byte[] buffer = new byte[1024];
    int numRead;// ww  w. j  a  v a2  s.  c om
    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            digest.update(buffer, 0, numRead);
        }
    } while (numRead != -1);
    fis.close();
    byte[] actualDigest = digest.digest();
    return new String(Hex.encodeHex(actualDigest));
}

From source file:Main.java

public static final String getOblyDevicesID(Context mContext) {

    String m_szImei = "";
    try {/*from  w  w w .j a  v a  2  s .  c  om*/
        TelephonyManager TelephonyMgr = null;
        TelephonyMgr = (TelephonyManager) mContext.getSystemService(Activity.TELEPHONY_SERVICE);
        m_szImei = TelephonyMgr.getDeviceId();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szDevIDShort = null;
    try {
        m_szDevIDShort = "35" + //we make this look like a valid IMEI

                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 10
                + Build.DEVICE.length() % 10 + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
                + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10
                + Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
                + Build.USER.length() % 10; //13 digits
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    String m_szAndroidID = "";
    try {
        m_szAndroidID = "";
        Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szWLANMAC = "";
    try {
        WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szBTMAC = null;
    try {
        BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
        m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        m_szBTMAC = m_BluetoothAdapter.getAddress();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID + m_szWLANMAC + m_szBTMAC;
    // compute md5
    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
    // get md5 bytes
    byte p_md5Data[] = m.digest();
    // create a hex string
    String m_szUniqueID = new String();
    for (int i = 0; i < p_md5Data.length; i++) {
        int b = (0xFF & p_md5Data[i]);
        // if it is a single digit, make sure it have 0 in front (proper padding)
        if (b <= 0xF)
            m_szUniqueID += "0";
        // add number to string
        m_szUniqueID += Integer.toHexString(b);
    } // hex string to uppercase
    m_szUniqueID = m_szUniqueID.toUpperCase();

    return m_szUniqueID;
}

From source file:it.feio.android.omninotes.utils.ACRAPostSender.java

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

From source file:Main.java

public static String getMD5EncryptedString(String encTarget) {
    MessageDigest mdEnc = null;
    try {//from w w  w.ja  v  a  2s . c o m
        mdEnc = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception while encrypting to md5");
        e.printStackTrace();
    } // 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:com.netflix.raigad.utils.SystemUtils.java

public static byte[] md5(byte[] buf) {
    try {//  w  w  w . j  a v  a2s.c o m
        MessageDigest mdigest = MessageDigest.getInstance("MD5");
        mdigest.update(buf, 0, buf.length);
        return mdigest.digest();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}