diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-03-14 09:56:59 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-03-14 09:56:59 +0100 |
commit | cd3bdef9d3c907cc76feac2164b96d47214c1775 (patch) | |
tree | b2b83a883472ee61591f4b27a10a9f60d91c7825 /sonar-plugin-api | |
parent | 7e83fc767544821bd204d3b70f602e88cf61d25a (diff) | |
download | sonarqube-cd3bdef9d3c907cc76feac2164b96d47214c1775.tar.gz sonarqube-cd3bdef9d3c907cc76feac2164b96d47214c1775.zip |
Fix some quality flaws
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/config/AesCipher.java | 13 | ||||
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java | 6 |
2 files changed, 10 insertions, 9 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/AesCipher.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/AesCipher.java index 52a64c626d3..5392f3651f4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/AesCipher.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/AesCipher.java @@ -44,6 +44,9 @@ final class AesCipher extends Cipher { // https://confluence.terena.org/display/~visser/No+256+bit+ciphers+for+Java+apps // http://java.sun.com/javase/6/webnotes/install/jre/README public static final int KEY_SIZE_IN_BITS = 128; + + private static final String CRYPTO_KEY = "AES"; + private final Settings settings; AesCipher(Settings settings) { @@ -52,7 +55,7 @@ final class AesCipher extends Cipher { String encrypt(String clearText) { try { - javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES"); + javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(CRYPTO_KEY); cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, loadSecretFile()); return new String(Base64.encodeBase64(cipher.doFinal(clearText.getBytes(Charsets.UTF_8)))); } catch (Exception e) { @@ -62,7 +65,7 @@ final class AesCipher extends Cipher { String decrypt(String encryptedText) { try { - javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES"); + 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); @@ -101,18 +104,18 @@ final class AesCipher extends Cipher { if (StringUtils.isBlank(s)) { throw new IllegalStateException("No secret key in the file: " + path); } - return new SecretKeySpec(Base64.decodeBase64(StringUtils.trim(s)), "AES"); + return new SecretKeySpec(Base64.decodeBase64(StringUtils.trim(s)), CRYPTO_KEY); } String generateRandomSecretKey() { try { - KeyGenerator keyGen = KeyGenerator.getInstance("AES"); + KeyGenerator keyGen = KeyGenerator.getInstance(CRYPTO_KEY); keyGen.init(KEY_SIZE_IN_BITS, new SecureRandom()); SecretKey secretKey = keyGen.generateKey(); return new String(Base64.encodeBase64(secretKey.getEncoded())); } catch (Exception e) { - throw new IllegalStateException("Fail to generate AES secret key", e); + throw new IllegalStateException("Fail to generate secret key", e); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java index 3bc88f0cc20..3857fc276ed 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java @@ -32,7 +32,6 @@ import java.util.regex.Pattern; public final class Encryption { private static final String BASE64_ALGORITHM = "b64"; - private final Base64Cipher base64Cipher; private static final String AES_ALGORITHM = "aes"; private final AesCipher aesCipher; @@ -41,10 +40,9 @@ public final class Encryption { private static final Pattern ENCRYPTED_PATTERN = Pattern.compile("\\{(.*?)\\}(.*)"); Encryption(Settings settings) { - base64Cipher = new Base64Cipher(); aesCipher = new AesCipher(settings); ciphers = ImmutableMap.of( - BASE64_ALGORITHM, base64Cipher, + BASE64_ALGORITHM, new Base64Cipher(), AES_ALGORITHM, aesCipher ); } @@ -57,7 +55,7 @@ public final class Encryption { } public boolean isEncrypted(String value) { - return value.startsWith("{") && value.indexOf("}") > 1; + return value.indexOf('{')==0 && value.indexOf('}') > 1; } public String encrypt(String clearText) { |