Class OSymmetricKey
java.lang.Object
com.orientechnologies.orient.core.security.symmetrickey.OSymmetricKey
Implements a symmetric key utility class that can create default keys and keys from a String, a
file, a KeyStore, and from the OSymmetricKeyConfig interface.
Static creation methods are provided for each type: OSymmetricKey.fromConfig() OSymmetricKey.fromString() OSymmetricKey.fromFile() OSymmetricKey.fromStream() OSymmetricKey.fromKeystore()
The encrypt() methods return a specialized Base64-encoded JSON document with these properties (depending on the cipher transform): "algorithm", "transform", "iv", "payload"
The decrypt() and decryptAsString() methods accept the Base64-encoded JSON document.
A symmetric key credential interceptor is provided (OSymmetricKeyCI) as well as several authenticators: OSecuritySymmetricKeyAuth, OSystemSymmetricKeyAuth
- Author:
- S. Colin Leister
-
Constructor Summary
ConstructorsConstructorDescriptionOSymmetricKey(String algorithm, String base64Key) Sets the SecretKey based on the specified algorithm and Base64 key specified.OSymmetricKey(String secretKeyAlgorithm, String cipherTransform, int keySize) Creates a key based on the algorithm, transformation, and key size specified.OSymmetricKey(SecretKey secretKey) Uses the specified SecretKey as the private key and sets key algorithm from the SecretKey. -
Method Summary
Modifier and TypeMethodDescriptionprotected static byte[]convertFromBase64(String base64) protected static StringconvertToBase64(byte[] bytes) protected voidcreate()byte[]This method decrypts the Base64-encoded JSON document using the specified algorithm and cipher transformation.decryptAsString(String encodedJSON) This method decrypts the Base64-encoded JSON document using the specified algorithm and cipher transformation.protected StringencodeJSON(byte[] encrypted, byte[] initVector) encrypt(byte[] bytes) This method encrypts an array of bytes.This is a convenience method that takes a String argument, encodes it as Base64, then calls encrypt(byte[]).This method encrypts an array of bytes.This is a convenience method that takes a String argument, encodes it as Base64, then calls encrypt(byte[]).static OSymmetricKeyfromConfig(OSymmetricKeyConfig keyConfig) Creates an OSymmetricKey from an OSymmetricKeyConfig interface.static OSymmetricKeyCreates an OSymmetricKey from a file containing a Base64 key.static OSymmetricKeyfromKeystore(InputStream is, String password, String keyAlias, String keyPassword) Creates an OSymmetricKey from a Java "JCEKS" KeyStore.static OSymmetricKeyfromKeystore(String path, String password, String keyAlias, String keyPassword) Creates an OSymmetricKey from a Java "JCEKS" KeyStore.static OSymmetricKeyfromStream(String algorithm, InputStream is) Creates an OSymmetricKey from an InputStream containing a Base64 key.static OSymmetricKeyfromString(String algorithm, String base64Key) Creates an OSymmetricKey from a Base64 key.Returns the internal SecretKey as a Base64 String.getDefaultCipherTransform(String transform) intgetIteration(int iteration) getKeyAlgorithm(String algorithm) intgetKeySize(int bits) intgetSaltLength(int length) getSeedAlgorithm(String algorithm) getSeedPhrase(String phrase) voidsaveToKeystore(OutputStream os, String ksPasswd, String keyAlias, String keyPasswd) Saves the internal SecretKey as a KeyStore.voidSaves the internal SecretKey to the specified OutputStream as a Base64 String.protected static StringseparateAlgorithm(String cipherTransform) Returns the secret key algorithm portion of the cipher transformation.setDefaultCipherTransform(String transform) setIteration(int iteration) setKeyAlgorithm(String algorithm) setKeySize(int bits) setSaltLength(int length) setSeedAlgorithm(String algorithm) setSeedPhrase(String phrase)
-
Constructor Details
-
OSymmetricKey
public OSymmetricKey() -
OSymmetricKey
Creates a key based on the algorithm, transformation, and key size specified. -
OSymmetricKey
Uses the specified SecretKey as the private key and sets key algorithm from the SecretKey.- Throws:
OSecurityException
-
OSymmetricKey
Sets the SecretKey based on the specified algorithm and Base64 key specified.- Throws:
OSecurityException
-
-
Method Details
-
getDefaultCipherTransform
-
getIteration
public int getIteration(int iteration) -
getKeyAlgorithm
-
getKeySize
public int getKeySize(int bits) -
getSaltLength
public int getSaltLength(int length) -
getSeedAlgorithm
-
getSeedPhrase
-
setDefaultCipherTransform
-
setIteration
-
setKeyAlgorithm
-
setKeySize
-
setSaltLength
-
setSeedAlgorithm
-
setSeedPhrase
-
create
protected void create() -
separateAlgorithm
Returns the secret key algorithm portion of the cipher transformation. -
fromConfig
Creates an OSymmetricKey from an OSymmetricKeyConfig interface. -
fromString
Creates an OSymmetricKey from a Base64 key. -
fromFile
Creates an OSymmetricKey from a file containing a Base64 key. -
fromStream
Creates an OSymmetricKey from an InputStream containing a Base64 key. -
fromKeystore
public static OSymmetricKey fromKeystore(String path, String password, String keyAlias, String keyPassword) Creates an OSymmetricKey from a Java "JCEKS" KeyStore.- Parameters:
path- The location of the KeyStore file.password- The password for the KeyStore. May be null.keyAlias- The alias name of the key to be used from the KeyStore. Required.keyPassword- The password of the key represented by keyAlias. May be null.
-
fromKeystore
public static OSymmetricKey fromKeystore(InputStream is, String password, String keyAlias, String keyPassword) Creates an OSymmetricKey from a Java "JCEKS" KeyStore.- Parameters:
is- The InputStream used to load the KeyStore.password- The password for the KeyStore. May be null.keyAlias- The alias name of the key to be used from the KeyStore. Required.keyPassword- The password of the key represented by keyAlias. May be null.
-
getBase64Key
Returns the internal SecretKey as a Base64 String. -
convertToBase64
-
convertFromBase64
-
encrypt
This is a convenience method that takes a String argument, encodes it as Base64, then calls encrypt(byte[]).- Parameters:
value- The String to be encoded to Base64 then encrypted.- Returns:
- A Base64-encoded JSON document.
-
encrypt
This is a convenience method that takes a String argument, encodes it as Base64, then calls encrypt(byte[]).- Parameters:
transform- The cipher transformation to use.value- The String to be encoded to Base64 then encrypted.- Returns:
- A Base64-encoded JSON document.
-
encrypt
This method encrypts an array of bytes.- Parameters:
bytes- The array of bytes to be encrypted.- Returns:
- The encrypted bytes as a Base64-encoded JSON document or null if unsuccessful.
-
encrypt
This method encrypts an array of bytes.- Parameters:
transform- The cipher transformation to use.bytes- The array of bytes to be encrypted.- Returns:
- The encrypted bytes as a Base64-encoded JSON document or null if unsuccessful.
-
encodeJSON
-
decryptAsString
This method decrypts the Base64-encoded JSON document using the specified algorithm and cipher transformation.- Parameters:
encodedJSON- The Base64-encoded JSON document.- Returns:
- The decrypted array of bytes as a UTF8 String or null if not successful.
-
decrypt
This method decrypts the Base64-encoded JSON document using the specified algorithm and cipher transformation.- Parameters:
encodedJSON- The Base64-encoded JSON document.- Returns:
- The decrypted array of bytes or null if unsuccessful.
-
saveToStream
Saves the internal SecretKey to the specified OutputStream as a Base64 String. -
saveToKeystore
Saves the internal SecretKey as a KeyStore.
-