Class Rsa

For creating and verifying RSA signatures in CRMScript.

Constructors

Rsa()

Initializes a new instance of the Rsa class.

Declaration

Rsa

Examples

Rsa rsa;

Methods

loadPrivateKey(String,String)

Loads a private key. The password is optional and should be specified if needed. This method must be called in prior to creating a signature.

Declaration

Bool loadPrivateKey(String privateKey, String password)

Examples

String priv_key = "-----BEGIN RSA PRIVATE KEY-----MIIBOwIBAAJBAMP2VEmsyBYmla....wA8pE36phyinhYbewXx2X8tQEww==-----END RSA PRIVATE KEY-----";
Rsa rsa;

Bool b = rsa.loadPrivateKey(priv_key, "foobar");

Parameters

Type Name Description
String privateKey Private key
String password Password

Returns

Type Description
Bool True if the key was loaded successfully; otherwise, false.

loadPublicKey(String)

Loads a public key. This method must be called in prior to creating a signature.

Declaration

Bool loadPublicKey(String publicKey)

Examples

String pub_key = "-----BEGIN PUBLIC KEY-----MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBig....f8kXvJLBEGBR5AgMBAAE=-----END PUBLIC KEY-----";
Rsa rsa;

Bool b = rsa.loadPublicKey(pub_key);
print(b.toString());

Parameters

Type Name Description
String publicKey Public key

Returns

Type Description
Bool True if the key was loaded successfully; otherwise, false.

createSignature(Byte[],Integer)

This method will take an array of binary data and create a RSA signature using the provided hashing algorithm. The signature is returned as a byte array. The private key must be loaded prior to calling this method.

Declaration

Byte[] createSignature(Byte[] data, Integer hashingAlgorithm)

Examples

String priv_key = "-----BEGIN RSA PRIVATE KEY-----MIIBOwIBAAJBAMP2VEmsyBYmla....wA8pE36phyinhYbewXx2X8tQEww==-----END RSA PRIVATE KEY-----";
Rsa rsa;
Bool loaded = rsa.loadPrivateKey(priv_key, "");
Byte[] content = String("Hello world").toByteArray();
Byte[] signature = rsa.createSignature(content, 3);
String base64Signature = encodeBase64(signature,false);
printLine(base64Signature);

Parameters

Type Name Description
Byte[] data Binary data
Integer hashingAlgorithm Which hashing algorithm, see supported above

Returns

Type Description
Byte[] Generated RSA signature as byte array.

createSignature(String,Integer)

This method takes a string of data and create a RSA signature using the provided hashing algorithm. This method is provided as a convenient way to create signatures for string data. Note that the string must be in UTF-8 encoding. If it is not, you need to use the more universal method taking a byte array as input. The private key must be loaded prior to calling this method.

Declaration

Byte[] createSignature(String data, Integer hashingAlgorithm)

Examples

String priv_key = "-----BEGIN RSA PRIVATE KEY-----MIIBOwIBAAJBAMP2VEmsyBYmla....wA8pE36phyinhYbewXx2X8tQEww==-----END RSA PRIVATE KEY-----";
Rsa rsa;

Bool b = rsa.loadPrivateKey(priv_key, "");
Byte[] signature = rsa.createSignature("Hello world", 3);
String base64Signature = encodeBase64(signature,false);
printLine(base64Signature);

Parameters

Type Name Description
String data String of data
Integer hashingAlgorithm Which hashing algorithm, see supported above

Returns

Type Description
Byte[] Generated RSA signature as byte array.

verifySignature(Byte[],Byte[],Integer)

This method will try to verify the RSA signature given the data and return if it was a success or not. Although you should pass in the hashing algorithm, the underlying code will try other algorithms if the signature does not pass the provided algorithm. This might change in the future, so you should specify the correct algorithm. The public key must be loaded prior to calling this method.

Declaration

Bool verifySignature(Byte[] data, Byte[] signature, Integer hashingAlgorithm)

Examples

String pub_key = "-----BEGIN PUBLIC KEY-----MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBig....f8kXvJLBEGBR5AgMBAAE=-----END PUBLIC KEY-----";
Rsa rsa;

Bool loaded = rsa.loadPublicKey(pub_key);
Byte[] content = String("Hello world").toByteArray();
String base64Signature = "vka6U5GrfxMWTGO....WW322CGWQ1SPJr8uZ+6"
Bool verified = rsa.verifySignature(content, decodeBase64(base64Signature));

Parameters

Type Name Description
Byte[] data Binary data
Byte[] signature Binary signature
Integer hashingAlgorithm Which hashing algorithm, see supported above.

Returns

Type Description
Bool True if valid; otherwise, false.

verifySignature(String,Byte[],Integer)

This method will try to verify the RSA signature given the data and return if it was a success or not. Note that the string must be in UTF-8 encoding. If it is not, you need to use the more universal method taking a byte array as input. Although you should pass in the hashing algorithm, the underlying code will try other algorithms if the signature does not pass the provided algorithm. This might change in the future, so you should specify the correct algorithm. The public key must be loaded prior to calling this method.

Declaration

Bool verifySignature(String data, Byte[] signature, Integer hashingAlgorithm)

Examples

String pub_key = "-----BEGIN PUBLIC KEY-----MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBig....f8kXvJLBEGBR5AgMBAAE=-----END PUBLIC KEY-----";
Rsa rsa;

Bool loaded = rsa.loadPublicKey(pub_key);
String base64Signature = "vka6U5GrfxMWTGO....WW322CGWQ1SPJr8uZ+6"
Bool verified = rsa.verifySignature("Hello world", decodeBase64(base64Signature));

Parameters

Type Name Description
String data String data
Byte[] signature Binary signature
Integer hashingAlgorithm Which hashing algorithm, see supported above.

Returns

Type Description
Bool True if valid; otherwise, false.

encrypt(Byte[])

A valid public key must have been loaded into the class. This method will use the public key and do an RSA encryption of the content.

Declaration

Byte[] encrypt(Byte[] data)

Examples

String pub_key = "-----BEGIN PUBLIC KEY-----MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBig....f8kXvJLBEGBR5AgMBAAE=-----END PUBLIC KEY-----";
Rsa rsa;

Bool loaded = rsa.loadPublicKey(pub_key);
Byte[] content = String("Hello world").toByteArray();
Byte[] encrypted = rsa.encrypt(content);

Parameters

Type Name Description
Byte[] data Binary data

Returns

Type Description
Byte[] The returned byte array contains the encrypted data.

decrypt(Byte[])

A valid private key matching the public key must have been loaded into the class. This method will use the private key and do an RSA decryption of the content. The returned byte array contains the decrypted data.

Declaration

Byte[] decrypt(Byte[] data)

Examples

String pub_key = "-----BEGIN PUBLIC KEY-----MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBig....f8kXvJLBEGBR5AgMBAAE=-----END PUBLIC KEY-----";
String priv_key = "-----BEGIN RSA PRIVATE KEY-----MIIBOwIBAAJBAMP2VEmsyBYmla....wA8pE36phyinhYbewXx2X8tQEww==-----END RSA PRIVATE KEY-----";
Rsa rsa;
rsa.loadPublicKey(pub_key);
rsa.loadPrivateKey(priv_key, "");
Byte[] content = String("Hello world").toByteArray();
Byte[] encrypted = rsa.encrypt(content);
Byte[] decrypted = rsa.decrypt(encrypted);
printLine(String(decrypted));

Parameters

Type Name Description
Byte[] data Binary data

Returns

Type Description
Byte[] The returned byte array contains the decrypted data.