Interface CryptoSupport
CryptoSupport provides a simple API to encrypt and decrypt
binary and string data.
This interface is not intended to be implemented by consumers. To use the API get the service from the service registry under the name "com.adobe.granite.crypto.CryptoSupport".
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptioncreateKeyPair(String algorithm) Generates a key pair.byte[]decrypt(byte[] cipherText) Decrypts the givencipherTextdata into plain text.byte[]decrypt(byte[] key, byte[] cipherText) Decrypts the givencipherTextdata into plain text.byte[]encrypt(byte[] plainText) Encrypts the givenplainTextdata into a cipher text.byte[]encrypt(byte[] key, byte[] plainText) Encrypts the givenplainTextdata into a cipher text.byte[]hmac_sha256(byte[] text) Generate HMAC bytes given some text.byte[]hmac_sha256(byte[] key, byte[] text) Generate HMAC bytes given a key and some text.booleanisProtected(String text) Returnstrueif the given string is to be considered protected by theprotect(String)method and can be converted to plain text by calling theunprotect(String)method.voidnextRandomBytes(byte[] bytes) Fill the byte buffer with securely-generated pseudo-random bytes.Encrypts the givenplainTextdata into a cipher text.Encrypts the givenplainTextdata into a cipher text.byte[]sign(byte[] text, PrivateKey privateKey, String algorithm) Sign some data using the given private keysign(Certificate issuerCertificate, KeyPair keyPair, X500Principal subject, long before, long after) Sign aCertificateeither using a provided issuer certificate or using theCertificatesubject as issuer (self-signed).Unprotect the given string such that the resulting plain text string if given to theprotect(byte[], String)returns the protected string given to this method.Unprotect the given string such that the resulting plain text string if given to theprotect(String)returns the protected string given to this method.byte[]unwrapKey(byte[] wrappedKeyData) Unwraps the givenwrappedKeyusing a symmetric key wrap algorithm.byte[]unwrapKey(byte[] kek, byte[] wrappedKeyData) Unwraps the givenwrappedKeyusing a symmetric key wrap algorithm.booleanPerform a signature verification with the given public key.byte[]wrapKey(byte[] keyData) Wraps the givenkeyDatausing a symmetric key wrap algorithm.byte[]wrapKey(byte[] kek, byte[] keyData) Wraps the givenkeyDatausing a symmetric key wrap algorithm.
-
Field Details
-
NAME
Name of the Encryption/Decryption service which may be used securely store sensitive data.- See Also:
-
-
Method Details
-
encrypt
Encrypts the givenplainTextdata into a cipher text.Note that this method and the
decrypt(byte[])method provide full round trip support:decrypt(encrypt(plainText)).equals(plainText) == true
Please note, that calling this method twice on the same
plainTextdoes not return the same cipher text:encrypt(plainText).equals(encrypt(plainText)) == false
- Parameters:
plainText- The plain text data to encrypt- Returns:
- The encrypted data
- Throws:
CryptoException- If any problem occurs encrypting the plain text data. TheThrowable.getCause()method may provide additional information on the encryption failure.
-
decrypt
Decrypts the givencipherTextdata into plain text.Note that this method and the
encrypt(byte[])method provide full round trip support:decrypt(encrypt(plainText)).equals(plainText) == true
- Parameters:
cipherText- The encrypted data to decrypt- Returns:
- The plain text data
- Throws:
CryptoException- If any problem occurs decrypting the cipher text. TheThrowable.getCause()method may provide additional information on the decryption failure.
-
encrypt
Encrypts the givenplainTextdata into a cipher text.Note that this method and the
decrypt(byte[], byte[])method provide full round trip support:decrypt(encrypt(key, plainText)).equals(key, plainText) == true
Please note that the implementation will not clear the byte[] key.
Please note, that calling this method twice on the same
plainTextdoes not return the same cipher text:encrypt(key, plainText).equals(encrypt(key, plainText)) == false
- Parameters:
key- The key material used by the encryption algorithm. This must be a non-null, non-empty array of bytes. The current implementations use AES for this operation and therefore require a key length of 16, 24, or 32 bytes (128, 192, or 256 bits).plainText- The plain text data to encrypt- Returns:
- The encrypted data
- Throws:
CryptoException- If any problem occurs encrypting the plain text data. TheThrowable.getCause()method may provide additional information on the encryption failure.- Since:
- 1.2
-
decrypt
Decrypts the givencipherTextdata into plain text.Please note that the implementation will not clear the byte[] key.
Note that this method and the
encrypt(byte[], byte[])method provide full round trip support:decrypt(encrypt(key, plainText)).equals(key, plainText) == true
- Parameters:
key- The key material used by the encryption algorithm. This must be a non-null, non-empty array of bytes. The current implementations use AES for this operation and therefore require a key length of 16, 24, or 32 bytes (128, 192, or 256 bits).cipherText- The encrypted data to decrypt- Returns:
- The plain text data
- Throws:
CryptoException- If any problem occurs decrypting the cipher text. TheThrowable.getCause()method may provide additional information on the decryption failure.- Since:
- 1.2
-
isProtected
Returnstrueif the given string is to be considered protected by theprotect(String)method and can be converted to plain text by calling theunprotect(String)method.- Parameters:
text- the string to test for protection- Returns:
trueif the given string is to be considered protected by theprotect(String)method and can be converted to plain text by calling theunprotect(String)method
-
protect
Encrypts the givenplainTextdata into a cipher text.This method is like
encrypt(byte[])but for character data.Note that this method and the
unprotect(String)method provide full round trip support:unprotect(protect(plainText)).equals(plainText) == true
Please note, that calling this method twice on the same
plainTextdoes not return the same cipher text:protect(plainText).equals(protect(plainText)) == false
- Parameters:
plainText- The plain text data to encrypt- Returns:
- The encrypted data
- Throws:
CryptoException- If any problem occurs encrypting the plain text data. TheThrowable.getCause()method may provide additional information on the encryption failure.
-
unprotect
Unprotect the given string such that the resulting plain text string if given to theprotect(String)returns the protected string given to this method.Note that this method and the
protect(String)method provide full round trip support:unprotect(protect(plainText)).equals(plainText) == true
- Parameters:
cipherText- The encrypted data to decrypt- Returns:
- The plain text data
- Throws:
CryptoException- If any problem occurs decrypting the cipher text. TheThrowable.getCause()method may provide additional information on the decryption failure. Particularly this exception may be thrown if thecipherTexthas obviously not been protected by theprotect(String)method andisProtected(String)would returnfalse.
-
protect
Encrypts the givenplainTextdata into a cipher text.This method is like
encrypt(byte[], byte[])but for character data.Please note that the implementation will not clear the byte[] key.
Note that this method and the
unprotect(byte[], String)method provide full round trip support:unprotect(protect(key, plainText)).equals(key, plainText) == true
Please note, that calling this method twice on the same
plainTextdoes not return the same cipher text:protect(key, plainText).equals(protect(key, plainText)) == false
- Parameters:
key- The key material used by the encryption algorithm. This must be a non-null, non-empty array of bytes. The current implementations use AES for this operation and therefore require a key length of 16, 24, or 32 bytes (128, 192, or 256 bits).plainText- The plain text data to encrypt- Returns:
- The encrypted data
- Throws:
CryptoException- If any problem occurs encrypting the plain text data. TheThrowable.getCause()method may provide additional information on the encryption failure.- Since:
- 1.2
-
unprotect
Unprotect the given string such that the resulting plain text string if given to theprotect(byte[], String)returns the protected string given to this method.Please note that the implementation will not clear the byte[] key.
Note that this method and the
protect(byte[], String)method provide full round trip support:unprotect(protect(key, plainText)).equals(key, plainText) == true
- Parameters:
key- The key material used by the encryption algorithm. This must be a non-null, non-empty array of bytes. The current implementations use AES for this operation and therefore require a key length of 16, 24, or 32 bytes (128, 192, or 256 bits).cipherText- The encrypted data to decrypt- Returns:
- The plain text data
- Throws:
CryptoException- If any problem occurs decrypting the cipher text. TheThrowable.getCause()method may provide additional information on the decryption failure. Particularly this exception may be thrown if thecipherTexthas obviously not been protected by theprotect(String)method andisProtected(String)would returnfalse.- Since:
- 1.2
-
wrapKey
Wraps the givenkeyDatausing a symmetric key wrap algorithm.Note that this method and the
unwrapKey(byte[], byte[])method provide full round trip support:unwrapKey(wrapKey(kek, keyData)).equals(kek, keyData) == true
Please note that the implementation will not clear the byte[] key.
Please note, that unlike for encryption methods, calling this method twice with the same
keyDatamay return the same cipher text.- Parameters:
kek- the key-encryption key used to seed the key wrap algorithm. This must be a non-null, non-empty array of bytes. The current implementations use AES key wrapping and require an effective key length of 16, 24, or 32 bytes (128, 192, or 256 bits). Ifkekis longer than 32 bytes, only the first 32 bytes are used.keyData- The key data to be wrapped. This must be a non-null, non-empty array of bytes. The current implementations require at least 16 bytes and a length that is a multiple of 8 bytes.- Returns:
- The wrapped key data
- Throws:
CryptoException- If any problem occurs wrapping the key data. TheThrowable.getCause()method may provide additional information on the wrapping failure.- Since:
- 1.5
-
wrapKey
Wraps the givenkeyDatausing a symmetric key wrap algorithm.Note that the kek the key-encryption key used to seed the key wrap algorithm is selected by the implementation.
Note that this method and the
unwrapKey(byte[])method provide full round trip support:unwrapKey(wrapKey(keyData)).equals(keyData) == true
Please note that the implementation will not clear the byte[] key.
Please note, that unlike for encryption methods, calling this method twice with the same
keyDatamay return the same cipher text.- Parameters:
keyData- The key data to be wrapped. This must be a non-null, non-empty array of bytes. The current implementations require at least 16 bytes and a length that is a multiple of 8 bytes.- Returns:
- The wrapped key data
- Throws:
CryptoException- If any problem occurs wrapping the key data. TheThrowable.getCause()method may provide additional information on the wrapping failure.- Since:
- 1.5
-
unwrapKey
Unwraps the givenwrappedKeyusing a symmetric key wrap algorithm.Note that this method and the
wrapKey(byte[], byte[])method provide full round trip support:unwrapKey(wrapKey(kek, keyData)).equals(kek, keyData) == true
Please note that the implementation will not clear the byte[] key.
- Parameters:
kek- the key-encryption key used to seed the key wrap algorithm. This must be a non-null, non-empty array of bytes. The current implementations use AES key wrapping and require an effective key length of 16, 24, or 32 bytes (128, 192, or 256 bits). Ifkekis longer than 32 bytes, only the first 32 bytes are used.wrappedKeyData- The wrapped key data. This must be a non-null, non-empty array of bytes.- Returns:
- The unwrapped key data
- Throws:
CryptoException- If any problem occurs unwrapping the key data. TheThrowable.getCause()method may provide additional information on the unwrapping failure.- Since:
- 1.5
-
unwrapKey
Unwraps the givenwrappedKeyusing a symmetric key wrap algorithm.Note that the kek the key-encryption key used to seed the key wrap algorithm is selected by the implementation.
Note that this method and the
wrapKey(byte[])method provide full round trip support:unwrapKey(wrapKey(keyData)).equals(keyData) == true
Please note that the implementation will not clear the byte[] key.
- Parameters:
wrappedKeyData- The wrapped key data. This must be a non-null, non-empty array of bytes.- Returns:
- The unwrapped key data
- Throws:
CryptoException- If any problem occurs unwrapping the key data. TheThrowable.getCause()method may provide additional information on the unwrapping failure.- Since:
- 1.5
-
nextRandomBytes
Fill the byte buffer with securely-generated pseudo-random bytes.- Parameters:
bytes- Buffer to fill with random bytes.- Throws:
NullPointerException- ifbytesisnull.CryptoException- If any problem occurs calculating the random data. TheThrowable.getCause()method may provide additional information on the failure.- Since:
- 1.1, Crypto Support 0.4
-
hmac_sha256
Generate HMAC bytes given a key and some text. In other, perhaps less cryptographically correct words, generates and returns a hash of 'text' encrypted by 'keyBytes'.The implementation is expected to implement the keyed hashing function using SHA-256 as the hash algorithm. See RFC 2104 for the HMAC specification.
Please note that the implementation will not clear the byte[] key.
If a string of character is to be hashed, it is suggested but not required to convert the String to a byte array using UTF-8.
- Parameters:
key- The key material for the HMAC operation. This must be a non-null, non-empty array of bytes.text- The clear text to apply the hash algorithm to.- Returns:
- The hash code.
- Throws:
CryptoException- If any problem occurs calculating the hash code of the text. TheThrowable.getCause()method may provide additional information on the failure.IllegalArgumentException- if thekeyortextisnullor an empty array.- Since:
- 1.1, Crypto Support 0.4
-
hmac_sha256
Generate HMAC bytes given some text. In other, perhaps less cryptographically correct words, generates and returns a hash of 'text' encrypted by 'keyBytes'.The implementation is expected to implement the keyed hashing function using SHA-256 as the hash algorithm. See RFC 2104 for the HMAC specification.
If a string of character is to be hashed, it is suggested but not required to convert the String to a byte array using UTF-8.
- Parameters:
text- The clear text to apply the hash algorithm to.- Returns:
- The hash code.
- Throws:
CryptoException- If any problem occurs calculating the hash code of the text. TheThrowable.getCause()method may provide additional information on the failure.IllegalArgumentException- iftextisnullor an empty array.- Since:
- 1.2
-
createKeyPair
Generates a key pair. This will generate a new key pair every time it is called.- Parameters:
algorithm- the standard string name of the algorithm. The current implementations support onlyRSA.- Returns:
- the generated key pair
- Throws:
CryptoException- If any problem occurs creating the key pair. TheThrowable.getCause()method may provide additional information on the key generation failure.IllegalArgumentException- if thealgorithmisnullor incorrect.- Since:
- 1.3
-
sign
Certificate sign(Certificate issuerCertificate, KeyPair keyPair, X500Principal subject, long before, long after) throws CryptoException Sign aCertificateeither using a provided issuer certificate or using theCertificatesubject as issuer (self-signed).- Parameters:
issuerCertificate- theCertificateof the issuer ornullto self-sign the certificate.keyPair- the key pair containing the certificate subjectPublicKeyand the issuerPrivateKeykey.subject- the subject of the certificate to be issuedbefore- thenotBeforeUTC timestamp for the certificate validity periodafter- thenotAfterUTC timestamp for the certificate validity period- Returns:
- the signed
Certificate - Throws:
CryptoException- if any problem occurs when signing- Since:
- 1.4
-
sign
Sign some data using the given private keyPlease note that the implementation will not clear the private key.
- Parameters:
text- the clear text to signprivateKey- the private key used to sign the clear textalgorithm- the standard string name of the algorithm. The current implementations supportSHA224withRSA,SHA256withRSA,SHA384withRSA, andSHA512withRSA.- Returns:
- the signedText
- Throws:
CryptoException- If any problem occurs signing the clear text. TheThrowable.getCause()method may provide additional information on the signing failure.IllegalArgumentException- if thealgorithmorprivateKeyisnullor incorrect.- Since:
- 1.3
-
verify
boolean verify(byte[] text, byte[] signedText, PublicKey publicKey, String algorithm) throws CryptoException Perform a signature verification with the given public key.Please note that the implementation will not clear the public key.
- Parameters:
text- The clear text which has been signedsignedText- the signed text to be verifiedpublicKey- the public key used to verify the signaturealgorithm- the standard string name of the algorithm. The current implementations supportSHA224withRSA,SHA256withRSA,SHA384withRSA, andSHA512withRSA.- Returns:
trueif the alleged signature (signedText) is the actual signature of the specified data (text)- Throws:
CryptoException- If any problem occurs verifying the signed text. TheThrowable.getCause()method may provide additional information on the verification failure.IllegalArgumentException- if thealgorithmorpublicKeyisnullor incorrect.- Since:
- 1.3
-