private byte[] encode(CharSequence rawPassword, byte[] salt) { try { PBEKeySpec spec = new PBEKeySpec(rawPassword.toString().toCharArray(), concatenate(salt, this.secret), this.iterations, this.hashWidth); SecretKeyFactory skf = SecretKeyFactory.getInstance(this.algorithm); return concatenate(salt, skf.generateSecret(spec).getEncoded()); } catch (GeneralSecurityException e) { throw new IllegalStateException("Could not create hash", e); } }
private byte[] encrypted(byte[] encryptedBytes, int ivLength) { return subArray(encryptedBytes, ivLength, encryptedBytes.length); }
private byte[] encode(CharSequence rawPassword, byte[] salt) { try { PBEKeySpec spec = new PBEKeySpec(rawPassword.toString().toCharArray(), concatenate(salt, this.secret), this.iterations, this.hashWidth); SecretKeyFactory skf = SecretKeyFactory.getInstance(this.algorithm); return concatenate(salt, skf.generateSecret(spec).getEncoded()); } catch (GeneralSecurityException e) { throw new IllegalStateException("Could not create hash", e); } }
private byte[] encrypted(byte[] encryptedBytes, int ivLength) { return subArray(encryptedBytes, ivLength, encryptedBytes.length); }
private byte[] digest(CharSequence rawPassword, byte[] salt) { byte[] digest = digester.digest(concatenate(salt, secret, Utf8.encode(rawPassword))); return concatenate(salt, digest); }
private byte[] iv(byte[] encrypted) { return this.ivGenerator != NULL_IV_GENERATOR ? subArray(encrypted, 0, this.ivGenerator.getKeyLength()) : NULL_IV_GENERATOR.generateKey(); }
protected byte[] encodeAndConcatenate(CharSequence rawPassword, byte[] salt) { return concatenate(salt, encode(rawPassword, salt)); }
private byte[] iv(byte[] encrypted) { return this.ivGenerator != NULL_IV_GENERATOR ? subArray(encrypted, 0, this.ivGenerator.getKeyLength()) : NULL_IV_GENERATOR.generateKey(); }
private byte[] digest(CharSequence rawPassword, byte[] salt) { byte[] digest = digester.digest(concatenate(salt, secret, Utf8.encode(rawPassword))); return concatenate(salt, digest); }
@Test public void subArray() { byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 }; byte[] two = new byte[] { (byte) 0xFF, (byte) 65, (byte) 66 }; byte[] subArray = EncodingUtils.subArray(bytes, 1, 4); assertThat(subArray).hasSize(3); assertThat(Arrays.equals(two, subArray)).isTrue(); }
private byte[] digest(CharSequence rawPassword) { byte[] digest = digest(concatenate(salt, secret, Utf8.encode(rawPassword))); return concatenate(salt, digest); }
@Override public boolean matches(CharSequence rawPassword, String encodedPassword) { byte[] digested = decode(encodedPassword); byte[] salt = subArray(digested, 0, this.saltGenerator.getKeyLength()); return matches(digested, encode(rawPassword, salt)); }
protected byte[] encodeAndConcatenate(CharSequence rawPassword, byte[] salt) { return concatenate(salt, encode(rawPassword, salt)); }
public boolean matches(CharSequence rawPassword, String encodedPassword) { byte[] digested = decode(encodedPassword); byte[] salt = subArray(digested, 0, saltGenerator.getKeyLength()); return matches(digested, digest(rawPassword, salt)); }
@Test public void concatenate() { byte[] bytes = new byte[] { (byte) 0x01, (byte) 0xFF, (byte) 65, (byte) 66, (byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 }; byte[] one = new byte[] { (byte) 0x01 }; byte[] two = new byte[] { (byte) 0xFF, (byte) 65, (byte) 66 }; byte[] three = new byte[] { (byte) 67, (byte) 0xC0, (byte) 0xC1, (byte) 0xC2 }; assertThat(Arrays.equals(bytes, EncodingUtils.concatenate(one, two, three))).isTrue(); }
@Override public boolean matches(CharSequence rawPassword, String encodedPassword) { byte[] digested = Hex.decode(encodedPassword); byte[] salt = subArray(digested, 0, this.saltGenerator.getKeyLength()); return matches(digested, encodeAndConcatenate(rawPassword, salt)); }
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[] decrypt(byte[] encryptedBytes) { byte[] iv = subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength()); encryptedBytes = subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length); @SuppressWarnings("deprecation") GCMBlockCipher blockCipher = new GCMBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()); blockCipher.init(false, new AEADParameters(secretKey, 128, iv, null)); return process(blockCipher, encryptedBytes); }
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; } }
public boolean matches(CharSequence rawPassword, String encodedPassword) { byte[] digested = decode(encodedPassword); byte[] salt = subArray(digested, 0, saltGenerator.getKeyLength()); return matches(digested, digest(rawPassword, salt)); }