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

/**
 * Converts a String into a specified hash.
 * //from   ww w.  j  a v a  2s. co  m
 * @param text
 *            Text to convert.
 * @return hash.
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
public static String hashAlgorithm(String hash, String text)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    //TapjoyLog.i(TAPJOY_UTIL, "" + hash + ": " + text);

    MessageDigest md;
    byte[] sha1hash = new byte[40];

    // MD5, SHA-1, etc
    md = MessageDigest.getInstance(hash);
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();

    return convertToHex(sha1hash);
}

From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

/**
 *
 * @param msg the message to sign//w  w w .  jav a 2s  .co  m
 * @return the signature hash
 * @throws CryptoException on sign error
 */
public static String sign(final String msg) throws CryptoException {
    try {
        final String toEnc = msg + MessageCryptoUtil.SIGNATURE;
        final MessageDigest mdEnc = MessageDigest.getInstance("MD5");
        mdEnc.update(toEnc.getBytes(Charset.forName("UTF-8")), 0, toEnc.length());
        return new BigInteger(1, mdEnc.digest()).toString(16);
    } catch (final Exception e) {
        throw new CryptoException("Creating signature failed", e);
    }
}

From source file:Main.java

public static String toSHA1(String s) {
    MessageDigest md = null;
    byte[] sha1hash = null;
    try {//  w  w w  .  j av  a 2  s.c om
        md = MessageDigest.getInstance("SHA-1");
        sha1hash = new byte[40];
        md.update(s.getBytes(UTF_8), 0, s.length());
    } catch (Exception e) {
        return ERROR_SHA1;
    }
    sha1hash = md.digest();
    Formatter formatter = new Formatter();
    for (byte b : sha1hash) {
        formatter.format("%02x", b);
    }
    return formatter.toString();
}

From source file:com.netflix.dynomitemanager.sidecore.utils.SystemUtils.java

public static byte[] md5(byte[] buf) {
    if (buf == null)
        throw new IllegalArgumentException("buffer cannot be null!");
    try {//w  w  w. j  av  a2  s . c  o m
        MessageDigest mdigest = MessageDigest.getInstance("MD5");
        mdigest.update(buf, 0, buf.length);
        return mdigest.digest();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getMD5(String value) {
    if (value == null || value.length() == 0)
        return null;

    MessageDigest m = null;

    try {/*w w w .  j a v  a  2 s .com*/
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    m.update(value.getBytes(), 0, value.length());
    return new BigInteger(1, m.digest()).toString(16).toUpperCase(Locale.getDefault());
}

From source file:com.sfalma.trace.Sfalma.java

public static String MD5(String data) throws Exception {
    MessageDigest m = MessageDigest.getInstance("MD5");

    m.update(data.getBytes(), 0, data.length());
    return new BigInteger(1, m.digest()).toString(16);
}

From source file:Main.java

private static String sha256Hex(final String filePath) throws NoSuchAlgorithmException, IOException {
    final InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
    final MessageDigest md = MessageDigest.getInstance("SHA-256");

    final byte[] dataBytes = new byte[1024];

    int nread;//from w  w  w . j a  v a2  s  . c o m
    while ((nread = fis.read(dataBytes)) != -1)
        md.update(dataBytes, 0, nread);
    final byte[] mdbytes = md.digest();

    final StringBuilder sb = new StringBuilder();
    for (final byte b : mdbytes)
        sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));

    return sb.toString();
}

From source file:Main.java

/**
 * Hash a string/*w  ww .  j  a va  2 s  . c o m*/
 *
 * @param toHash String to be hashed.
 *
 * @return Hashed string.
 */
public static String hash(Object toHash) {
    String hashString = toHash.toString();
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return hashString;
    }

    md.update(hashString.getBytes(), 0, hashString.length());
    return convertToHex(md.digest());
}

From source file:Main.java

public static String md5Calc(File f) {
    int i;//from   w ww  . j  a v  a 2s  . com

    byte[] buffer = new byte[1024];
    int read = 0;
    String md5hash;
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(f);
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        md5hash = bigInt.toString(16);
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    if (md5hash.length() != 33) {
        String tmp = "";
        for (i = 1; i < (33 - md5hash.length()); i++) {
            tmp = tmp.concat("0");
        }
        md5hash = tmp.concat(md5hash);
    }

    return md5hash;
}

From source file:Main.java

/**
 * Generates SHA256 hash of the password which is used as key
 *
 * @param password used to generated key
 * @return SHA256 of the password//from  w w  w .j av a 2s .c o m
 */
private static SecretKeySpec generateKey(final String password)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    final MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
    byte[] bytes = password.getBytes("UTF-8");
    digest.update(bytes, 0, bytes.length);
    byte[] key = digest.digest();

    log("SHA-256 key ", key);

    return new SecretKeySpec(key, "AES");
}