@Override public String generateKey() { byte[] key = this.keyGenerator.generateKey(); byte[] base64EncodedKey = this.encoder.encode(key); return new String(base64EncodedKey); } }
public String encode(CharSequence rawPassword) { return encode(rawPassword, saltGenerator.generateKey()); }
/** * Calculates the hash of password (and salt bytes, if supplied) and returns a base64 * encoded concatenation of the hash and salt, prefixed with {SHA} (or {SSHA} if salt * was used). * * @param rawPass the password to be encoded. * * @return the encoded password in the specified format * */ public String encode(CharSequence rawPass) { byte[] salt = this.saltGenerator.generateKey(); return encode(rawPass, salt); }
public String generateKey() { return new String(Hex.encode(keyGenerator.generateKey())); }
public String encode(CharSequence rawPassword) { return digest(rawPassword, saltGenerator.generateKey()); }
@Override public String encode(CharSequence rawPassword) { byte[] salt = this.saltGenerator.generateKey(); byte[] encoded = encodeAndConcatenate(rawPassword, salt); return String.valueOf(Hex.encode(encoded)); }
/** * Create a {@link BytesKeyGenerator} that returns a single, shared * {@link SecureRandom} key of a custom length. * @param keyLength the key length in bytes, e.g. 16, for a 16 byte key. */ public static BytesKeyGenerator shared(int keyLength) { return new SharedKeyGenerator(secureRandom(keyLength).generateKey()); }
@Override public String encode(CharSequence rawPassword) { byte[] salt = this.saltGenerator.generateKey(); byte[] encoded = encode(rawPassword, salt); return encode(encoded); }
private byte[] iv(byte[] encrypted) { return this.ivGenerator != NULL_IV_GENERATOR ? subArray(encrypted, 0, this.ivGenerator.getKeyLength()) : NULL_IV_GENERATOR.generateKey(); }
@Override public String encode(CharSequence rawPassword) { byte[] salt = this.saltGenerator.generateKey(); byte[] encoded = encode(rawPassword, salt); return encode(encoded); }
@Test public void secureRandomCustomLength() { BytesKeyGenerator keyGenerator = KeyGenerators.secureRandom(21); assertThat(keyGenerator.getKeyLength()).isEqualTo(21); byte[] key = keyGenerator.generateKey(); assertThat(key).hasSize(21); byte[] key2 = keyGenerator.generateKey(); assertThat(Arrays.equals(key, key2)).isFalse(); }
public byte[] encrypt(byte[] bytes) { synchronized (this.encryptor) { byte[] iv = this.ivGenerator.generateKey(); initCipher(this.encryptor, Cipher.ENCRYPT_MODE, this.secretKey, this.alg.getParameterSpec(iv)); byte[] encrypted = doFinal(this.encryptor, bytes); return this.ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted; } }
@Test public void secureRandom() { BytesKeyGenerator keyGenerator = KeyGenerators.secureRandom(); assertThat(keyGenerator.getKeyLength()).isEqualTo(8); byte[] key = keyGenerator.generateKey(); assertThat(key).hasSize(8); byte[] key2 = keyGenerator.generateKey(); assertThat(Arrays.equals(key, key2)).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(); }
@Before public void setUp() { this.generator = mock(BytesKeyGenerator.class); when(this.generator.generateKey()).thenReturn(Hex.decode("4b0febebd439db7ca77153cb254520c3")); when(this.generator.getKeyLength()).thenReturn(16); }
public byte[] encrypt(byte[] bytes) { synchronized (this.encryptor) { byte[] iv = this.ivGenerator.generateKey(); initCipher(this.encryptor, Cipher.ENCRYPT_MODE, this.secretKey, this.alg.getParameterSpec(iv)); byte[] encrypted = doFinal(this.encryptor, bytes); return this.ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted; } }
@Override public byte[] encrypt(byte[] bytes) { byte[] iv = this.ivGenerator.generateKey(); @SuppressWarnings("deprecation") GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()); blockCipher.init(true, new AEADParameters(secretKey, 128, iv, null)); byte[] encrypted = process(blockCipher, bytes); return iv != null ? concatenate(iv, encrypted) : encrypted; }
@Override public byte[] encrypt(byte[] bytes) { byte[] iv = this.ivGenerator.generateKey(); @SuppressWarnings("deprecation") GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()); blockCipher.init(true, new AEADParameters(secretKey, 128, iv, null)); byte[] encrypted = process(blockCipher, bytes); return iv != null ? concatenate(iv, encrypted) : encrypted; }
@Override public byte[] encrypt(byte[] bytes) { byte[] iv = this.ivGenerator.generateKey(); @SuppressWarnings("deprecation") PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding()); blockCipher.init(true, new ParametersWithIV(secretKey, iv)); byte[] encrypted = process(blockCipher, bytes); return iv != null ? concatenate(iv, encrypted) : encrypted; }
@Override public byte[] encrypt(byte[] bytes) { byte[] iv = this.ivGenerator.generateKey(); @SuppressWarnings("deprecation") PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding()); blockCipher.init(true, new ParametersWithIV(secretKey, iv)); byte[] encrypted = process(blockCipher, bytes); return iv != null ? concatenate(iv, encrypted) : encrypted; }