Java Digest digest(String orgin, String algorithm)

Here you can find the source of digest(String orgin, String algorithm)

Description

digest

License

Apache License

Declaration

public static String digest(String orgin, String algorithm) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.security.MessageDigest;

public class Main {

    public static String digest(String orgin, String algorithm) {

        try {/*from  www. ja v  a  2 s  . c o m*/
            byte[] strTemp = orgin.getBytes("utf-8");
            MessageDigest mdTemp = MessageDigest.getInstance(algorithm);
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();

            return bytes2hex(md);
        } catch (Exception e) {
            return null;
        }
    }

    public static String bytes2hex(byte[] bts) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        int j = bts.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = bts[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    }
}

Related

  1. digest(String input, Charset charset)
  2. digest(String input, String algorithm, String encoding)
  3. digest(String key)
  4. digest(String message, byte[] salt)
  5. digest(String name, String source)
  6. digest(String password)
  7. digest(String plain, String algorithm)
  8. digest(String planeText)
  9. digest(String provider, File file, int radix)