Package org.apache.commons.crypto.cipher
Class OpenSslNative
java.lang.Object
org.apache.commons.crypto.cipher.OpenSslNative
JNI implementation for OpenSSL 1.x called from
OpenSsl
. The native methods in this
class are defined in OpenSslNative.h (generated by javah).-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Hides this constructor from external access. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
clean
(long context) Cleans the context at native.static int
ctrl
(long context, int type, int arg, byte[] data) Allows various cipher specific parameters to be determined and set.static int
doFinal
(long context, ByteBuffer output, int offset, int maxOutputLength) Finishes a multiple-part operation.static int
doFinalByteArray
(long context, byte[] output, int offset, int maxOutputLength) Finishes a multiple-part operation.static long
init
(long context, int mode, int alg, int padding, byte[] key, byte[] iv) Declares a native method to initialize the cipher context.static long
initContext
(int algorithm, int padding) Declares a native method to initialize the cipher context.static void
initIDs()
Declares a native method to initialize JNI field and method IDs.static int
update
(long context, ByteBuffer input, int inputOffset, int inputLength, ByteBuffer output, int outputOffset, int maxOutputLength) Continues a multiple-part encryption/decryption operation.static int
updateByteArray
(long context, byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) Continues a multiple-part encryption/decryption operation.static int
updateByteArrayByteBuffer
(long context, byte[] input, int inputOffset, int inputLength, ByteBuffer output, int outputOffset, int maxOutputLength) Continues a multiple-part encryption/decryption operation.
-
Constructor Details
-
OpenSslNative
private OpenSslNative()Hides this constructor from external access.
-
-
Method Details
-
clean
public static void clean(long context) Cleans the context at native.- Parameters:
context
- The cipher context address
-
ctrl
public static int ctrl(long context, int type, int arg, byte[] data) Allows various cipher specific parameters to be determined and set. it will call OpenSSL's API int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) In OpenSSL, data type of ptr can be char* or long*. Here, we map java's byte[] to native void*ptr. Note that the byte order is ByteOrder.nativeOrder.- Parameters:
context
- The cipher context addresstype
- CtrlValuesarg
- argument like a tag lengthdata
- byte buffer or null- Returns:
- return 0 if there is any error, else return 1.
-
doFinal
Finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Parameters:
context
- The cipher context addressoutput
- The byte buffer for the resultoffset
- The offset in output where the result is storedmaxOutputLength
- The maximum length for output- Returns:
- The number of bytes stored in output
-
doFinalByteArray
public static int doFinalByteArray(long context, byte[] output, int offset, int maxOutputLength) Finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Parameters:
context
- The cipher context addressoutput
- The byte array for the resultoffset
- The offset in output where the result is storedmaxOutputLength
- The maximum length for output- Returns:
- The number of bytes stored in output
-
init
public static long init(long context, int mode, int alg, int padding, byte[] key, byte[] iv) Declares a native method to initialize the cipher context.- Parameters:
context
- The cipher context addressmode
- ENCRYPT_MODE or DECRYPT_MODEalg
- Algorithm Mode of OpenSslpadding
- the padding mode of OpenSsl cipherkey
- crypto keyiv
- crypto iv- Returns:
- the context address of cipher
-
initContext
public static long initContext(int algorithm, int padding) Declares a native method to initialize the cipher context.- Parameters:
algorithm
- The algorithm name of cipherpadding
- The padding name of cipher- Returns:
- the context address of cipher
-
initIDs
public static void initIDs()Declares a native method to initialize JNI field and method IDs. -
update
public static int update(long context, ByteBuffer input, int inputOffset, int inputLength, ByteBuffer output, int outputOffset, int maxOutputLength) Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Parameters:
context
- The cipher context addressinput
- The input byte bufferinputOffset
- The offset in input where the input startsinputLength
- The input lengthoutput
- The byte buffer for the resultoutputOffset
- The offset in output where the result is storedmaxOutputLength
- The maximum length for output- Returns:
- The number of bytes stored in output
-
updateByteArray
public static int updateByteArray(long context, byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Parameters:
context
- The cipher context addressinput
- The input byte arrayinputOffset
- The offset in input where the input startsinputLength
- The input lengthoutput
- The byte array for the resultoutputOffset
- The offset in output where the result is storedmaxOutputLength
- The maximum length for output- Returns:
- The number of bytes stored in output
-
updateByteArrayByteBuffer
public static int updateByteArrayByteBuffer(long context, byte[] input, int inputOffset, int inputLength, ByteBuffer output, int outputOffset, int maxOutputLength) Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Parameters:
context
- The cipher context addressinput
- The input byte arrayinputOffset
- The offset in input where the input startsinputLength
- The input lengthoutput
- The byte buffer for the resultoutputOffset
- The offset in output where the result is storedmaxOutputLength
- The maximum length for output- Returns:
- The number of bytes stored in output
-