class securityIO extends Object
A class to provide cryptographic features to Jervis such as RSA encryption and base64 encoding.
To run this example, clone Jervis and execute ./gradlew console to bring up a Groovy Console with the classpath set up.
import net.gleske.jervis.tools.securityIO if(!(new File('/tmp/id_rsa').exists())) { 'openssl genrsa -out /tmp/id_rsa 2048'.execute().waitFor() 'openssl rsa -in /tmp/id_rsa -pubout -outform pem -out /tmp/id_rsa.pub'.execute().waitFor() } def security = new securityIO(new File("/tmp/id_rsa").text) println 'Key size: ' + security.id_rsa_keysize.toString() def s = security.rsaEncrypt('hello friend') println 'Length of encrypted output: ' + s.length() println 'Encrypted string:' println s println 'Decrypted string:' println security.rsaDecrypt(s) new File('/tmp/id_rsa').delete() new File('/tmp/id_rsa.pub').delete()
Modifiers | Name | Description |
---|---|---|
int |
id_rsa_keysize |
Shortcut to getting the key size of key_pair. |
KeyPair |
key_pair |
A decoded RSA key pair used for encryption and decryption. |
Constructor and description |
---|
securityIO
() Instantiates an unconfigured instance of this class. |
securityIO
(String private_key_pem) Instantiates the class and configures a private key for decryption. |
Type Params | Return Type | Name and description |
---|---|---|
|
byte[] |
decodeBase64Bytes(String content) Decode a base64 String into Bytes. |
|
String |
decodeBase64String(String content) Decode a base64 String. |
|
String |
encodeBase64(String content) Encode a String into a base64 String. |
|
String |
encodeBase64(byte[] content) Encode raw Bytes into a base64 String. |
|
int |
getId_rsa_keysize() Gets the id_rsa_keysize from the decoded private key. |
|
Boolean |
isSecureField(def field) Checks to see if a field in the Jervis YAML is a secure field. |
|
String |
rsaDecrypt(String ciphertext) Uses RSA asymetric encryption to decrypt a cipher text String and outputs plain text. |
|
String |
rsaEncrypt(String plaintext) Uses RSA asymetric encryption to encrypt a plain text String and outputs cipher text. |
|
void |
setId_rsa_keysize(int i) A noop which does nothing. |
|
void |
setKey_pair(String pem) Sets key_pair by decoding the String. |
Shortcut to getting the key size of key_pair.
A decoded RSA key pair used for encryption and decryption. The key size can be determined from the modulus. For example,
println key_pair.private.modulus.bitLength() println key_pair.public.modulus.bitLength()
Instantiates an unconfigured instance of this class. Call setKey_pair(java.lang.String) to properly use this class.
Instantiates the class and configures a private key for decryption. Automatically calls setKey_pair(java.lang.String) as part of instantiating.
private_key_pem
- The contents of an X.509 PEM encoded RSA private key.Decode a base64 String into Bytes.
content
- Base64 encoded String.Decode a base64 String.
content
- Base64 encoded String.Encode a String into a base64 String.
content
- A plain String.Encode raw Bytes into a base64 String.
content
- Base64 encoded String.Gets the id_rsa_keysize from the decoded private key.
Checks to see if a field in the Jervis YAML is a secure field. If it is then decryption should be attempted. This only detects of decryption is plausible.
property
- A simple object that can take multiple types to check against.Uses RSA asymetric encryption to decrypt a cipher text String and outputs plain text. For third party reference, this is essentially executing the following commands in a terminal.
echo 'ciphertext' | openssl enc -base64 -A -d | openssl rsautl -decrypt -inkey /tmp/id_rsa
ciphertext
- A Base64 encoded cipher text String to be decrypted.Uses RSA asymetric encryption to encrypt a plain text String and outputs cipher text. For third party reference, this is essentially executing the following commands in a terminal.
echo -n 'plaintext' | openssl rsautl -encrypt -inkey ./id_rsa.pub -pubin | openssl enc -base64 -A
plaintext
- A plain text String to be encrypted.A noop which does nothing. It prevents setting the id_rsa_keysize because the getter is automatically calculated from key_pair. This method throws a SecurityException if it is called.
Jervis API documentation.