[关闭]
@Pollux 2016-07-23T03:22:00.000000Z 字数 1554 阅读 798

md5加密

web


# com.pollux.md5.md5.java

  1. package com.pollux.md5;
  2. public interface md5 {
  3. public String getmd5(String name, String password);
  4. }

# com.pollux.md5.md5impl.java

  1. package com.pollux.md5;
  2. import java.security.MessageDigest;
  3. import org.springframework.stereotype.Service;
  4. @Service("md5")
  5. public class md5impl implements md5 {
  6. public String getmd5(String name, String password) {
  7. // TODO Auto-generated method stub
  8. // long time = System.currentTimeMillis();
  9. char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  10. try {
  11. String salt = "Pollux";
  12. String addsalt = password + name + salt;
  13. byte[] btinput = addsalt.getBytes();
  14. MessageDigest mdInst = MessageDigest.getInstance("MD5");
  15. mdInst.update(btinput);
  16. byte[] md = mdInst.digest();
  17. int j = md.length;
  18. char str[] = new char[j * 2];
  19. int k = 0;
  20. for (int i = 0; i < j; i++) {
  21. byte byte0 = md[i];
  22. str[k++] = hexDigits[byte0 >>> 4 & 0xf];
  23. str[k++] = hexDigits[byte0 & 0xf];
  24. }
  25. // long timenow = System.currentTimeMillis();
  26. // System.out.println(timenow-time);
  27. String result = new String(str);
  28. String finalstring = result.substring(5,result.length()-3);
  29. return finalstring;
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. return null;
  33. }
  34. }
  35. }

# com.pollux.controller.Testmd5

  1. package com.pollux.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import com.pollux.md5.md5;
  7. @Controller
  8. public class Testmd5 {
  9. @Autowired
  10. private md5 md5;
  11. @ResponseBody
  12. @RequestMapping(value = "/md5")
  13. public String md5(String username,String password){
  14. return md5.getmd5(username, password);
  15. }
  16. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注