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:Main.java

private static String sha1Hash(String toHash) {
    String hash = null;//from www.ja  v a 2 s.  com
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        byte[] bytes = toHash.getBytes("UTF-8");
        digest.update(bytes, 0, bytes.length);
        bytes = digest.digest();

        // This is ~55x faster than looping and String.formating()
        hash = bytesToHex(bytes);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return hash;
}

From source file:Main.java

public static String md5(final String in) {
    MessageDigest digest;
    try {/* w ww.  j a va  2 s .c  o  m*/
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        final byte[] a = digest.digest();
        final int len = a.length;
        final StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.mascherl.example.service.LoginService.java

public static String sha256(String value) {
    MessageDigest messageDigest = createMessageDigest();
    messageDigest.update(value.getBytes(StandardCharsets.UTF_8));
    byte[] digest = messageDigest.digest();
    byte[] base64Digest = Base64.getEncoder().encode(digest);
    return new String(base64Digest, StandardCharsets.UTF_8);
}

From source file:Main.java

/**
 * This method uses MessageDigest class to convert the passed string to
 * SHA-1 hexadecimal text.//from   w w w  .  j a  va  2 s .  c  om
 *
 * @param text Input string
 * @return Return SHA-1 value
 */
public static String convertToSHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[30];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
}

From source file:StringUtils.java

public static String hashString(String sPassword) {

    if (sPassword == null || sPassword.equals("")) {
        return "empty:";
    } else {/*from w ww  . j a v  a 2s . c  om*/
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(sPassword.getBytes("UTF-8"));
            byte[] res = md.digest();
            return "sha1:" + byte2hex(res);
        } catch (NoSuchAlgorithmException e) {
            return "plain:" + sPassword;
        } catch (UnsupportedEncodingException e) {
            return "plain:" + sPassword;
        }
    }
}

From source file:Main.java

private static String encode(String str, String method) {
    MessageDigest md = null;
    String dstr = null;//w ww .jav a  2 s  .  c  o  m
    try {
        md = MessageDigest.getInstance(method);
        md.update(str.getBytes());
        dstr = new BigInteger(1, md.digest()).toString(16);
        int dstrCount = 32 - dstr.length();
        for (int i = 0; i < dstrCount; i++) {
            dstr = "0" + dstr;
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return dstr;
}

From source file:Main.java

private static String getDigestValue(String s, String digestType) {
    String result = "";
    byte strTemp[] = s.getBytes();
    MessageDigest digestTemp;

    try {/*from   w  w  w . j a va 2 s.co m*/
        digestTemp = MessageDigest.getInstance(digestType);
        digestTemp.update(strTemp);

        byte[] digestValue = digestTemp.digest();

        result = bytesToHexString(digestValue);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}

From source file:org.nebulaframework.util.hashing.SHA1Generator.java

/**
 * Generates SHA1 Hash for the given {@code byte[]} and returns
 * the hash code as a {@code byte[]}./*  w  ww.  j a v a2s .  c om*/
 * 
 * @param source source value
 * @return SHA-1 hash for value
 * 
 * @see #generate(String)
 * @see #generateAsString(byte[])
 * 
 * @throws IllegalArgumentException if {@code source} is {@code null}
 */
public static byte[] generate(byte[] source) throws IllegalArgumentException {

    // Check for nulls
    Assert.notNull(source);

    try {
        MessageDigest digest = MessageDigest.getInstance("SHA");
        digest.update(source);
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        log.fatal("Cannot load hashing algorithm", e);
        throw new RuntimeException(e);
    }
}

From source file:MD5.java

public static String create(String content) throws MD5IsNotSupported {
    String result = "";
    try {//  w ww  .  j a v  a  2  s .c  om
        byte[] defaultBytes = content.getBytes();
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        result = hexString.toString();
    } catch (NoSuchAlgorithmException ex) {
        throw new MD5IsNotSupported(ex);
    }

    assert !result.isEmpty();
    return result;
}

From source file:Main.java

public static String getMessageDigest(byte[] paramArrayOfByte) {
    char[] arrayOfChar1 = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
    String str;/*from   w w w  .j a v  a  2 s. c om*/
    try {
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.update(paramArrayOfByte);
        byte[] arrayOfByte = localMessageDigest.digest();
        int i = arrayOfByte.length;
        char[] arrayOfChar2 = new char[i * 2];
        int j = 0;
        int k = 0;
        while (true) {
            if (j >= i) {
                str = new String(arrayOfChar2);
                break;
            }
            int m = arrayOfByte[j];
            int n = k + 1;
            arrayOfChar2[k] = arrayOfChar1[(0xF & m >>> 4)];
            k = n + 1;
            arrayOfChar2[n] = arrayOfChar1[(m & 0xF)];
            j++;
        }
    } catch (Exception localException) {
        str = null;
    }
    return str;
}