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 encode(String string) {
    try {/*  w ww .j a  v a  2 s . c om*/
        MessageDigest e = MessageDigest.getInstance("MD5");
        return bytesToHexString(e.digest(string.getBytes()));
    } catch (NoSuchAlgorithmException var2) {
        var2.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String encode(String text) {
    try {/*from w w w . j  ava2  s.  c  o m*/
        MessageDigest digest = MessageDigest.getInstance("md5");
        byte[] result = digest.digest(text.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte b : result) {
            int number = b & 0xff;
            String hex = Integer.toHexString(number);
            if (hex.length() == 1) {
                sb.append("0" + hex);
            } else {
                sb.append(hex);
            }
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String getCallSignature(String subAccountSid, String subAccountToken) {
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    String timestamp = format.format(new Date());
    String signature = subAccountSid + subAccountToken + timestamp;
    try {/*w ww.j ava  2s. co  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        return byte2HexStr(md.digest(signature.getBytes("utf-8")));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:at.gv.egiz.pdfas.web.helper.DigestHelper.java

public static String getHexEncodedHash(byte[] data, String algorithm) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algorithm);
    byte[] hash = md.digest(data);
    return Hex.encodeHexString(hash);
}

From source file:Main.java

public static String generateMarvelHash(String publicKey, String privateKey) {
    String marvelHash = "";
    try {//  w  w  w.  j a v  a 2s. co 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;
}

From source file:Main.java

public static byte[] toMD5(byte[] bytes) throws Exception {
    try {/*w  w w . j a  v a 2s.  c o  m*/
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        return algorithm.digest(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception(e);
    }
}

From source file:Main.java

/**
 * Returns a String Object which contains the SHA256 Hash value of the input
 *
 * @param input a String object for which SHA256 should be calculated
 * @return SHA256 of the input String//w  w w  .jav a 2  s.c  om
 */
public static String getSHA256(String input) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(input.getBytes("UTF-8"));
        StringBuilder hexString = new StringBuilder();

        for (int i = 0; i < hash.length; i++) {
            String hex = Integer.toHexString(0xff & hash[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static void decrypt(String fileIn, String fileOut, byte key[])
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(fileIn);
    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");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);

    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;/*from  ww  w .  j  av  a  2 s. co m*/
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }

    fos.flush();
    fos.close();
    cis.close();
}

From source file:Main.java

public static String getHash(String input) {
    if (input == null || input.equals("") || input.isEmpty()) {
        return "";
    }/* w w w.j a  v a 2 s.c o  m*/

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] messageDigest = md.digest(input.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            sb.append(Integer.toString((messageDigest[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}

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

public static String createSHA1(String email, byte[] pwd) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    String base = email + ":" + new String(pwd);
    byte[] result = md.digest(base.getBytes());
    return new String(Base64.encode(result));
}