zoukankan      html  css  js  c++  java
  • JAVA代码MD5加密方法

    PwdEncoder.java 接口类
    package com.common.security.encoder;
    
    /**
     * 密码加密接口
     */
    public interface PwdEncoder {
    	/**
    	 * 密码加密
    	 * 
    	 * @param rawPass
    	 *            未加密密码,null作为空串
    	 * @return 加密后密码
    	 */
    	public String encodePassword(String rawPass);
    
    	/**
    	 * 密码加密
    	 * 
    	 * @param rawPass
    	 *            未加密密码,null作为空串
    	 * @param salt
    	 *            混淆码
    	 * @return
    	 */
    	public String encodePassword(String rawPass, String salt);
    
    	/**
    	 * 验证密码是否正确
    	 * 
    	 * @param encPass
    	 *            加密密码
    	 * @param rawPass
    	 *            未加密密码,null作为空串
    	 * @return true:密码正确;false:密码错误
    	 */
    	public boolean isPasswordValid(String encPass, String rawPass);
    
    	/**
    	 * 验证密码是否正确
    	 * 
    	 * @param encPass
    	 *            加密密码
    	 * @param rawPass
    	 *            未加密密码,null作为空串
    	 * @param salt
    	 *            混淆码
    	 * @return true:密码正确;false:密码错误
    	 */
    	public boolean isPasswordValid(String encPass, String rawPass, String salt);
    }
    

      

    Md5PwdEncoder.java
    package com.common.security.encoder;
    
    import java.io.UnsupportedEncodingException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    import org.apache.commons.codec.binary.Hex;
    
    /**
     * MD5密码加密
     */
    public class Md5PwdEncoder implements PwdEncoder {
    	public String encodePassword(String rawPass) {
    		return encodePassword(rawPass, defaultSalt);
    	}
    
    	public String encodePassword(String rawPass, String salt) {
    		String saltedPass = mergePasswordAndSalt(rawPass, salt, false);
    		MessageDigest messageDigest = getMessageDigest();
    		byte[] digest;
    		try {
    			digest = messageDigest.digest(saltedPass.getBytes("UTF-8"));
    		} catch (UnsupportedEncodingException e) {
    			throw new IllegalStateException("UTF-8 not supported!");
    		}
    		return new String(Hex.encodeHex(digest));
    	}
    
    	public boolean isPasswordValid(String encPass, String rawPass) {
    		return isPasswordValid(encPass, rawPass, defaultSalt);
    	}
    
    	public boolean isPasswordValid(String encPass, String rawPass, String salt) {
    		if (encPass == null) {
    			return false;
    		}
    		String pass2 = encodePassword(rawPass, salt);
    		return encPass.equals(pass2);
    	}
    
    	protected final MessageDigest getMessageDigest() {
    		String algorithm = "MD5";
    		try {
    			return MessageDigest.getInstance(algorithm);
    		} catch (NoSuchAlgorithmException e) {
    			throw new IllegalArgumentException("No such algorithm ["
    					+ algorithm + "]");
    		}
    	}
    
    	/**
    	 * Used by subclasses to extract the password and salt from a merged
    	 * <code>String</code> created using
    	 * {@link #mergePasswordAndSalt(String,Object,boolean)}.
    	 * <p>
    	 * The first element in the returned array is the password. The second
    	 * element is the salt. The salt array element will always be present, even
    	 * if no salt was found in the <code>mergedPasswordSalt</code> argument.
    	 * </p>
    	 * 
    	 * @param mergedPasswordSalt
    	 *            as generated by <code>mergePasswordAndSalt</code>
    	 * 
    	 * @return an array, in which the first element is the password and the
    	 *         second the salt
    	 * 
    	 * @throws IllegalArgumentException
    	 *             if mergedPasswordSalt is null or empty.
    	 */
    	protected String mergePasswordAndSalt(String password, Object salt,
    			boolean strict) {
    		if (password == null) {
    			password = "";
    		}
    		if (strict && (salt != null)) {
    			if ((salt.toString().lastIndexOf("{") != -1)
    					|| (salt.toString().lastIndexOf("}") != -1)) {
    				throw new IllegalArgumentException(
    						"Cannot use { or } in salt.toString()");
    			}
    		}
    		if ((salt == null) || "".equals(salt)) {
    			return password;
    		} else {
    			return password + "{" + salt.toString() + "}";
    		}
    	}
    
    	/**
    	 * 混淆码。防止破解。
    	 */
    	private String defaultSalt;
    
    	/**
    	 * 获得混淆码
    	 * 
    	 * @return
    	 */
    	public String getDefaultSalt() {
    		return defaultSalt;
    	}
    
    	/**
    	 * 设置混淆码
    	 * 
    	 * @param defaultSalt
    	 */
    	public void setDefaultSalt(String defaultSalt) {
    		this.defaultSalt = defaultSalt;
    	}
    }
    

      

     调用:

    public static void main(String[] args) {
    		PwdEncoder pwdEncoder=new Md5PwdEncoder();
    		System.out.println(pwdEncoder.encodePassword("11111"));
    	}
    

      

  • 相关阅读:
    Nubia Z5S官方4.4 UI2.0音频Audio部分简单分析(也适用于其它8974/8064机型)以及降低破音出现几率的方法
    import MySQLdb UserWarning
    Visual Studio 2013 Update2
    cocos2d 重写顶点着色语言
    tomcat之组成结构
    4、Android Activity的生命周期 Activity的生命周期
    学习中遇到的c++问题,持续更新
    Android开发之发送邮件功能的实现(源码分享)
    Java 类型, Hibernate 映射类型及 SQL 类型之间的相应关系
    QT内label控件通过opencv显示图像
  • 原文地址:https://www.cnblogs.com/pxblog/p/10528162.html
Copyright © 2011-2022 走看看