/** * Constructs a new {@code DefaultHashService} instance with the following defaults: * <ul> * <li>{@link #setHashAlgorithmName(String) hashAlgorithmName} = {@code SHA-512}</li> * <li>{@link #setHashIterations(int) hashIterations} = {@code 1}</li> * <li>{@link #setRandomNumberGenerator(org.apache.shiro.crypto.RandomNumberGenerator) randomNumberGenerator} = * new {@link SecureRandomNumberGenerator}()</li> * <li>{@link #setGeneratePublicSalt(boolean) generatePublicSalt} = {@code false}</li> * </ul> * <p/> * If this hashService will be used for password hashing it is recommended to set the * {@link #setPrivateSalt(ByteSource) privateSalt} and significantly increase the number of * {@link #setHashIterations(int) hashIterations}. See the class-level JavaDoc for more information. */ public DefaultHashService() { this.algorithmName = "SHA-512"; this.iterations = 1; this.generatePublicSalt = false; this.rng = new SecureRandomNumberGenerator(); }
ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); new Sha512Hash(password, salt).toBase64();
SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator(); int byteSize = generatedSaltSize / 8; //generatedSaltSize is in *bits* - convert to byte size: return generator.nextBytes(byteSize);
/** * Test new Shiro 1.1 functionality, where the salt is obtained from the stored account information, as it * should be. See <a href="https://issues.apache.org/jira/browse/SHIRO-186">SHIRO-186</a> */ @Test public void testSaltedAuthenticationInfo() { //use SHA-1 hashing in this test: HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(Sha1Hash.ALGORITHM_NAME); //simulate a user account with a SHA-1 hashed and salted password: ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); Object hashedPassword = new Sha1Hash("password", salt); SimpleAuthenticationInfo account = new SimpleAuthenticationInfo("username", hashedPassword, salt, "realmName"); //simulate a username/password (plaintext) token created in response to a login attempt: AuthenticationToken token = new UsernamePasswordToken("username", "password"); //verify the hashed token matches what is in the account: assertTrue(matcher.doCredentialsMatch(token, account)); }
private RandomNumberGenerator getRandomNumberGenerator() { if ( randomNumberGenerator == null ) { randomNumberGenerator = new SecureRandomNumberGenerator(); } return randomNumberGenerator; }
RandomNumberGenerator rng = new SecureRandomNumberGenerator(); Object salt = rng.nextBytes(); //Now hash the plain-text password with the random salt and multiple //iterations and then Base64-encode the value (requires less space than Hex): String hashedPasswordBase64 = new Sha256Hash(password, salt, 1024).toBase64();
public static void main(String[] args) { int iterations = 2; SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator(); ByteSource nextByteSource = generator.nextBytes(); Md5Hash md5Hash = new Md5Hash("234", ByteSource.Util.bytes(nextByteSource),iterations); md5Hash.setIterations(iterations); System.out.println("md5hash to hex: "+md5Hash.toHex()); SimpleHash hash = new SimpleHash("md5","234",ByteSource.Util.bytes(nextByteSource),iterations); System.out.println("simple hash to hex: "+hash.toHex()); }
/** * 获取随机盐值 * * @param length 字节长度,一个字节2位16进制数表示 * @return */ public static String getRandomSalt(int length) { return new SecureRandomNumberGenerator().nextBytes(length).toHex(); }
/** * Generates a salt for use with password salting * * @return randomly generated salt in Base64 format */ public static String generateSalt() { // Generate a random salt for a password (password salt is Base64) RandomNumberGenerator rng = new SecureRandomNumberGenerator(); return rng.nextBytes().toBase64(); }
/** * 获取随机盐值 * * @param length 字节长度,一个字节2位16进制数表示 * @return */ public static String getRandomSalt(int length) { return new SecureRandomNumberGenerator().nextBytes(length).toHex(); }
/** * 生成随机盐 */ public static String randomSalt() { // 一个Byte占两个字节,此处生成的3字节,字符串长度为6 SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator(); return secureRandom.nextBytes(3).toHex(); } }
/** * 获取随机盐值 * * @param length 字节长度,一个字节2位16进制数表示 * @return */ public static String getRandomSalt(int length) { return new SecureRandomNumberGenerator().nextBytes(length).toHex(); }
@Override protected byte[] getIv() { RandomNumberGenerator generator = new SecureRandomNumberGenerator(); return generator.nextBytes(getBlockSize()).getBytes(); }
public ConstantKeyAESCBC(byte[] key) { this.key = key; RandomNumberGenerator generator = new SecureRandomNumberGenerator(); iv = generator.nextBytes(aescbc.getBlockSize()).getBytes(); }
public ConstantKeyConstantSuffixAESECB(byte[] suffix, boolean shouldAddRandomPrefix) { RandomNumberGenerator generator = new SecureRandomNumberGenerator(); key = generator.nextBytes(aesecb.getBlockSize()).getBytes(); this.suffix = suffix; this.shouldAddRandomPrefix = shouldAddRandomPrefix; }
private static void encryptPassword(User user) throws UnsupportedEncodingException { RandomNumberGenerator rng = new SecureRandomNumberGenerator(); ByteSource byteSource = rng.nextBytes(); String salt = byteSource.toBase64(); String hashedPasswordBase64 = new Sha256Hash(user.getPassword(), salt, 1024).toBase64(); user.setPassword(hashedPasswordBase64); user.setSalt(salt); }
public static void createUser(DbConn cnx, String login, String password, RRole... roles) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); String[] rr = new String[roles.length]; for (int i = 0; i < roles.length; i++) { rr[i] = roles[i].getName(); } RUser.create(cnx, login, new Sha512Hash(password, salt, 100000).toHex(), salt.toHex(), rr); } }
public static void changeUserPassword(DbConn cnx, int userId, String newPassword) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); String hash = new Sha512Hash(newPassword, salt, 100000).toHex(); QueryResult qr = cnx.runUpdate("user_update_password_by_id", hash, salt.toHex(), userId); if (qr.nbUpdated == 0) { throw new JqmAdminApiUserException("user with this ID does not exist"); } }
public static void createUser(DbConn cnx, String login, String password, RRole... roles) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); String[] rr = new String[roles.length]; for (int i = 0; i < roles.length; i++) { rr[i] = roles[i].getName(); } RUser.create(cnx, login, new Sha512Hash(password, salt, 100000).toHex(), salt.toHex(), rr); } }
public User regist(User user, String addr) { user.setCreateDate(Times.now()); user.setRegisterIp(addr); user.setSystem(false); String salt = new SecureRandomNumberGenerator().nextBytes().toBase64(); user.setSalt(salt); user.setPassword(new Sha256Hash(user.getPassword(), salt, 1024).toBase64()); return dao().insert(user); }