@Pollux
2016-07-23T03:22:00.000000Z
字数 1554
阅读 798
web
# com.pollux.md5.md5.java
package com.pollux.md5;
public interface md5 {
public String getmd5(String name, String password);
}
# com.pollux.md5.md5impl.java
package com.pollux.md5;
import java.security.MessageDigest;
import org.springframework.stereotype.Service;
@Service("md5")
public class md5impl implements md5 {
public String getmd5(String name, String password) {
// TODO Auto-generated method stub
// long time = System.currentTimeMillis();
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
try {
String salt = "Pollux";
String addsalt = password + name + salt;
byte[] btinput = addsalt.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btinput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
// long timenow = System.currentTimeMillis();
// System.out.println(timenow-time);
String result = new String(str);
String finalstring = result.substring(5,result.length()-3);
return finalstring;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
# com.pollux.controller.Testmd5
package com.pollux.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pollux.md5.md5;
@Controller
public class Testmd5 {
@Autowired
private md5 md5;
@ResponseBody
@RequestMapping(value = "/md5")
public String md5(String username,String password){
return md5.getmd5(username, password);
}
}