Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

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

Prototype

public byte[] digest() 

Source Link

Document

Completes the hash computation by performing final operations such as padding.

Usage

From source file:customerproject.customerutilities.Utilities.java

public static String SHA256(String email, String password)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {

    String text = email + "." + password;
    MessageDigest md = DigestUtils.getSha256Digest();

    byte[] sha256hash = new byte[32];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha256hash = md.digest();

    //return Base64.encodeBase64String(sha256hash);
    return Base64.encodeBase64URLSafeString(sha256hash);
    //String hex = convertToHex(sha256hash);

}

From source file:Main.java

static long md5sum(String text) {

    byte[] defaultBytes = text.getBytes();

    try {// w  w w.  j  a v a  2s  . c o m

        MessageDigest algorithm = MessageDigest.getInstance("MD5");

        algorithm.reset();

        algorithm.update(defaultBytes);

        byte digest[] = algorithm.digest();

        ByteBuffer buffer = ByteBuffer.wrap(digest);

        return buffer.getLong();

    } catch (NoSuchAlgorithmException e) {

        Log.e("udt", "md5 failed: " + e);

        return 0;

    }

}

From source file:club.jmint.crossing.specs.Security.java

public static String MD5(String str) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    try {/*from ww w .  j  a  va2  s . com*/
        byte[] btInput = str.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();

        int j = md.length;
        char mdstr[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            mdstr[k++] = hexDigits[byte0 >>> 4 & 0xf];
            mdstr[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(mdstr);
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        return null;
    }
}

From source file:it.paolorendano.clm.util.Utils.java

/**
 * Hash password./*from  w w  w . ja v a  2  s  .c om*/
 *
 * @param password the password
 * @return the string
 */
public static String hashPassword(String password) {
    MessageDigest mdigest;
    try {
        mdigest = MessageDigest.getInstance("SHA-256");
        mdigest.update(password.getBytes("UTF-8"));
        return Base64.encodeBase64String(mdigest.digest());
    } catch (NoSuchAlgorithmException e) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error(e.getMessage(), e);
    }
    return null;
}

From source file:Main.java

/**
 * Return a md5 string based on a given string 
 *//*from w w w  . ja v a  2  s.c o m*/
public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md;
    md = MessageDigest.getInstance("MD5");
    byte[] md5hash = new byte[32];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    md5hash = md.digest();
    return convertToHex(md5hash);
}

From source file:customerproject.customerutilities.Utilities.java

public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {

    MessageDigest md;
    md = MessageDigest.getInstance("MD5");
    byte[] md5hash = new byte[32];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    md5hash = md.digest();
    return convertToHex(md5hash);
}

From source file:Main.java

public static byte[] getDigestFromURL(URL u) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    InputStream in = u.openStream();
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
        md5.update(data, 0, bytesRead);/*from  w w  w  .j  ava  2s . c  o  m*/
    }
    return md5.digest();
}

From source file:org.sentilo.common.rest.hmac.HMACBuilder.java

private static String calculateMD5(final String contentToEncode) throws NoSuchAlgorithmException {
    final MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGORITHM);
    digest.update(contentToEncode.getBytes());
    return new String(Base64.encodeBase64(digest.digest()));
}

From source file:org.jboss.aerogear.sync.jsonmergepatch.client.JsonMergePatchClientSynchronizer.java

public static String checksum(final JsonNode content) {
    try {//w  ww.j  a v a 2 s.  c om
        final MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(content.asText().getBytes(UTF_8));
        return new BigInteger(1, md.digest()).toString(16);
    } catch (final Exception e) {
        throw new RuntimeException(e.getMessage(), e.getCause());
    }
}

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 {/*ww w. ja va2s .  com*/
        MessageDigest mdigest = MessageDigest.getInstance("MD5");
        mdigest.update(buf, 0, buf.length);
        return mdigest.digest();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}