SHA1散列

 1 import java.security.MessageDigest;

 2 import java.security.NoSuchAlgorithmException;

 3 

 4 public class SHA1 {

 5      public String Encrypt(String strSrc) {

 6            MessageDigest md = null;

 7            String strDes = null;

 8 

 9             byte[] bt = strSrc.getBytes();

10             try {

11 

12                 md = MessageDigest. getInstance("SHA-1");

13                 md.update(bt);

14                 strDes = bytes2Hex(md.digest()); // to HexString

15            } catch (NoSuchAlgorithmException e) {

16                 System. out.println( "Invalid algorithm.");

17                  return null;

18            }

19             return strDes;

20      }

21 

22      public String bytes2Hex( byte[] bts) {

23            String des = "";

24            String tmp = null;

25             for ( int i = 0; i < bts. length; i++) {

26                 tmp = (Integer. toHexString(bts[i] & 0xFF));

27                  if (tmp.length() == 1) {

28                      des += "0";

29                 }

30                 des += tmp;

31            }

32             return des;

33      }

34 

35      public static void main(String[] args) {

36            SHA1 te = new SHA1();

37            String strSrc = "abcd";

38            System. out.println( "Use SHA:" + te.Encrypt(strSrc).toUpperCase());

39      }

40 }

你可能感兴趣的:(SHA1)