package com.roadway.website.util;
import java.security.MessageDigest;

/** *//**
* @discription:A tool of Secutity by SHA
* @author :Hui Wanpeng
* @time :2008-1-25
* @version :1.0
* @see :no
*/
public class Security
{
private synchronized static byte[] encode(String origin)
{
byte[] hash = null;
try
{
MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
hash = md.digest(origin.getBytes());
} catch (Exception ex)
{
ex.printStackTrace();
}
return hash;
}

public synchronized static String getPassword(String origin)
{
String result = "";
byte[] hash = Security.encode(origin);
for (int i = 0; i < hash.length; i++)
{
int itemp = hash[i]&0xFF;
if(itemp<16) result += "0";
result += Integer.toString(itemp, 16).toUpperCase();
}
return result;
}

public synchronized static boolean isPassword(String origin, String result)
{
if (Security.getPassword(origin).equals(result))
{
return true;
}
return false;
}
public static void main(String[] args)
{
String result = "";
result = Security.getPassword("huiwanpeng");
System.out.println(result);
}
}
本文介绍了一个使用MD5算法进行密码加密的Java工具类实现。该工具类提供了密码的编码、验证等功能,适用于简单的安全场景需求。
293

被折叠的 条评论
为什么被折叠?



