/framework/src/play-java/src/main/java/play/libs/Crypto.java

https://gitlab.com/KiaraGrouwstra/playframework · Java · 203 lines · 52 code · 17 blank · 134 comment · 2 complexity · 9bbd44491251f2956ece962261ad69ce MD5 · raw file

  1. /*
  2. * Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
  3. */
  4. package play.libs;
  5. import javax.inject.Inject;
  6. import javax.inject.Singleton;
  7. /**
  8. * Cryptographic utilities.
  9. * <br>
  10. * These utilities are intended as a convenience, however it is important to read each methods documentation and
  11. * understand the concepts behind encryption to use this class properly. Safe encryption is hard, and there is no
  12. * substitute for an adequate understanding of cryptography. These methods will not be suitable for all encryption
  13. * needs.
  14. *
  15. * For more information about cryptography, we recommend reading the OWASP Cryptographic Storage Cheatsheet:
  16. *
  17. * https://www.owasp.org/index.php/Cryptographic_Storage_Cheat_Sheet
  18. */
  19. @Singleton
  20. public class Crypto {
  21. private final play.api.libs.Crypto crypto;
  22. @Inject
  23. public Crypto(play.api.libs.Crypto crypto) {
  24. this.crypto = crypto;
  25. }
  26. /**
  27. * Signs the given String with HMAC-SHA1 using the given key.
  28. * <br>
  29. * By default this uses the platform default JSSE provider. This can be overridden by defining
  30. * <code>application.crypto.provider</code> in <code>application.conf</code>.
  31. *
  32. * @param message The message to sign.
  33. * @param key The private key to sign with.
  34. * @return A hexadecimal encoded signature.
  35. */
  36. public String sign(String message, byte[] key) {
  37. return crypto.sign(message, key);
  38. }
  39. /**
  40. * Signs the given String with HMAC-SHA1 using the application's secret key.
  41. * <br>
  42. * By default this uses the platform default JSSE provider. This can be overridden by defining
  43. * <code>application.crypto.provider</code> in <code>application.conf</code>.
  44. *
  45. * @param message The message to sign.
  46. * @return A hexadecimal encoded signature.
  47. */
  48. public String sign(String message) {
  49. return crypto.sign(message);
  50. }
  51. /**
  52. * Sign a token. This produces a new token, that has this token signed with a nonce.
  53. *
  54. * This primarily exists to defeat the BREACH vulnerability, as it allows the token to effectively be random per
  55. * request, without actually changing the value.
  56. *
  57. * @param token The token to sign
  58. * @return The signed token
  59. */
  60. public String signToken(String token) {
  61. return crypto.signToken(token);
  62. }
  63. /**
  64. * Extract a signed token that was signed by {@link #signToken(String)}.
  65. *
  66. * @param token The signed token to extract.
  67. * @return The verified raw token, or null if the token isn't valid.
  68. */
  69. public String extractSignedToken(String token) {
  70. scala.Option<String> extracted = crypto.extractSignedToken(token);
  71. if (extracted.isDefined()) {
  72. return extracted.get();
  73. } else {
  74. return null;
  75. }
  76. }
  77. /**
  78. * Generate a cryptographically secure token
  79. */
  80. public String generateToken() {
  81. return crypto.generateToken();
  82. }
  83. /**
  84. * Generate a signed token
  85. */
  86. public String generateSignedToken() {
  87. return crypto.generateSignedToken();
  88. }
  89. /**
  90. * Compare two signed tokens
  91. */
  92. public boolean compareSignedTokens(String tokenA, String tokenB) {
  93. return crypto.compareSignedTokens(tokenA, tokenB);
  94. }
  95. /**
  96. * Constant time equals method.
  97. *
  98. * Given a length that both Strings are equal to, this method will always run in constant time. This prevents
  99. * timing attacks.
  100. */
  101. public boolean constantTimeEquals(String a, String b) {
  102. return crypto.constantTimeEquals(a, b);
  103. }
  104. /**
  105. * Encrypt a String with the AES encryption standard using the application's secret key.
  106. * <br>
  107. * The provider used is by default this uses the platform default JSSE provider. This can be overridden by defining
  108. * <code>application.crypto.provider</code> in <code>application.conf</code>.
  109. * <br>
  110. * The transformation algorithm used is the provider specific implementation of the <code>AES</code> name. On
  111. * Oracles JDK, this is <code>AES/CTR/NoPadding</code>. This algorithm is suitable for small amounts of data,
  112. * typically less than 32 bytes, hence is useful for encrypting credit card numbers, passwords etc. For larger
  113. * blocks of data, this algorithm may expose patterns and be vulnerable to repeat attacks.
  114. * <br>
  115. * The transformation algorithm can be configured by defining <code>application.crypto.aes.transformation</code> in
  116. * <code>application.conf</code>. Although any cipher transformation algorithm can be selected here, the secret key
  117. * spec used is always AES, so only AES transformation algorithms will work.
  118. *
  119. * @param value The String to encrypt.
  120. * @return An hexadecimal encrypted string.
  121. */
  122. public String encryptAES(String value) {
  123. return crypto.encryptAES(value);
  124. }
  125. /**
  126. * Encrypt a String with the AES encryption standard and the supplied private key.
  127. * <br>
  128. * The private key must have a length of 16 bytes.
  129. * <br>
  130. * The provider used is by default this uses the platform default JSSE provider. This can be overridden by defining
  131. * <code>application.crypto.provider</code> in <code>application.conf</code>.
  132. * <br>
  133. * The transformation algorithm used is the provider specific implementation of the <code>AES</code> name. On
  134. * Oracles JDK, this is <code>AES/CTR/NoPadding</code>. This algorithm is suitable for small amounts of data,
  135. * typically less than 32bytes, hence is useful for encrypting credit card numbers, passwords etc. For larger
  136. * blocks of data, this algorithm may expose patterns and be vulnerable to repeat attacks.
  137. * <br>
  138. * The transformation algorithm can be configured by defining <code>application.crypto.aes.transformation</code> in
  139. * <code>application.conf</code>. Although any cipher transformation algorithm can be selected here, the secret key
  140. * spec used is always AES, so only AES transformation algorithms will work.
  141. *
  142. * @param value The String to encrypt.
  143. * @param privateKey The key used to encrypt.
  144. * @return An hexadecimal encrypted string.
  145. */
  146. public String encryptAES(String value, String privateKey) {
  147. return crypto.encryptAES(value, privateKey);
  148. }
  149. /**
  150. * Decrypt a String with the AES encryption standard using the application's secret key.
  151. * <br>
  152. * The provider used is by default this uses the platform default JSSE provider. This can be overridden by defining
  153. * <code>application.crypto.provider</code> in <code>application.conf</code>.
  154. * <br>
  155. * The transformation used is by default <code>AES/CTR/NoPadding</code>. It can be configured by defining
  156. * <code>application.crypto.aes.transformation</code> in <code>application.conf</code>. Although any cipher
  157. * transformation algorithm can be selected here, the secret key spec used is always AES, so only AES transformation
  158. * algorithms will work.
  159. *
  160. * @param value An hexadecimal encrypted string.
  161. * @return The decrypted String.
  162. */
  163. public String decryptAES(String value) {
  164. return crypto.decryptAES(value);
  165. }
  166. /**
  167. * Decrypt a String with the AES encryption standard.
  168. * <br>
  169. * The private key must have a length of 16 bytes.
  170. * <br>
  171. * The provider used is by default this uses the platform default JSSE provider. This can be overridden by defining
  172. * <code>application.crypto.provider</code> in <code>application.conf</code>.
  173. * <br>
  174. * The transformation used is by default <code>AES/CTR/NoPadding</code>. It can be configured by defining
  175. * <code>application.crypto.aes.transformation</code> in <code>application.conf</code>. Although any cipher
  176. * transformation algorithm can be selected here, the secret key spec used is always AES, so only AES transformation
  177. * algorithms will work.
  178. *
  179. * @param value An hexadecimal encrypted string.
  180. * @param privateKey The key used to encrypt.
  181. * @return The decrypted String.
  182. */
  183. public String decryptAES(String value, String privateKey) {
  184. return crypto.decryptAES(value, privateKey);
  185. }
  186. }