Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

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

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:Main.java

static String md5hash(String key) {
    MessageDigest hash = null;//from   ww  w. j a  v a2s . c  om
    try {
        hash = MessageDigest.getInstance(HASH_ALGORITHM_MD5);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    hash.update(key.getBytes());
    byte[] digest = hash.digest();
    StringBuilder builder = new StringBuilder();
    for (int b : digest) {
        builder.append(Integer.toHexString((b >> 4) & 0xf));
        builder.append(Integer.toHexString((b >> 0) & 0xf));
    }
    return builder.toString();
}

From source file:com.jopss.logico.negocio.util.CriptoUtils.java

public static String md5Encrypt(String texto) {
    String encripted = null;//from w ww.  ja  v a 2s.  co m
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(texto.getBytes());
        BigInteger hash = new BigInteger(1, md.digest());
        encripted = hash.toString(16);
    } catch (NoSuchAlgorithmException e) {
        log.error(e);
    }
    return encripted;
}

From source file:Main.java

public static String hashKeyForDisk(String key) {
    String cacheKey;// www. ja  v  a2s.c  o m
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(key.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash = null;
    try {/*from w  w w. j  a v  a2 s.  c  om*/
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash = null;
    try {/*from   ww w .jav a2  s  . c om*/
        hash = MessageDigest.getInstance("MD5").digest( //No i18n
                string.getBytes("UTF-8"));//No i18n
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10) {
            hex.append("0");//No i18n
        }
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:com.cienet.util.EncryptDeciphering.java

/**
 * ?MD5?// w  w  w.  j ava  2s  . c  om
 * 
 * @param inputStream
 * @return
 */
public static String streamToMD5(InputStream inputStream) {
    try {
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[1024];
        int numRead = 0;
        while ((numRead = inputStream.read(buffer)) > 0) {
            mdTemp.update(buffer, 0, numRead);
        }
        return toHexString(mdTemp.digest());
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String md5(String input) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(input.getBytes("UTF-8"));
    BigInteger hash = new BigInteger(1, md5.digest());
    return String.format("%1$032X", hash);
}

From source file:Main.java

public static String md5_32(String plainText) {
    String re_md5 = new String();
    try {/*from  w ww.jav a2s  .c o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();

        int i;

        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }

        re_md5 = buf.toString();

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

From source file:Main.java

/**
 * SHA256 implementation/*from  ww  w .java  2  s .c  o m*/
 * @param toHash the cleartext string
 * @return a SHA256 hashed string
 */
public static String sha256(final String toHash) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(toHash.getBytes());
        StringBuilder hexString = new StringBuilder();

        for (byte aHash : hash) {
            String hex = Integer.toHexString(0xff & aHash);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }

        return hexString.toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:aes_encryption.AES_Encryption.java

public static void setKey(String myKey) {

    MessageDigest sha = null;//ww  w  .j  ava 2 s  . c  o m
    try {
        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();
    }
}