本文整理汇总了Java中org.apache.shiro.crypto.SecureRandomNumberGenerator类的典型用法代码示例。如果您正苦于以下问题:Java SecureRandomNumberGenerator类的具体用法?Java SecureRandomNumberGenerator怎么用?Java SecureRandomNumberGenerator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SecureRandomNumberGenerator类属于org.apache.shiro.crypto包,在下文中一共展示了SecureRandomNumberGenerator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testHashService
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
@Test
public void testHashService(){
//默认也是好似用sha512
DefaultHashService hashService = new DefaultHashService();
hashService.setHashAlgorithmName("SHA-512");
//私盐
hashService.setPrivateSalt(new SimpleByteSource("123"));
//是否生成公盐
hashService.setGeneratePublicSalt(true);
//用于生成公盐,默认就这个
hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());
//生成hash值的迭代次数
hashService.setHashIterations(1);
HashRequest request = new HashRequest.Builder().setAlgorithmName("MD5")
.setSource(ByteSource.Util.bytes("hello"))
.setSalt(ByteSource.Util.bytes("123"))
.setIterations(2)
.build();
String hex = hashService.computeHash(request).toHex();
System.out.println(hex);
}
示例2: bind
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
public static void bind(ServiceBinder binder) {
binder.bind(RandomNumberGenerator.class, SecureRandomNumberGenerator.class);
binder.bind(ConfigurationService.class, ConfigurationServiceImpl.class);
binder.bind(VdrDataService.class, VdrDataServiceImpl.class);
binder.bind(EpgDataService.class, EpgDataServiceFacadeImpl.class);
binder.bind(EpgImageService.class, EpgImageServiceFacadeImpl.class);
binder.bind(CommandService.class, CommandServiceImpl.class);
binder.bind(EpgdSearchTimerService.class, EpgdSearchTimerServiceImpl.class);
binder.bind(SvdrpNashornService.class, SvdrpNashornServiceImpl.class);
binder.bind(Epg2VdrNashornService.class, Epg2VdrNashornServiceImpl.class);
binder.bind(ChannelMapService.class, ChannelMapServiceImpl.class);
binder.bind(UserService.class, UserServiceImpl.class);
binder.bind(ChannelEncoder.class);
binder.bind(GlobalLogoFilename.class);
binder.bind(GlobalValues.class);
}
示例3: setupEnvironment
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
@Contribute(SymbolProvider.class)
@ApplicationDefaults
public static void setupEnvironment(MappedConfiguration<String, Object> configuration) {
configuration.add(SymbolConstants.JAVASCRIPT_INFRASTRUCTURE_PROVIDER, "jquery");
configuration.add(SymbolConstants.BOOTSTRAP_ROOT, "context:jbootstrap");
configuration.add(SymbolConstants.HMAC_PASSPHRASE, new SecureRandomNumberGenerator().getSecureRandom().toString());
configuration.add(SymbolConstants.MINIFICATION_ENABLED, true);
configuration.add(SymbolConstants.ENABLE_HTML5_SUPPORT, true);
configuration.add(SymbolConstants.ENABLE_PAGELOADING_MASK, true);
configuration.add(SymbolConstants.SUPPORTED_LOCALES, "de,en");
configuration.add(SymbolConstants.CLUSTERED_SESSIONS, false);
configuration.add(SymbolConstants.COMBINE_SCRIPTS, true);
configuration.add(SymbolConstants.COMPACT_JSON, true);
configuration.add(SymbolConstants.COMPRESS_WHITESPACE, true);
configuration.add(SymbolConstants.GZIP_COMPRESSION_ENABLED, true);
configuration.add(SymbolConstants.PRELOADER_MODE, PreloaderMode.ALWAYS);
configuration.add("tapestry.closure-compiler-level", "SIMPLE_OPTIMIZATIONS");
// INFO:
// only in production 1 to 5 minutes
// configuration.add(SymbolConstants.FILE_CHECK_INTERVAL, 60);
}
示例4: main
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
public static void main(String[] args) throws NoSuchAlgorithmException {
DefaultHashService hashService = new DefaultHashService(); // 默认算法SHA-512
SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
randomNumberGenerator.setSeed("123".getBytes());
String salt = randomNumberGenerator.nextBytes().toHex();
HashRequest request = new HashRequest.Builder().setAlgorithmName("MD5")
.setSource(ByteSource.Util.bytes("123456")).setSalt(ByteSource.Util.bytes(salt)).setIterations(2)
.build();
String hex = hashService.computeHash(request).toHex();
System.out.println(salt);
System.out.println(hex);
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecretKey deskey = keygen.generateKey();
System.out.println(Base64.encodeToString(deskey.getEncoded()));
}
示例5: securePassword
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
/**
* Шифрует пароль в профиле пользователя. Предпологаетя, что пароль
* находится в <code>user.getPassword()</code> в открытом виде.
*
* @param user профиль пользователя
*/
public static void securePassword(final UserProfile user) {
// We'll use a Random Number Generator to generate salts. This is
// much more secure than using a username as a salt or nothaving a
// salt at all. Shiro makes this easy.
// Note that a normal app would reference an attribute rather than
// create a new RNG every time:
final RandomNumberGenerator rng = new SecureRandomNumberGenerator();
final ByteSource salt = rng.nextBytes();
final String plainTextPassword = user.getPassword();
// Now hash the plain-text password with the random salt and
// multiple
// iterations and then Base64-encode the value (requires less space
// than Hex):
final String hashedPasswordBase64 = new Sha256Hash(plainTextPassword, salt, HASH_ITERATIONS).toBase64();
// saveAndChangeOwner the salt with the new account. The HashedCredentialsMatcher
// will need it later when handling login attempts:
user.setPasswordSalt(salt.toBase64());
user.setPassword(hashedPasswordBase64);
}
示例6: createUserIfMissing
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
/**
* Creates a new user if does not exist. If it exists, it is unlocked and roles are reset (password is untouched).
*
* @param cnx
* @param login
* @param password
* the raw password. it will be hashed.
* @param description
* @param roles
*/
static void createUserIfMissing(DbConn cnx, String login, String password, String description, String... roles)
{
try
{
int userId = cnx.runSelectSingle("user_select_id_by_key", Integer.class, login);
cnx.runUpdate("user_update_enable_by_id", userId);
RUser.set_roles(cnx, userId, roles);
}
catch (NoResultException e)
{
ByteSource salt = new SecureRandomNumberGenerator().nextBytes();
String hash = new Sha512Hash(password, salt, 100000).toHex();
String saltS = salt.toHex();
RUser.create(cnx, login, hash, saltS, roles);
}
}
示例7: save
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
@RequiresPermissions("admin:edit")
@RequestMapping(value = "/admin", method = RequestMethod.POST)
@ResponseBody
public ResponseDTO save(@RequestBody AdminAddAO ao) {
logger.info("请求参数: {}", ao);
String salt = new SecureRandomNumberGenerator().nextBytes().toHex();
ao.setSalt(salt);
adminService.addAdmin(ao);
return new ResponseDTO(1, "success", null);
}
示例8: generatePassword
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
private void generatePassword(User user, String plainTextPassword) {
RandomNumberGenerator rng = new SecureRandomNumberGenerator();
Object salt = rng.nextBytes();
String hashedPasswordBase64 = new Sha256Hash(plainTextPassword, salt, 1024).toBase64();
user.setPassword(hashedPasswordBase64);
user.setSalt(salt.toString());
}
示例9: generatePassword
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
private void generatePassword(User user, String plainTextPassword) {
RandomNumberGenerator rng = new SecureRandomNumberGenerator();
Object salt = rng.nextBytes();
String hashedPasswordBase64 = new Sha256Hash(plainTextPassword, salt,1024).toBase64();
user.setPassword(hashedPasswordBase64);
user.setSalt(salt.toString());
}
示例10: md5Password
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
/**
* 对密码进行md5加密,并返回密文和salt,包含在User对象中
* @param username 用户名
* @param password 密码
* @param hashIterations 迭代次数
* @return UserEntity对象,包含密文和salt
*/
public static UserEntity md5Password(String username,String password,int hashIterations){
SecureRandomNumberGenerator secureRandomNumberGenerator=new SecureRandomNumberGenerator();
String salt= secureRandomNumberGenerator.nextBytes().toHex();
//组合username,两次迭代,对密码进行加密
String password_cryto = new Md5Hash(password,username+salt,hashIterations).toBase64();
UserEntity user=new UserEntity();
user.setPassword(password_cryto);
user.setCredentialsSalt(salt);
user.setUserName(username);
return user;
}
示例11: testGeneratePassword
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
@Test
public void testGeneratePassword(){
String algorithmName = "md5";
String username = "liu";
String password = "123";
String salt1 = username;
String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
int hashIteration = 2;
SimpleHash simpleHash = new SimpleHash(algorithmName, password, salt1 + salt2, hashIteration);
String encodePassword = simpleHash.toHex();
System.out.println(salt2);
System.out.println(encodePassword);
}
示例12: encryptPasswordAndGenSalt
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
private void encryptPasswordAndGenSalt(User user) {
SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator();
generator.setSeed(SEED.getBytes());
String salt = generator.nextBytes().toHex();
user.setSalt(salt);
DefaultHashService hashService = new DefaultHashService();
HashRequest request = new HashRequest.Builder().setAlgorithmName(algorithmName)
.setSource(ByteSource.Util.bytes(user.getPassword())).setSalt(ByteSource.Util.bytes(salt))
.setIterations(hashIterations).build();
user.setPassword(hashService.computeHash(request).toHex());
}
示例13: passwordsMatch
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
public boolean passwordsMatch(User user, String password) {
SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator();
generator.setSeed(SEED.getBytes());
String salt = user.getSalt();
DefaultHashService hashService = new DefaultHashService();
HashRequest request = new HashRequest.Builder().setAlgorithmName(algorithmName)
.setSource(ByteSource.Util.bytes(password)).setSalt(ByteSource.Util.bytes(salt))
.setIterations(hashIterations).build();
return user.getPassword().equals(hashService.computeHash(request).toHex());
}
示例14: setPassword
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
public final void setPassword(String password){
ByteSource salt = new SecureRandomNumberGenerator().nextBytes();
SimpleHash hashedPw = new SimpleHash(Sha256Hash.ALGORITHM_NAME, password, salt);
authcInfo.setCredentials(hashedPw);
authcInfo.setCredentialsSalt(salt);
}
示例15: encryptPassword
import org.apache.shiro.crypto.SecureRandomNumberGenerator; //导入依赖的package包/类
@Override
public void encryptPassword(SysUser user) {
RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
user.setSalt(randomNumberGenerator.nextBytes().toHex());
String password = new SimpleHash(algorithmName, user.getPassword(),
ByteSource.Util.bytes(user.getUsername() + user.getSalt()),
hashIterations).toHex();
user.setPassword(password);
}