锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
* MD5鍔犲瘑
*
* @param s
* 瑕佸姞瀵嗙殑瀛楃涓?涓嶅彲涓簄ull)
*
* @return 榪斿洖鍔犲瘑鍚庣殑瀵嗘枃
*/
public final static String MD5(String pwd) {
String Digest = "";
try {
MessageDigest currentAlgorithm = MessageDigest.getInstance("md5");
currentAlgorithm.reset();
byte[] mess = pwd.getBytes();// 鑾峰彇瑕佸姞瀵嗙殑byte鏁扮粍
byte[] hash = currentAlgorithm.digest(mess);// 鑾峰彇鍔犲瘑浠ュ悗鐨刡yte鏁扮粍
for (int i = 0; i < hash.length; i++) {
int v = hash[i];
if (v < 0) {
v = 256 + v; // 濡傛灉 v < 0 > 0 鐨勬暟錛屽惁鍒欎笉濂借漿鎹㈡垚瀛楃錛屽摢鏈堿SC浠g爜<0鐨勫憖
}
if (v < 16) {
Digest += "0"; // 濡傛灉璇<1616榪涘埗鏁板氨鏄彧鏈変釜浣嶏紝渚嬪15杞崲灝辨垚闀垮害灝辯浉絳変簡
}
Digest += Integer.toString(v, 16).toUpperCase();
}
mess = null;
hash = null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return Digest;
}
public class CheckCodeTag extends TagSupport {
private String id;
private String height;
private String width;
public CheckCodeTag() {
}
@SuppressWarnings( { "unchecked", "static-access" })
public int doStartTag() throws JspException {
StringBuffer html = new StringBuffer();
if (height == null || height.length() == 0)
height = "18";
if (width == null || width.length() == 0)
width = "60";
html.append("<img alt=\"閲嶆柊鑾峰彇楠岃瘉鐮乗"");
if (id != null && id.length() > 0) {
html.append(" id=\"");
html.append(id);
html.append("\"");
}
html.append(" height=\"");
html.append(height);
html.append("\" width=\"");
html.append(width);
html.append("\" src=\"/checkCodeImg\" onclick=\"this.src='/checkCodeImg?now='+new Date();\" style=\"cursor: pointer\" />");
try {
pageContext.getOut().println(html.toString());
} catch (Exception e) {
throw new JspException(e.getMessage());
}
return this.SKIP_BODY;
}
public void setId(String id) {
this.id = id;
}
public void setHeight(String height) {
this.height = height;
}
public void setWidth(String width) {
this.width = width;
}
}
public class CheckCodeImg extends HttpServlet {
private Font mFont = new Font("Arial black", Font.PLAIN, 16);
public void init() throws ServletException {
super.init();
}
Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/png");
int width = 60, height = 18;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(mFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}
char[] codes = { // 'a','b','c','d','e','f','h','k','m','n','r','s','t','x','y',//15涓?br />
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
// 'A','B','C','D','E','F','G','H','K','N','S','T','X','Y'//14
};
String sRand = "";
for (int i = 0; i < 4; i++) {
int j = random.nextInt(10);
String tmp = String.valueOf(codes[j]);
sRand += tmp;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(tmp, 15 * i + 2, 15);
}
HttpSession session = request.getSession(true);
session.setAttribute(MyConstant.SESSION_CHECKCODE, sRand);
g.dispose();
ImageIO.write(image, "PNG", response.getOutputStream());
}
}