java密碼加鹽加密


package AddSalt;
import java.util.UUID;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
 
public class TestPasswordSalt {
    public static void main(String[] args) {
    	String uuid=UUID.randomUUID().toString().replaceAll("-", "");
    	System.out.println("uuid===="+uuid);
        String pwd1 = md5("123456", "admin"+uuid);
        System.out.println(pwd1);
        
        String uuid1=UUID.randomUUID().toString().replaceAll("-", "");
    	System.out.println("uuid1===="+uuid1);
        String pwd2 = md5("123456", "abc"+uuid1);
        System.out.println(pwd2);
        
    }
 
    public static final String md5(String password, String salt){
        //加密方式
        String hashAlgorithmName = "MD5";
        //鹽:相同密碼使用不同的鹽加密后的結果不同
        ByteSource byteSalt = ByteSource.Util.bytes(salt);
        //密碼
        Object source = password;
        //加密次數
        int hashIterations = 2;
        SimpleHash result = new SimpleHash(hashAlgorithmName, source, byteSalt, hashIterations);
        return result.toString();
    }
}

  //pom.xml文件

<dependencies>
	   <dependency>
		   <groupId>org.apache.shiro</groupId>
		   <artifactId>shiro-spring</artifactId>
		   <version>1.3.2</version>
	  </dependency>
  </dependencies>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM