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

public static String toMD5(String md5) {
    try {//from  ww  w  .j a  v  a2  s  .c o  m
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

public static String generate(String s) {
    try {/*from w  w  w  .j  a  va  2  s.  c om*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] digest = messageDigest.digest(s.getBytes());
        StringBuilder md5 = new StringBuilder();
        for (byte value : digest) {
            md5.append(Integer.toHexString((value & 0xFF) | 0x100).substring(1, 3));
        }
        return md5.toString();
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:Main.java

public static String getMd5String(String md5) {
    try {/*from   ww  w.j  a  va2  s.c  o  m*/
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public final synchronized static byte[] md5(byte[] buff) throws NoSuchAlgorithmException {
    MessageDigest md5Obj = MessageDigest.getInstance("MD5");
    md5Obj.reset();/*from   ww w  .ja v a2 s  .co  m*/
    return md5Obj.digest(buff);
}

From source file:Main.java

public static void encrypt(String fileIn, String fileOut, byte key[])
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    FileInputStream fis = new FileInputStream(fileIn);
    // This stream write the encrypted text. This stream will be wrapped by another stream.
    FileOutputStream fos = new FileOutputStream(fileOut);

    // Length is 32 bytes
    //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    key = sha.digest(key);
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;//from www  . jav  a2 s.com
    byte[] d = new byte[8];
    while ((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}

From source file:com.skilrock.lms.common.utility.MD5Encoder.java

public static String encode(String value) {

    try {//from  ww  w .  j a va 2s .  c o  m

        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] hashMD5 = md5.digest(value.getBytes());
        return (new BASE64Encoder()).encode(hashMD5);

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    }

    return null;
}

From source file:ch.windmobile.server.social.mongodb.util.AuthenticationServiceUtil.java

public static boolean validateSHA1(String email, Object password, String base64)
        throws NoSuchAlgorithmException {
    if (password == null) {
        throw new IllegalArgumentException("password cannot be null");
    }//  w w  w  .j a va 2  s.c  o m
    MessageDigest md = MessageDigest.getInstance("SHA1");
    String base = email + ":" + password.toString();
    byte[] result = md.digest(base.getBytes());
    byte[] data = Base64.decode(base64.getBytes());
    return MessageDigest.isEqual(data, result);
}

From source file:Main.java

private static String computeHash(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();/* w w  w . jav  a2 s  . co m*/

    byte[] byteData = digest.digest(input.getBytes("UTF-8"));
    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 hashBytes(byte[] input, String algo) {
    try {/*from  ww  w.java2  s  .com*/
        MessageDigest md = MessageDigest.getInstance(algo);
        byte[] hashBytes = md.digest(input);
        String hash = toHexString(hashBytes);

        md.reset();
        return hash;
    } catch (NoSuchAlgorithmException e) {
        Log.e("FDroid", "Device does not support " + algo + " MessageDisgest algorithm");
        return null;
    }
}

From source file:Main.java

public static String generateMarvelHash(String publicKey, String privateKey) {

    String marvelHash = "";

    try {/*from  ww w  .  ja  va  2  s .c  o m*/
        String timeStamp = getUnixTimeStamp();
        String marvelData = timeStamp + privateKey + publicKey;

        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] hash = messageDigest.digest(marvelData.getBytes());

        StringBuilder stringBuilder = new StringBuilder(2 * hash.length);

        for (byte b : hash)
            stringBuilder.append(String.format("%02x", b & 0xff));

        marvelHash = stringBuilder.toString();

    } catch (NoSuchAlgorithmException e) {

        System.out.println("[DEBUG]" + " MarvelApiUtils generateMarvelHash - " + "NoSuchAlgorithmException");
    }

    return marvelHash;
}