ShaUtils.java 930 B

123456789101112131415161718192021222324252627282930313233
  1. package com.gree.mall.contest.utils;
  2. import java.nio.charset.StandardCharsets;
  3. import java.security.MessageDigest;
  4. public class ShaUtils {
  5. public static String getSha1(String str){
  6. if(str==null||str.length()==0){
  7. return null;
  8. }
  9. char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9',
  10. 'a','b','c','d','e','f'};
  11. try {
  12. MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
  13. mdTemp.update(str.getBytes(StandardCharsets.UTF_8));
  14. byte[] md = mdTemp.digest();
  15. int j = md.length;
  16. char[] buf = new char[j*2];
  17. int k = 0;
  18. for (byte byte0 : md) {
  19. buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
  20. buf[k++] = hexDigits[byte0 & 0xf];
  21. }
  22. return new String(buf);
  23. } catch (Exception e) {
  24. return null;
  25. }
  26. }
  27. }