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

/**
 * Converts a String into a specified hash.
 * //ww w .  j av a2  s.  c  om
 * @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:com.stickyd.services.impl.UserLoginServiceImpl.java

public static String md5(String s) {
    MessageDigest md5;
    try {//from w w w .j av a  2  s  . c o m
        md5 = MessageDigest.getInstance("MD5");
        md5.update(s.getBytes());
        return new BigInteger(1, md5.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    }
}

From source file:edu.sjsu.cohort6.esp.common.CommonUtils.java

private static String generateMD5Hash(String plaintext) throws NoSuchAlgorithmException {
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.reset();/* w ww  .j  a  va2s.  co  m*/
    m.update(plaintext.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.toString(16);
    // Now we need to zero pad it if you actually want the full 32 chars.
    while (hashtext.length() < 32) {
        hashtext = "0" + hashtext;
    }
    return hashtext;
}

From source file:Main.java

public static byte[] createDigest(String passcode, long t1, double q1)
        throws IOException, NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(passcode.getBytes());/*w  ww. j a  v a2s . c  o m*/
    ByteBuffer bb = ByteBuffer.allocate(16); //8 bytes for long and double each
    bb.putLong(t1);
    bb.putDouble(q1);
    md.update(bb);
    return md.digest();
}

From source file:biblivre3.utils.TextUtils.java

public static String encodePassword(String password) {
    if (password == null) {
        throw new ExceptionUser("Password is null");
    }//from  w  w  w . j  a  va 2 s .c  o  m
    if (password.trim().length() == 0) {
        throw new ExceptionUser("Password is empty");
    }
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(password.getBytes("UTF-8"));
        byte[] raw = md.digest();
        return new String((new Base64()).encode(raw));
    } catch (Exception e) {
        throw new ExceptionUser(e.getMessage());
    }
}

From source file:Main.java

public static String sha512(String what_to_encode) {
    final MessageDigest sha512;
    try {/* w  w  w  . j a  va 2  s  .  c o m*/
        sha512 = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e) {
        return "404";
    }
    sha512.update(what_to_encode.getBytes());
    byte byteData[] = sha512.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 encode(String input) {
    try {// w  w  w  .  j a  va  2 s .com
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] inputByteArray = input.getBytes();
        messageDigest.update(inputByteArray);
        byte[] resultByteArray = messageDigest.digest();
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] resultCharArray = new char[resultByteArray.length * 2];
        int index = 0;
        for (byte b : resultByteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        return new String(resultCharArray);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:com.magic.util.HashCrypt.java

public static String getDigestHash(String str, String code, String hashType) {
    if (str == null)
        return null;
    try {/* w ww .  j av a2s . c o m*/
        byte codeBytes[] = null;

        if (code == null)
            codeBytes = str.getBytes();
        else
            codeBytes = str.getBytes(code);
        MessageDigest messagedigest = MessageDigest.getInstance(hashType);

        messagedigest.update(codeBytes);
        byte digestBytes[] = messagedigest.digest();
        char[] digestChars = Hex.encodeHex(digestBytes);
        ;
        return "{" + hashType + "}" + new String(digestChars, 0, digestChars.length);
    } catch (Exception e) {
        throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e);
    }
}

From source file:com.google.api.ads.adwords.awreporting.model.util.UrlHashUtil.java

/**
 * Creates a SHA-1 Hash of the url//from  ww  w .  j av  a2s .c  o  m
 * 
 * @param url the url that needs to be hashed
 * @return a Stri g with a SHA-1 hash of the URL
 */
public static String createUrlHash(String url) {
    String hash = null;
    MessageDigest messageDigest;
    try {
        messageDigest = MessageDigest.getInstance("SHA-1");
        messageDigest.reset();
        messageDigest.update(url.getBytes("UTF-8"));
        final byte[] resultByte = messageDigest.digest();
        hash = new String(Hex.encodeHex(resultByte));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return hash;
}

From source file:com.pieframework.runtime.utils.CertificateUtils.java

public static String getThumbPrint(X509Certificate certificate) {

    String thumbPrint = "";
    try {/*from w w w.  j a v a  2s  .c  om*/

        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] der = certificate.getEncoded();
        md.update(der);
        byte[] digest = md.digest();

        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

        StringBuffer buf = new StringBuffer(digest.length * 2);

        for (int i = 0; i < digest.length; ++i) {
            buf.append(hexDigits[(digest[i] & 0xf0) >> 4]);
            buf.append(hexDigits[digest[i] & 0x0f]);
        }

        thumbPrint = buf.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return thumbPrint;
}