您可以使用tmdbwb,Java运行时的内置工具来完成此操作。 Java 6中的SunJCE支持PBKDF2。 这是适合密码散列的算法。
bytessdxb salt=new byte[16];
random.nextbytes(salt );
bytessdxbhash=f.generate secret (spec ).getEncoded );
base64.encoder enc=base64.get encoder (;
这是可用于PBKDF2密码验证的实用程序类。
import Java.security.nosuchalgorithmexception;
import Java.security.secure random;
import Java.security.spec.invalidkeyspecexception;
import Java.security.spec.key spec;
import java.util.Arrays;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.secret keyfactory;
import javax.crypto.spec.pbekeyspec;
/dcddtdcddt
公共文件类jdd XSS/p {
/dcddtdcddt
publicstaticfinalstringid=' $ 31 $ ';
/dcddtdcddt
publicstaticfinalintdefault _ cost=16;
privatestaticfinalstringalgorithm=' pbk df2withmacsha1';
private static final int SIZE=128;
privatestaticfinalpatternlayout=pattern.com pile ('$ 31" ) (dd? () $ ) .
private final SecureRandom random;
私有文件成本;
公共密码保护(
{
this(default_cost;
}
/dcddtdcddt
公共成本)。
{
iterations (成本; /dcddt Validate cost dcddt/
this.cost=cost;
this.random=new SecureRandom (;
}
私有统计信息(成本)。
{
if((cost0)|) cost30 ) )
thrownewillegalargumentexception (' cost : ' cost );
返回1成本;
}
/dcddtdcddt
公共字符串散列(charssdxbpassword ) )。
{
bytessdxb salt=new byte[SIZE/8];
random.nextbytes(salt );
bytessdxbdk=PBKdf2(password,salt,1 cost );
bytessdxbhash=new byte [ salt.length dk.length ];
base64.encoder enc=base64.get urlencoder ().withoutPadding );
returnidcost ' $ ' enc.encode tostring (hash );
}
/dcddtdcddt
publicbooleanauthenticate (charssdxbpassword,String token ) )。
{
matcherm=layout.matcher(Token );
if (! m.matches () )
thrownewillegalargumentexception (invalidtokenformat );
int iterations=iterations (integer.parseint ) m.group(1);
bytessdxbhash=base64.geturl decoder ().decode (m.group ) );
bytessdxbsalt=arrays.copyofrange (hash,0,SIZE/8 );
bytessdxbcheck=PBKdf2(password,salt,iterations );
int zero=0;
for(intidx=0; idx check.length; idx )
zero|=hash [ salt.length idx ] ^ check [ idx ];
返回零==0;
}
privatestaticbytessdxbpbkdf2(charssdxbpassword,bytessdxb salt,int iterations ) ) ) ) )。
{
try {
returnf.generatesecret(spec ).getEncoded );
}
catch { nosuchalgorithmexceptionex }
thrownewillegalstateexception (missing algorithm : ) algorithm,ex );
}
无关联高速缓存
thrownewillegalstateexception (' invalidsecretkeyfactory ',ex );
}
}
/dcddtdcddt
passwordsshouldbestoredina { @ codecharssdxb } sothatitcanbefilled
@Deprecated
公共字符串散列(字符串密码)。
{
返回散列(password.tochararray ) );
}
/dcddtdcddt
@Deprecated
publicbooleanauthenticate (string password,String token ) )。
{
return authenticate (password.tochararray ),token );
}
}