PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/plugins/bitbucket/api/impl/DefaultEncryptor.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 116 lines | 78 code | 20 blank | 18 comment | 3 complexity | 6c956b196baeb33ceee31c331bf27e04 MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket.api.impl;
  2. import com.atlassian.jira.plugins.bitbucket.api.Encryptor;
  3. import com.atlassian.jira.project.ProjectManager;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.math.BigInteger;
  9. import java.security.MessageDigest;
  10. import java.util.Arrays;
  11. /**
  12. * An encryption service for storing passwords
  13. */
  14. public class DefaultEncryptor implements Encryptor
  15. {
  16. final Logger logger = LoggerFactory.getLogger(DefaultEncryptor.class);
  17. private final ProjectManager projectManager;
  18. public DefaultEncryptor(ProjectManager projectManager)
  19. {
  20. this.projectManager = projectManager;
  21. }
  22. /**
  23. * Encrypt the input into a hex encoded string;
  24. * @param input the input to encrypt
  25. * @param projectKey the project key
  26. * @param repoURL the repository url
  27. * @return the encrypted string
  28. */
  29. public String encrypt(String input, String projectKey, String repoURL)
  30. {
  31. byte[] encrypted;
  32. try
  33. {
  34. // TODO - Do we need projectID? Can we use projectKey instead?
  35. String projectID = projectManager.getProjectObjByKey(projectKey).getId().toString();
  36. byte[] key = (projectID + repoURL).getBytes("UTF-8");
  37. MessageDigest sha = MessageDigest.getInstance("SHA-1");
  38. key = sha.digest(key);
  39. key = Arrays.copyOf(key, 16); // use only first 128 bit
  40. // Generate the secret key specs.
  41. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  42. // Instantiate the cipher
  43. Cipher cipher = Cipher.getInstance("AES");
  44. cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
  45. encrypted = cipher.doFinal((input).getBytes());
  46. }
  47. catch (Exception e)
  48. {
  49. logger.debug("error encrypting",e);
  50. encrypted = new byte[0];
  51. }
  52. BigInteger bi = new BigInteger(1, encrypted);
  53. return String.format("%0" + (encrypted.length << 1) + "X", bi);
  54. }
  55. private static byte[] hexStringToByteArray(String s)
  56. {
  57. int len = s.length();
  58. byte[] data = new byte[len / 2];
  59. for (int i = 0; i < len; i += 2)
  60. {
  61. data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
  62. + Character.digit(s.charAt(i + 1), 16));
  63. }
  64. return data;
  65. }
  66. public String decrypt(String password, String projectKey, String repoURL)
  67. {
  68. if (password == null)
  69. return null;
  70. try
  71. {
  72. byte[] ciphertext = DefaultEncryptor.hexStringToByteArray(password);
  73. String projectID = projectManager.getProjectObjByKey(projectKey).getId().toString();
  74. // Get the Key
  75. byte[] key = (projectID + repoURL).getBytes("UTF-8");
  76. MessageDigest sha = MessageDigest.getInstance("SHA-1");
  77. key = sha.digest(key);
  78. key = Arrays.copyOf(key, 16); // use only first 128 bit
  79. // Generate the secret key specs.
  80. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  81. // Instantiate the cipher
  82. Cipher cipher = Cipher.getInstance("AES");
  83. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
  84. byte[] original = cipher.doFinal(ciphertext);
  85. String originalString = new String(original);
  86. //logger.debug("Original string: " + originalString + "\nOriginal string (Hex): " + original.toString());
  87. // logger.debug("decrypted password [ " + original+" ]");
  88. return originalString;
  89. }
  90. catch (Exception e)
  91. {
  92. logger.debug("error encrypting",e);
  93. }
  94. return "";
  95. }
  96. }