832f583e9ede9b2b5cb59332c259519d60d8f679.svn-base 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package com.chinacreator.videoalliance.smc.util;
  2. import java.security.MessageDigest;
  3. public class SHAUtil {
  4. /***
  5. * SHA加密 生成40位SHA码
  6. * @param 待加密字符串
  7. * @return 返回40位SHA码
  8. */
  9. public static String shaEncode(String inStr) throws Exception {
  10. MessageDigest sha = null;
  11. try {
  12. sha = MessageDigest.getInstance("SHA");
  13. } catch (Exception e) {
  14. System.out.println(e.toString());
  15. e.printStackTrace();
  16. return "";
  17. }
  18. byte[] byteArray = inStr.getBytes("UTF-8");
  19. byte[] md5Bytes = sha.digest(byteArray);
  20. StringBuffer hexValue = new StringBuffer();
  21. for (int i = 0; i < md5Bytes.length; i++) {
  22. int val = ((int) md5Bytes[i]) & 0xff;
  23. if (val < 16) {
  24. hexValue.append("0");
  25. }
  26. hexValue.append(Integer.toHexString(val));
  27. }
  28. return hexValue.toString();
  29. }
  30. /**
  31. * 测试主函数
  32. * @param args
  33. * @throws Exception
  34. */
  35. public static void main(String args[]) throws Exception {
  36. String str = new String("amigoxiexiexingxing");
  37. System.out.println("原始:" + str);
  38. System.out.println("SHA后:" + shaEncode(str));
  39. }
  40. }