You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Encryption.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.config.internal;
  21. import java.util.HashMap;
  22. import java.util.Locale;
  23. import java.util.Map;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;
  26. import javax.annotation.Nullable;
  27. /**
  28. * @since 3.0
  29. */
  30. public final class Encryption {
  31. private static final String BASE64_ALGORITHM = "b64";
  32. private static final String AES_ALGORITHM = "aes";
  33. private final AesCipher aesCipher;
  34. private final Map<String, Cipher> ciphers;
  35. private static final Pattern ENCRYPTED_PATTERN = Pattern.compile("\\{(.*?)\\}(.*)");
  36. public Encryption(@Nullable String pathToSecretKey) {
  37. aesCipher = new AesCipher(pathToSecretKey);
  38. ciphers = new HashMap<>();
  39. ciphers.put(BASE64_ALGORITHM, new Base64Cipher());
  40. ciphers.put(AES_ALGORITHM, aesCipher);
  41. }
  42. public void setPathToSecretKey(@Nullable String pathToSecretKey) {
  43. aesCipher.setPathToSecretKey(pathToSecretKey);
  44. }
  45. /**
  46. * Checks the availability of the secret key, that is required to encrypt and decrypt.
  47. */
  48. public boolean hasSecretKey() {
  49. return aesCipher.hasSecretKey();
  50. }
  51. public boolean isEncrypted(String value) {
  52. return value.indexOf('{') == 0 && value.indexOf('}') > 1;
  53. }
  54. public String encrypt(String clearText) {
  55. return encrypt(AES_ALGORITHM, clearText);
  56. }
  57. public String scramble(String clearText) {
  58. return encrypt(BASE64_ALGORITHM, clearText);
  59. }
  60. public String generateRandomSecretKey() {
  61. return aesCipher.generateRandomSecretKey();
  62. }
  63. public String decrypt(String encryptedText) {
  64. Matcher matcher = ENCRYPTED_PATTERN.matcher(encryptedText);
  65. if (matcher.matches()) {
  66. Cipher cipher = ciphers.get(matcher.group(1).toLowerCase(Locale.ENGLISH));
  67. if (cipher != null) {
  68. return cipher.decrypt(matcher.group(2));
  69. }
  70. }
  71. return encryptedText;
  72. }
  73. private String encrypt(String algorithm, String clearText) {
  74. Cipher cipher = ciphers.get(algorithm);
  75. if (cipher == null) {
  76. throw new IllegalArgumentException("Unknown cipher algorithm: " + algorithm);
  77. }
  78. return String.format("{%s}%s", algorithm, cipher.encrypt(clearText));
  79. }
  80. }