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:de.micromata.genome.gwiki.auth.PasswordUtils.java

/**
 * Standart asymetrischen password verschlsseln mit SHA-256 fr Genome
 * /*from   www  . j  ava2 s .c om*/
 * @param plaintext Nie <code>null</code>
 * @return password hash
 */

public static String unsaltedSecureHash(String plaintext) {
    Validate.notNull(plaintext, "plaintext not set");
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(plaintext.getBytes(CharEncoding.UTF_8));
        byte raw[] = md.digest();
        String hash = Converter.encodeBase64(raw);
        return hash;
    } catch (Exception ex) {
        /**
         * @logging
         * @reason Beim asymetrischen verschlsseln ist ein Fehler aufgetreten.
         * @action berprfen der Java-Installation
         */
        throw new LoggedRuntimeException(ex, LogLevel.Fatal, GenomeLogCategory.System,
                "Error while executing hashing encryption: " + ex.getMessage());
    }
}

From source file:gov.nasa.jpl.analytics.util.CommonUtil.java

public static String hashString(String str) {
    String hash = "";
    try {/*from  ww w  .  j  a v  a2 s  .  co  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(str.getBytes("UTF-8"));
        byte[] digest = md.digest();
        hash = String.format("%064x", new java.math.BigInteger(1, digest)).toUpperCase();
    } catch (NoSuchAlgorithmException e) {
        LOG.error("Not a valid Hash Algorithm for String " + str);
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        LOG.error("Not a valid Encoding for String " + str);
        e.printStackTrace();
    }
    return hash;
}

From source file:Main.java

public static String hash(String pass) {
    MessageDigest md = null;
    try {// w  ww. ja v  a2 s. c  om
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    md.update(pass.getBytes());
    byte byteData[] = md.digest();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();

}

From source file:Main.java

public static String sha1(String strSrc) {
    MessageDigest md = null;
    String strDes = "";

    byte[] bt = strSrc.getBytes();
    try {/* ww w .  ja v a  2s.  c o m*/
        md = MessageDigest.getInstance("SHA-1");
        md.update(bt);
        byte[] encryptStr = md.digest();
        String tmp = null;
        for (int i = 0; i < encryptStr.length; i++) {
            tmp = (Integer.toHexString(encryptStr[i] & 0xFF));
            if (tmp.length() == 1) {
                strDes += "0";
            }
            strDes += tmp;
        }
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Invalid algorithm.");
        return null;
    }
    return strDes;
}

From source file:com.dianping.wed.cache.redis.util.TestDataUtil.java

/**
 * MD5//from   ww  w  .  jav  a2 s .  c om
 *
 * @param str ?
 * @return ?
 */
public static String toMD5(String str) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] byteDigest = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < byteDigest.length; offset++) {
            i = byteDigest[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        //32?
        return buf.toString();
        // 16?
        //return buf.toString().substring(8, 24);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

/**
 * Method for returning an md5 hash of a string.
 * /*  w  w  w .  j a  v  a 2  s  .co m*/
 * @param val
 *            the string to hash.
 * @return A hex string representing the md5 hash of the input.
 */
private static String md5(String val) {
    String result = null;

    if ((val != null) && (val.length() > 0)) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(val.getBytes(), 0, val.length());
            result = String.format("%032X", new BigInteger(1, md5.digest()));
        } catch (NoSuchAlgorithmException nsae) {
            result = val.substring(0, 32);
        }
    }

    return result;
}

From source file:com.microsoft.aad.adal4j.AsymmetricKeyCredential.java

private static byte[] getHash(final byte[] inputBytes)
        throws NoSuchAlgorithmException, CertificateEncodingException {
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(inputBytes);/*  w ww.  j a va  2 s . c  o m*/
    return md.digest();

}

From source file:net.sf.jml.util.DigestUtils.java

public static byte[] sha1(ByteBuffer[] buffers) {
    try {//  ww w  .j  a v  a  2 s  .c o  m
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        for (int i = 0; i < buffers.length; i++) {
            update(digest, buffers[i].slice());
        }
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        log.error(e, e);
        return null;
    }
}

From source file:com.github.robozonky.integrations.zonkoid.ZonkoidConfirmationProvider.java

static String md5(final String secret) throws NoSuchAlgorithmException {
    final MessageDigest mdEnc = MessageDigest.getInstance("MD5");
    mdEnc.update(secret.getBytes(Defaults.CHARSET));
    return new BigInteger(1, mdEnc.digest()).toString(16);
}

From source file:com.moki.touch.util.UrlUtil.java

/**
 * This method is used to create a filename for cached content
 * @param url the url of the content to be cached
 * @return an MD5 hashed string//from  w w  w. j ava 2  s. co  m
 */
public static String hashUrl(String url) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(url.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

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