/**
* SHA加密算法加盐
* @author mcrking
*
*/
public class SHAUtil {
/**
* SHA
* @param msg
* @return
*/
public static String encryptSHA(String msg,String salt) {
StringBuilder sb = new StringBuilder();
try{
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(salt.getBytes());
byte[] bytes = md.digest(msg.getBytes());
for(int i=0; i< bytes.length ;i++){
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
}catch(Exception e){
}
return sb.toString().trim();
}
/**
* 获取sha的salt
* @return
*/
public static String getSaltSHA1(){
SecureRandom sr;
byte[] salt = new byte[16];
try {
sr = SecureRandom.getInstance("SHA1PRNG");
sr.nextBytes(salt);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return salt.toString();
}
Boolean checkPW(String userId ,String pwd){
boolean flag = false;
User user = UserServer.getUserInfo(String userId);
String newPwd = encryptSHA(pwd ,user.getSalt());
if(newPwd.equals(user.getPwd()){
flag = true;
}
return flag;
}
评论