import java.security.MessageDigest;

import org.apache.commons.codec.digest.DigestUtils;

import org.bouncycastle.crypto.Digest;

import org.bouncycastle.crypto.digests.SHA1Digest;

import org.bouncycastle.crypto.digests.SHA224Digest;

import org.bouncycastle.crypto.digests.SHA256Digest;

import org.bouncycastle.crypto.digests.SHA384Digest;

import org.bouncycastle.crypto.digests.SHA512Digest;

import org.bouncycastle.util.encoders.Hex;


/**

 * 安全散列算法

 */

public class SHADemo

{


public static void main(String[] args) throws Exception

{

System.out.println(jdkSha1("测试 test!"));

System.out.println(bcSha1("测试 test!"));

System.out.println(bcSha224("测试 test!"));

System.out.println(bcSha256("测试 test!"));

System.out.println(bcSha384("测试 test!"));

System.out.println(bcSha512("测试 test!"));

}

public static String jdkSha1(String srcString) throws Exception

{

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

byte[] encodeStr = md.digest(srcString.getBytes());

return Hex.toHexString(encodeStr);

}

public static String bcSha1(String srcString) throws Exception

{

Digest digest = new SHA1Digest();

digest.update(srcString.getBytes(),0,srcString.getBytes().length);

byte[] sha1Bytes = new byte[digest.getDigestSize()];

digest.doFinal(sha1Bytes, 0);

return  Hex.toHexString(sha1Bytes);

}

public static String bcSha224(String srcString) throws Exception

{

Digest digest = new SHA224Digest();

digest.update(srcString.getBytes(),0,srcString.getBytes().length);

byte[] sha224Bytes = new byte[digest.getDigestSize()];

digest.doFinal(sha224Bytes, 0);

return  Hex.toHexString(sha224Bytes);

}

public static String bcSha256(String srcString) throws Exception

{

Digest digest = new SHA256Digest();

digest.update(srcString.getBytes(),0,srcString.getBytes().length);

byte[] shaBytes = new byte[digest.getDigestSize()];

digest.doFinal(shaBytes, 0);

return  Hex.toHexString(shaBytes);

}

public static String bcSha384(String srcString) throws Exception

{

Digest digest = new SHA384Digest();

digest.update(srcString.getBytes(),0,srcString.getBytes().length);

byte[] shaBytes = new byte[digest.getDigestSize()];

digest.doFinal(shaBytes, 0);

return  Hex.toHexString(shaBytes);

}

public static String bcSha512(String srcString) throws Exception

{

Digest digest = new SHA512Digest();

digest.update(srcString.getBytes(),0,srcString.getBytes().length);

byte[] shaBytes = new byte[digest.getDigestSize()];

digest.doFinal(shaBytes, 0);

return  Hex.toHexString(shaBytes);

}

public static String ccSha1(String srcString) throws Exception

{

return DigestUtils.sha1Hex(srcString);

}

public static String ccSha256(String srcString) throws Exception

{

return DigestUtils.sha256Hex(srcString);

}

public static String ccSha384(String srcString) throws Exception

{

return DigestUtils.sha384Hex(srcString);

}

public static String ccSha512(String srcString) throws Exception

{

return DigestUtils.sha512Hex(srcString);

}


}