public Encryption(@Nullable String pathToSecretKey) { aesCipher = new AesCipher(pathToSecretKey); ciphers = new HashMap<>(); ciphers.put(BASE64_ALGORITHM, new Base64Cipher()); ciphers.put(AES_ALGORITHM, aesCipher); }
@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 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); } }
@Test public void decrypt_other_key() throws Exception { URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/other_secret_key.txt"); AesCipher cipher = new AesCipher(new File(resource.toURI()).getCanonicalPath()); try { // text encrypted with another key cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY="); fail(); } catch (RuntimeException e) { assertThat(e.getCause()).isInstanceOf(BadPaddingException.class); } }
@Test public void doesNotHaveSecretKey() { AesCipher cipher = new AesCipher("/my/twitter/id/is/SimonBrandhof"); assertThat(cipher.hasSecretKey()).isFalse(); }
@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 loadSecretKeyFromFile_trim_content() throws Exception { URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/non_trimmed_secret_key.txt"); String path = new File(resource.toURI()).getCanonicalPath(); AesCipher cipher = new AesCipher(null); Key secretKey = cipher.loadSecretFileFromFile(path); assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); assertThat(secretKey.getEncoded().length).isGreaterThan(10); }
@Test public void encrypt() throws Exception { AesCipher cipher = new AesCipher(pathToSecretKey()); String encryptedText = cipher.encrypt("this is a secret"); assertThat(StringUtils.isNotBlank(encryptedText)).isTrue(); assertThat(Base64.isArrayByteBase64(encryptedText.getBytes())).isTrue(); }
@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"); }
@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"); }
@Test public void hasSecretKey() throws Exception { AesCipher cipher = new AesCipher(pathToSecretKey()); assertThat(cipher.hasSecretKey()).isTrue(); }
@Test public void loadSecretKeyFromFile() throws Exception { AesCipher cipher = new AesCipher(null); Key secretKey = cipher.loadSecretFileFromFile(pathToSecretKey()); assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); assertThat(secretKey.getEncoded().length).isGreaterThan(10); }
@Test public void encryptThenDecrypt() throws Exception { AesCipher cipher = new AesCipher(pathToSecretKey()); assertThat(cipher.decrypt(cipher.encrypt("foo"))).isEqualTo("foo"); }
@Test public void loadSecretKeyFromFile_no_property() throws Exception { thrown.expect(IllegalStateException.class); AesCipher cipher = new AesCipher(null); cipher.loadSecretFileFromFile(null); }
@Test public void loadSecretKeyFromFile_file_does_not_exist() throws Exception { thrown.expect(IllegalStateException.class); AesCipher cipher = new AesCipher(null); cipher.loadSecretFileFromFile("/file/does/not/exist"); }
public Encryption(@Nullable String pathToSecretKey) { aesCipher = new AesCipher(pathToSecretKey); ciphers = ImmutableMap.of( BASE64_ALGORITHM, new Base64Cipher(), AES_ALGORITHM, aesCipher); }
public Encryption(@Nullable String pathToSecretKey) { aesCipher = new AesCipher(pathToSecretKey); ciphers = new HashMap<>(); ciphers.put(BASE64_ALGORITHM, new Base64Cipher()); ciphers.put(AES_ALGORITHM, aesCipher); }