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

static Cipher createCipher(int mode) throws Exception {
    PBEKeySpec keySpec = new PBEKeySpec("test".toCharArray());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(keySpec);
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update("input".getBytes());
    byte[] digest = md.digest();
    byte[] salt = new byte[8];
    for (int i = 0; i < 8; ++i)
        salt[i] = digest[i];/*from   www .  j  a va 2  s .co m*/
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
    cipher.init(mode, key, paramSpec);
    return cipher;
}

From source file:Main.java

private static byte[] encryptAlgorithm(byte[] data, String algorithm) {
    try {/*from  w  w  w.  j  a v a 2  s  .com*/
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(data);
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return new byte[0];
}

From source file:Main.java

public static byte[] encrypt(String x) throws Exception {
    java.security.MessageDigest d = null;
    d = java.security.MessageDigest.getInstance("SHA-1");
    d.reset();//w w  w.ja v a2s .com
    d.update(x.getBytes());
    return d.digest();
}

From source file:Main.java

private static byte[] hashTemplate(final byte[] data, final String algorithm) {
    if (data == null || data.length <= 0)
        return null;
    try {/*from  ww  w . ja va  2s .  c om*/
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(data);
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static String hashBytes(MessageDigest hash, byte[] bytes) {
    hash.update(bytes);/*from ww  w. j a  v a2 s.  c om*/
    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:Main.java

private static byte[] hashTemplate(byte[] data, String algorithm) {
    if (data == null || data.length <= 0)
        return null;
    try {/*from w ww .ja v a  2s  .com*/
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(data);
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] md5Digest(byte[] password) throws Exception {
    try {// ww w.j  av a  2  s . c  o  m
        MessageDigest alg = MessageDigest.getInstance("MD5");
        alg.update(password);
        byte[] digest = alg.digest();
        return digest;
    } catch (Exception e) {
        throw new Exception(e);
    }
}

From source file:Main.java

public static String SHA256(String text) throws NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance("SHA-256");

    md.update(text.getBytes());//from   w w  w  .j av a  2s .c o m
    byte[] digest = md.digest();

    return Base64.encodeToString(digest, Base64.DEFAULT);
}

From source file:Main.java

public static String md5(String str) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(str.getBytes());/*from   w  ww  . ja v a  2 s . co m*/

    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 byte[] computeSHA256(byte[] convertme, int offset, int len) {
    try {/*from  w w w .j av  a 2 s  .  co  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(convertme, offset, len);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}