SHAUtil.java 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package com.steerinfo.util;
  2. import java.security.MessageDigest;
  3. /**
  4. * @author Bourne.Cao
  5. * @date 2021-12-29 18:01
  6. */
  7. public class SHAUtil {
  8. /**
  9. * @Comment SHA1实现
  10. * @Author Ron
  11. * @Date 2017年9月13日 下午3:30:36
  12. * @return
  13. */
  14. public static String shaEncode(String inStr) throws Exception {
  15. MessageDigest sha = null;
  16. try {
  17. sha = MessageDigest.getInstance("SHA");
  18. } catch (Exception e) {
  19. //system.out.println(e.toString());
  20. e.printStackTrace();
  21. return "";
  22. }
  23. byte[] byteArray = inStr.getBytes("UTF-8");
  24. byte[] md5Bytes = sha.digest(byteArray);
  25. StringBuffer hexValue = new StringBuffer();
  26. for (int i = 0; i < md5Bytes.length; i++) {
  27. int val = ((int) md5Bytes[i]) & 0xff;
  28. if (val < 16) {
  29. hexValue.append("0");
  30. }
  31. hexValue.append(Integer.toHexString(val));
  32. }
  33. return hexValue.toString();
  34. }
  35. }