Package org.restlet.ext.crypto.internal
Class CryptoUtils
- java.lang.Object
-
- org.restlet.ext.crypto.internal.CryptoUtils
-
public final class CryptoUtils extends java.lang.ObjectSimple usage of standard cipher features from JRE.
-
-
Constructor Summary
Constructors Modifier Constructor Description privateCryptoUtils()Private constructor to ensure that the class acts as a true utility class i.e.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static javax.crypto.CiphercreateCipher(java.lang.String algorithm, byte[] secretKey, int mode)Creates a cipher for a given algorithm and secret.static java.lang.Stringdecrypt(java.lang.String algo, byte[] secretKey, byte[] encrypted)Decrypts a bytes array.static java.lang.Stringdecrypt(java.lang.String algo, java.lang.String base64Secret, byte[] encrypted)Decrypts a bytes array.private static byte[]doFinal(java.lang.String algo, byte[] secretKey, int mode, byte[] what)Does final processing.static byte[]encrypt(java.lang.String algo, byte[] secretKey, java.lang.String content)Encrypts a content string.static byte[]encrypt(java.lang.String algo, java.lang.String base64Secret, java.lang.String content)Encrypts a content string.static java.lang.StringmakeNonce(java.lang.String secretKey)Generates a nonce as recommended in section 3.2.1 of RFC-2617, but without the ETag field.
-
-
-
Method Detail
-
createCipher
private static javax.crypto.Cipher createCipher(java.lang.String algorithm, byte[] secretKey, int mode) throws java.security.GeneralSecurityExceptionCreates a cipher for a given algorithm and secret.- Parameters:
algorithm- The cryptographic algorithm.secretKey- The cryptographic secret.mode- The cipher mode, eitherCipher.ENCRYPT_MODEorCipher.DECRYPT_MODE.- Returns:
- The new cipher.
- Throws:
java.security.GeneralSecurityException
-
decrypt
public static java.lang.String decrypt(java.lang.String algo, byte[] secretKey, byte[] encrypted) throws java.security.GeneralSecurityExceptionDecrypts a bytes array.- Parameters:
algo- The cryptographic algorithm.secretKey- The cryptographic secret key.encrypted- The encrypted bytes.- Returns:
- The decrypted content string.
- Throws:
java.security.GeneralSecurityException
-
decrypt
public static java.lang.String decrypt(java.lang.String algo, java.lang.String base64Secret, byte[] encrypted) throws java.security.GeneralSecurityExceptionDecrypts a bytes array.- Parameters:
algo- The cryptographic algorithm.base64Secret- The cryptographic secret key, encoded as a Base64 string.encrypted- The encrypted bytes.- Returns:
- The decrypted content string.
- Throws:
java.security.GeneralSecurityException
-
doFinal
private static byte[] doFinal(java.lang.String algo, byte[] secretKey, int mode, byte[] what) throws java.security.GeneralSecurityExceptionDoes final processing.- Parameters:
algo- The cryptographic algorithm.secretKey- The cryptographic secret key.mode- The processing mode, eitherCipher.DECRYPT_MODEorCipher.ENCRYPT_MODE.what- The byte array to process.- Returns:
- The processed byte array.
- Throws:
java.security.GeneralSecurityException
-
encrypt
public static byte[] encrypt(java.lang.String algo, byte[] secretKey, java.lang.String content) throws java.security.GeneralSecurityExceptionEncrypts a content string.- Parameters:
algo- The cryptographic algorithm.secretKey- The cryptographic secret key.content- The content string to encrypt.- Returns:
- The encrypted bytes.
- Throws:
java.security.GeneralSecurityException
-
encrypt
public static byte[] encrypt(java.lang.String algo, java.lang.String base64Secret, java.lang.String content) throws java.security.GeneralSecurityExceptionEncrypts a content string.- Parameters:
algo- The cryptographic algorithm.base64Secret- The cryptographic secret, encoded as a Base64 string.content- The content string to encrypt.- Returns:
- The encrypted bytes.
- Throws:
java.security.GeneralSecurityException
-
makeNonce
public static java.lang.String makeNonce(java.lang.String secretKey)
Generates a nonce as recommended in section 3.2.1 of RFC-2617, but without the ETag field. The format is:Base64.encodeBytes(currentTimeMS + ":" + md5String(currentTimeMS + ":" + secretKey))- Parameters:
secretKey- a secret value known only to the creator of the nonce. It's inserted into the nonce, and can be used later to validate the nonce.
-
-