/xwiki-platform-core/xwiki-platform-crypto/src/main/java/org/xwiki/crypto/passwd/PasswordCryptoService.java

https://github.com/sorzu/xwiki-platform · Java · 105 lines · 18 code · 8 blank · 79 comment · 0 complexity · b9174f6a2aba610f01ef91ac3baa609c MD5 · raw file

  1. /*
  2. * See the NOTICE file distributed with this work for additional
  3. * information regarding copyright ownership.
  4. *
  5. * This is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU Lesser General Public License as
  7. * published by the Free Software Foundation; either version 2.1 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This software 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this software; if not, write to the Free
  17. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  19. */
  20. package org.xwiki.crypto.passwd;
  21. import java.security.GeneralSecurityException;
  22. import org.xwiki.component.annotation.ComponentRole;
  23. /**
  24. * Service allowing users to encrypt and decrypt text using a password.
  25. *
  26. * @version $Id: 6cfcd5881b8de9515c5b14fed253943687a57b68 $
  27. * @since 2.5M1
  28. */
  29. @ComponentRole
  30. public interface PasswordCryptoService
  31. {
  32. /**
  33. * Encipher the given text with the password. The same password will be able to decipher it.
  34. *
  35. * @param plaintext the text to encrypt.
  36. * @param password which will be needed to decrypt the text.
  37. * @return Base64 encoded ciphertext which can be decrypted back to plaintext only with the decryptText function.
  38. * @throws GeneralSecurityException if something goes wrong.
  39. */
  40. String encryptText(final String plaintext, final String password)
  41. throws GeneralSecurityException;
  42. /**
  43. * Decrypt a piece of text encrypted with encryptText.
  44. *
  45. * @param base64Ciphertext Base64 encoded ciphertext to decrypt.
  46. * @param password which was used to encrypt the text.
  47. * @return the decrypted text or null if the provided password was wrong.
  48. * @throws GeneralSecurityException if something goes wrong.
  49. */
  50. String decryptText(final String base64Ciphertext, final String password)
  51. throws GeneralSecurityException;
  52. /**
  53. * Encipher the given byte array with the password. The same password will be able to decipher it.
  54. *
  55. * @param message the message to encrypt.
  56. * @param password which will be needed to decrypt the text.
  57. * @return raw ciphertext which can be decrypted back to data using {@link #decryptBytes(byte[], String)}
  58. * @throws GeneralSecurityException if something goes wrong.
  59. */
  60. byte[] encryptBytes(final byte[] message, final String password)
  61. throws GeneralSecurityException;
  62. /**
  63. * Decrypt raw ciphertext created with {@link #encryptBytes(byte[], String)}.
  64. * Most of the time the response is null if the password is incorrect, 1 out of 250 times the output is
  65. * unintelligable garbage.
  66. *
  67. * @param rawCiphertext the ciphertext to decrypt.
  68. * @param password which was used to encrypt the text.
  69. * @return the decrypted message or null if the provided password was wrong.
  70. * @throws GeneralSecurityException if something goes wrong.
  71. */
  72. byte[] decryptBytes(final byte[] rawCiphertext, final String password)
  73. throws GeneralSecurityException;
  74. /**
  75. * Hash a password with a hash function specifically designed to make password guessing attacks difficult.
  76. * This hash does salting and multiple iterations which incure not only CPU but memory expense.
  77. *
  78. * @param password the plain text user supplied password.
  79. * @return a String of base-64 formatted bytes which can be used to verify the password later using
  80. * isPasswordCorrect. It is generally considered impossible to derive a password from this data however
  81. * for particularly easy to guess passwords, an attacker may guess the password using isPasswordCorrect
  82. * although the underlying function is designed to make that resource intensive.
  83. * @throws GeneralSecurityException on errors
  84. */
  85. String protectPassword(final String password) throws GeneralSecurityException;
  86. /**
  87. * Check the validity of a password.
  88. *
  89. * @param password the plain text user supplied password.
  90. * @param protectedPassword the result from calling protectPassword.
  91. * @return true if after running the user supplied password through the same underlying function, the output
  92. * matches the protectedPassword.
  93. * @throws GeneralSecurityException on errors
  94. */
  95. boolean isPasswordCorrect(final String password, final String protectedPassword)
  96. throws GeneralSecurityException;
  97. }