PageRenderTime 67ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/core/util/src/main/java/org/rhq/core/util/obfuscation/Obfuscator.java

https://github.com/metlos/RHQ-old
Java | 121 lines | 49 code | 14 blank | 58 comment | 6 complexity | c9583d96fad5014437ff2173055cc61d MD5 | raw file
  1. /*
  2. * RHQ Management Platform
  3. * Copyright (C) 2005-2012 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. package org.rhq.core.util.obfuscation;
  20. import java.math.BigInteger;
  21. import java.security.InvalidKeyException;
  22. import java.security.NoSuchAlgorithmException;
  23. import javax.crypto.BadPaddingException;
  24. import javax.crypto.Cipher;
  25. import javax.crypto.IllegalBlockSizeException;
  26. import javax.crypto.NoSuchPaddingException;
  27. import javax.crypto.spec.SecretKeySpec;
  28. /**
  29. * This class makes available methods for obfuscating a string in the very same way
  30. * as the <code>org.jboss.resource.security.SecureIdentityLoginModule</code> in JBossAS 4.2.3.
  31. * <p>
  32. * This is to ensure backwards compatibility in case we switch containers that would start
  33. * obfuscating the password in a different way and also to make those methods available to
  34. * other code. The original methods in the SecureIdentityLoginModule are marked private.
  35. *
  36. * @author Lukas Krejci
  37. */
  38. public final class Obfuscator {
  39. private static final byte[] KEY = "jaas is the way".getBytes();
  40. public static final String ALGORITHM = "Blowfish";
  41. //no instances, please
  42. private Obfuscator() {
  43. }
  44. /**
  45. * Encodes the secret string so that the value is not immediately readable by
  46. * a "casual viewer".
  47. *
  48. * @param secret the string to encode
  49. * @return encoded string
  50. * @throws NoSuchPaddingException
  51. * @throws NoSuchAlgorithmException
  52. * @throws InvalidKeyException
  53. * @throws BadPaddingException
  54. * @throws IllegalBlockSizeException
  55. */
  56. public static String encode(String secret) throws NoSuchPaddingException, NoSuchAlgorithmException,
  57. InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
  58. SecretKeySpec key = new SecretKeySpec(KEY, ALGORITHM);
  59. Cipher cipher = Cipher.getInstance(ALGORITHM);
  60. cipher.init(Cipher.ENCRYPT_MODE, key);
  61. byte[] encoding = cipher.doFinal(secret.getBytes());
  62. BigInteger n = new BigInteger(encoding);
  63. return n.toString(16);
  64. }
  65. /**
  66. * Decodes the string obfuscated using the {@link #encode(String)} method back to the
  67. * original value.
  68. * <p>
  69. * This method differs from its original <code>org.jboss.resource.security.SecureIdentityLoginModule#decode</code>
  70. * private method in that it returns a String whereas the original method returns a char[].
  71. *
  72. * @param secret the encoded (obfuscated) string
  73. * @return the decoded string
  74. * @throws NoSuchPaddingException
  75. * @throws NoSuchAlgorithmException
  76. * @throws InvalidKeyException
  77. * @throws BadPaddingException
  78. * @throws IllegalBlockSizeException
  79. */
  80. public static String decode(String secret) throws NoSuchPaddingException, NoSuchAlgorithmException,
  81. InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
  82. SecretKeySpec key = new SecretKeySpec(KEY, ALGORITHM);
  83. BigInteger n = new BigInteger(secret, 16);
  84. byte[] encoding = n.toByteArray();
  85. //SECURITY-344: fix leading zeros
  86. if (encoding.length % 8 != 0) {
  87. int length = encoding.length;
  88. int newLength = ((length / 8) + 1) * 8;
  89. int pad = newLength - length; //number of leading zeros
  90. byte[] old = encoding;
  91. encoding = new byte[newLength];
  92. for (int i = old.length - 1; i >= 0; i--) {
  93. encoding[i + pad] = old[i];
  94. }
  95. //SECURITY-563: handle negative numbers
  96. if (n.signum() == -1) {
  97. for (int i = 0; i < newLength - length; i++) {
  98. encoding[i] = (byte) -1;
  99. }
  100. }
  101. }
  102. Cipher cipher = Cipher.getInstance(ALGORITHM);
  103. cipher.init(Cipher.DECRYPT_MODE, key);
  104. byte[] decode = cipher.doFinal(encoding);
  105. return new String(decode);
  106. }
  107. }