protected AbstractPasswordEncoder() { this.saltGenerator = KeyGenerators.secureRandom(); }
@Test public void string() { StringKeyGenerator keyGenerator = KeyGenerators.string(); String hexStringKey = keyGenerator.generateKey(); assertThat(hexStringKey.length()).isEqualTo(16); assertThat(Hex.decode(hexStringKey)).hasSize(8); String hexStringKey2 = keyGenerator.generateKey(); assertThat(hexStringKey.equals(hexStringKey2)).isFalse(); }
@Test public void shared() throws Exception { BytesKeyGenerator keyGenerator = KeyGenerators.shared(21); assertThat(keyGenerator.getKeyLength()).isEqualTo(21); byte[] key = keyGenerator.generateKey(); assertThat(key).hasSize(21); byte[] key2 = keyGenerator.generateKey(); assertThat(Arrays.equals(key, key2)).isTrue(); }
@Override public String generateSalt() { return KeyGenerators.string().generateKey(); } }
@Test public void correctPrefixCaseIsUsed() { this.sha.setForceLowerCasePrefix(false); assertThat(this.sha.encode("somepassword").startsWith("{SSHA}")); this.sha.setForceLowerCasePrefix(true); assertThat(this.sha.encode("somepassword").startsWith("{ssha}")); this.sha = new LdapShaPasswordEncoder(KeyGenerators.shared(0)); this.sha.setForceLowerCasePrefix(false); assertThat(this.sha.encode("somepassword").startsWith("{SHA}")); this.sha.setForceLowerCasePrefix(true); assertThat(this.sha.encode("somepassword").startsWith("{SSHA}")); }
BouncyCastleAesBytesEncryptor(String password, CharSequence salt) { this(password, salt, KeyGenerators.secureRandom(16)); }
/** * Initialize Utility class * @param springEnvironment Spring environment */ public static void init(Environment springEnvironment) { Environment environment = springEnvironment; encoding = environment.getProperty(AweConstants.PROPERTY_APPLICATION_ENCODING, "UTF-8"); masterKey = environment.getProperty(AweConstants.PROPERTY_SECURITY_MASTER_KEY, "4W3M42T3RK3Y%$ED"); aes = new Crypto.AES(); keyGenerator = KeyGenerators.string(); }
public LdapShaPasswordEncoder() { this(KeyGenerators.secureRandom()); }
/** * Generates password with 16 characters length with Spring standard key generator. * * @return password with 16 characters */ public static String generatePassword() { StringKeyGenerator generator = KeyGenerators.string(); return generator.generateKey(); }
protected AbstractPasswordEncoder() { this.saltGenerator = KeyGenerators.secureRandom(); }
public String generateRandomString(){ StringKeyGenerator keyGenerator = KeyGenerators.string(); return keyGenerator.generateKey(); }
public LdapShaPasswordEncoder() { this(KeyGenerators.secureRandom()); }
/** * Crypt a (non-null) byte array into a token * * @param data A byte array to turn into a token * @return A reversible token that contains the data */ protected String cryptBytes(byte[] data) { final String salt = KeyGenerators.string().generateKey(); BytesEncryptor encryptor = new BouncyCastleAesGcmBytesEncryptor(secret, salt); String encrypted = Base64.getUrlEncoder().encodeToString(encryptor.encrypt(data)); return salt + "." + encrypted; }
BouncyCastleAesBytesEncryptor(String password, CharSequence salt) { this(password, salt, KeyGenerators.secureRandom(16)); }
/** * Creates an instance with the provided key length and encoder. * @param encoder the encoder to use * @param keyLength the key length to use */ public Base64StringKeyGenerator(Base64.Encoder encoder, int keyLength) { if (encoder == null) { throw new IllegalArgumentException("encode cannot be null"); } if (keyLength < DEFAULT_KEY_LENGTH) { throw new IllegalArgumentException("keyLength must be greater than or equal to" + DEFAULT_KEY_LENGTH); } this.encoder = encoder; this.keyGenerator = KeyGenerators.secureRandom(keyLength); }
/** * Creates an instance with the provided key length and encoder. * @param encoder the encoder to use * @param keyLength the key length to use */ public Base64StringKeyGenerator(Base64.Encoder encoder, int keyLength) { if (encoder == null) { throw new IllegalArgumentException("encode cannot be null"); } if (keyLength < DEFAULT_KEY_LENGTH) { throw new IllegalArgumentException("keyLength must be greater than or equal to" + DEFAULT_KEY_LENGTH); } this.encoder = encoder; this.keyGenerator = KeyGenerators.secureRandom(keyLength); }
/** * Creates a {@link StringKeyGenerator} that hex-encodes {@link SecureRandom} keys of * 8 bytes in length. The hex-encoded string is keyLength * 2 characters in length. */ public static StringKeyGenerator string() { return new HexEncodingStringKeyGenerator(secureRandom()); }
/** * Creates a {@link StringKeyGenerator} that hex-encodes {@link SecureRandom} keys of * 8 bytes in length. The hex-encoded string is keyLength * 2 characters in length. */ public static StringKeyGenerator string() { return new HexEncodingStringKeyGenerator(secureRandom()); }
/** * Creates a standard password-based bytes encryptor using 256 bit AES encryption. * Derives the secret key using PKCS #5's PBKDF2 (Password-Based Key Derivation * Function #2). Salts the password to prevent dictionary attacks against the key. The * provided salt is expected to be hex-encoded; it should be random and at least 8 * bytes in length. Also applies a random 16 byte initialization vector to ensure each * encrypted message will be unique. Requires Java 6. * * @param password the password used to generate the encryptor's secret key; should * not be shared * @param salt a hex-encoded, random, site-global salt value to use to generate the * key */ public static BytesEncryptor standard(CharSequence password, CharSequence salt) { return new AesBytesEncryptor(password.toString(), salt, KeyGenerators.secureRandom(16)); }
/** * Creates a standard password-based bytes encryptor using 256 bit AES encryption with * Galois Counter Mode (GCM). Derives the secret key using PKCS #5's PBKDF2 * (Password-Based Key Derivation Function #2). Salts the password to prevent * dictionary attacks against the key. The provided salt is expected to be * hex-encoded; it should be random and at least 8 bytes in length. Also applies a * random 16 byte initialization vector to ensure each encrypted message will be * unique. Requires Java 6. * * @param password the password used to generate the encryptor's secret key; should * not be shared * @param salt a hex-encoded, random, site-global salt value to use to generate the * key * * @see #standard(CharSequence, CharSequence) which uses the slightly weaker CBC mode * (instead of GCM) */ public static BytesEncryptor stronger(CharSequence password, CharSequence salt) { return new AesBytesEncryptor(password.toString(), salt, KeyGenerators.secureRandom(16), CipherAlgorithm.GCM); }