자바 암호 화 SHA 에 소금 추가

1630 단어 자바
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

public class SHA {
	public static final String KEY_SHA = "SHA";   
	public static final String ra="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

	/**
	 *    sha  
	 * @param s        
	 * @return        
	 */
	public static String sha(String s)
	{
	    BigInteger sha =null;
	    byte[] bys = s.getBytes();   
	    try {
	         MessageDigest messageDigest = MessageDigest.getInstance(KEY_SHA);  
	         messageDigest.update(bys);
	         sha = new BigInteger(messageDigest.digest());
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    return sha.toString(32);
	}
	
	/**
	 *    +    sha  
	 * @param s        
	 * @return          
	 */	
	public static Map getResult(String s){
		Map map=new HashMap();
		String salt=getSalt();
		map.put("salt", salt);// 
		map.put("password", sha(s+salt));//      
 	    return map;
	}
	
	/**
	 *      
	 * @return       
	 */
	private static String getSalt() {
		SecureRandom random=new SecureRandom();
		int length=random.nextInt(5)+8;//    ,   8-12     
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = ra.charAt(random.nextInt(ra.length()));
        }
        return new String(text);
    }
	
	/**
	 *     
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(getResult("12345678"));
	}

좋은 웹페이지 즐겨찾기