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(byte[] input) 

Source Link

Document

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

Usage

From source file:net.blogracy.model.hashes.Hashes.java

/**
 * Creates an hash from a {@code String}
 *
 * @param value is the {@code String} to be hashed
 * @return the Hash of {@param value}/*  w w  w .  j a  v  a  2s.  com*/
 */
static public Hash newHash(String value) {
    Hash result = null;
    try {
        MessageDigest digester = MessageDigest.getInstance("SHA-1");
        final byte[] hash = digester.digest(value.getBytes());
        result = new HashImpl(hash);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String md5(String senha) {
    String sen = "";
    MessageDigest md = null;
    try {//from  ww w  .  j a v  a 2 s  .  com
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.w("Problemas ao gerar MD5", e);
    }
    BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));
    sen = hash.toString(16);
    return sen;
}

From source file:com.shangpin.utils.Digests.java

/**
 * MD5Encode/*from  w w  w.j  av a 2s .c o m*/
 * 
 * @param origin
 * @return MD5 code
 */
public static String MD5Encode(String origin) {
    String resultString = null;

    try {
        resultString = new String(origin);
        MessageDigest md = MessageDigest.getInstance("MD5");
        resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
    } catch (Exception ex) {

    }
    return resultString;
}

From source file:io.crate.testing.Utils.java

public static String sha1(String input) {
    StringBuilder stringBuilder = new StringBuilder();
    try {/*ww w .  j  ava  2  s  . c  o m*/
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        for (byte res : messageDigest.digest(input.getBytes())) {
            stringBuilder.append(Integer.toString((res & 0xff) + 0x100, 16).substring(1));
        }
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Hashing algorithms does not exist");
    }
    return stringBuilder.toString();
}

From source file:aes_encryption.AES_Encryption.java

public static void setKey(String myKey) {

    MessageDigest sha = null;
    try {//from w ww  . j  a va  2 s .c om
        key = myKey.getBytes("UTF-8");
        System.out.println(key.length);
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16); //change the bits later 128 then 256
        System.out.println(key.length);
        System.out.println(new String(key, "UTF-8"));
        secretKey = new SecretKeySpec(key, "AES");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:com.monarchapis.client.authentication.HawkV1RequestProcessor.java

private static String getHawkHash(BaseClient<?> client) {
    try {/*  w  w w . j a  v a2  s.  co  m*/
        StringBuilder sb = new StringBuilder();
        String httpContent = client.getBody();
        String mimeType = "";
        String content = "";

        if (httpContent != null) {
            mimeType = StringUtils.trimToEmpty(StringUtils.substringBefore(client.getContentType(), ";"));
            content = httpContent;
        }

        sb.append("hawk.1.payload\n");
        sb.append(mimeType);
        sb.append("\n");
        sb.append(content);
        sb.append("\n");

        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(sb.toString().getBytes("UTF-8"));
        return Base64.encodeBase64String(hash);
    } catch (Exception e) {
        throw new RuntimeException("Could not create hawk hash", e);
    }
}

From source file:com.pinterest.pinlater.backends.common.PinLaterBackendUtils.java

/**
 * Get the salted hash of a password.// ww w.j  a va  2  s.  co  m
 *
 * @param password  String to be salted and hashed.
 * @return Salted hash of the password string.
 */
public static String getSaltedHash(String password) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    String strToDigest = password + SALT;
    byte[] digestedStr = digest.digest(strToDigest.getBytes());
    return new String(Hex.encodeHex(digestedStr));
}

From source file:cn.com.qiqi.order.utils.Encodes.java

public static String md5(String rawPass, Object salt, int iterations) {
    try {/*from  w  w w. j  a  v a2  s. c o m*/
        String saltedPass = mergePasswordAndSalt(rawPass, salt, false);
        MessageDigest messageDigest = MessageDigest.getInstance("md5");
        byte[] digest = messageDigest.digest(saltedPass.getBytes());
        for (int i = 1; i < 1; i++) {
            digest = messageDigest.digest(digest);
        }
        return encodeHex(digest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:DigestUtil.java

/**
 * Digests the {@code data} with the specified {@code type} and returns the 
 * raw unencoded bytes.//from  w  ww.  j av a2 s.  c o  m
 */
public static byte[] getPlainDigestedValue(String type, byte[] data) {
    try {
        MessageDigest digest = MessageDigest.getInstance(type);
        return digest.digest(data);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.codedx.burp.security.InvalidCertificateDialogStrategy.java

public static byte[] getSHA1(byte[] input) {
    try {/*from w w  w.j  a  v  a 2 s  .  c  om*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();
        return md.digest(input);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}