@Test public void loadSecretKeyFromFile_no_property() throws Exception { thrown.expect(IllegalStateException.class); AesCipher cipher = new AesCipher(null); cipher.loadSecretFileFromFile(null); }
public String generateRandomSecretKey() { return aesCipher.generateRandomSecretKey(); }
private Key loadSecretFile() throws IOException { String path = getPathToSecretKey(); return loadSecretFileFromFile(path); }
@Test public void doesNotHaveSecretKey() { AesCipher cipher = new AesCipher("/my/twitter/id/is/SimonBrandhof"); assertThat(cipher.hasSecretKey()).isFalse(); }
@Test public void testDefaultPathToSecretKey() { AesCipher cipher = new AesCipher(null); String path = cipher.getPathToSecretKey(); assertThat(StringUtils.isNotBlank(path)).isTrue(); assertThat(new File(path).getName()).isEqualTo("sonar-secret.txt"); }
@Test public void generateRandomSecretKey() { AesCipher cipher = new AesCipher(null); String key = cipher.generateRandomSecretKey(); assertThat(StringUtils.isNotBlank(key)).isTrue(); assertThat(Base64.isArrayByteBase64(key.getBytes())).isTrue(); }
@Test public void decrypt() throws Exception { AesCipher cipher = new AesCipher(pathToSecretKey()); // the following value has been encrypted with the key /org/sonar/api/config/AesCipherTest/aes_secret_key.txt String clearText = cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY="); assertThat(clearText).isEqualTo("this is a secret"); }
@Override public String encrypt(String clearText) { try { javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(CRYPTO_KEY); cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, loadSecretFile()); return Base64.encodeBase64String(cipher.doFinal(clearText.getBytes(StandardCharsets.UTF_8.name()))); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IllegalStateException(e); } }
/** * Checks the availability of the secret key, that is required to encrypt and decrypt. */ public boolean hasSecretKey() { return aesCipher.hasSecretKey(); }
/** * This method checks the existence of the file, but not the validity of the contained key. */ boolean hasSecretKey() { String path = getPathToSecretKey(); if (StringUtils.isNotBlank(path)) { File file = new File(path); return file.exists() && file.isFile(); } return false; }
public Encryption(@Nullable String pathToSecretKey) { aesCipher = new AesCipher(pathToSecretKey); ciphers = new HashMap<>(); ciphers.put(BASE64_ALGORITHM, new Base64Cipher()); ciphers.put(AES_ALGORITHM, aesCipher); }
public void setPathToSecretKey(@Nullable String pathToSecretKey) { aesCipher.setPathToSecretKey(pathToSecretKey); }
@Test public void hasSecretKey() throws Exception { AesCipher cipher = new AesCipher(pathToSecretKey()); assertThat(cipher.hasSecretKey()).isTrue(); }
@Test public void decrypt_bad_key() throws Exception { URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/bad_secret_key.txt"); AesCipher cipher = new AesCipher(new File(resource.toURI()).getCanonicalPath()); try { cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY="); fail(); } catch (RuntimeException e) { assertThat(e.getCause()).isInstanceOf(InvalidKeyException.class); } }
@Override public String decrypt(String encryptedText) { try { javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(CRYPTO_KEY); cipher.init(javax.crypto.Cipher.DECRYPT_MODE, loadSecretFile()); byte[] cipherData = cipher.doFinal(Base64.decodeBase64(StringUtils.trim(encryptedText))); return new String(cipherData, StandardCharsets.UTF_8); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IllegalStateException(e); } }
@Test public void encrypt_bad_key() throws Exception { thrown.expect(RuntimeException.class); thrown.expectMessage("Invalid AES key"); URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/bad_secret_key.txt"); AesCipher cipher = new AesCipher(new File(resource.toURI()).getCanonicalPath()); cipher.encrypt("this is a secret"); }
/** * Checks the availability of the secret key, that is required to encrypt and decrypt. */ public boolean hasSecretKey() { return aesCipher.hasSecretKey(); }
/** * This method checks the existence of the file, but not the validity of the contained key. */ boolean hasSecretKey() { String path = getPathToSecretKey(); if (StringUtils.isNotBlank(path)) { File file = new File(path); return file.exists() && file.isFile(); } return false; }