📄 mtokengenerator.java
字号:
package net.jumperz.security;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import net.jumperz.util.MStringUtil;
public class MTokenGenerator
{
private SecureRandom random;
public static final String ALG = "SHA1PRNG";
public static final int BYTE_LEN = 16;
private static MTokenGenerator instance;
static
{
try
{
instance = new MTokenGenerator();
}
catch( Exception e )
{
e.printStackTrace();
}
}
// --------------------------------------------------------------------------------
public static String getToken2( int len )
{
return instance.getToken( len );
}
// --------------------------------------------------------------------------------
public static String getToken2()
{
return instance.getToken();
}
// --------------------------------------------------------------------------------
public MTokenGenerator()
throws NoSuchAlgorithmException
{
random = SecureRandom.getInstance( ALG );
byte[] seed = random.generateSeed( BYTE_LEN );
random.setSeed( seed );
}
// --------------------------------------------------------------------------------
public synchronized String getToken( int len )
{
byte[] buf = new byte[ len ];
synchronized( random )
{
random.nextBytes( buf );
}
return MStringUtil.byteToHexString( buf );
}
// --------------------------------------------------------------------------------
public synchronized String getToken()
{
return getToken( BYTE_LEN );
}
// --------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -