> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rsa

> For creating and verifying RSA signatures in CRMScript.

For creating and verifying RSA signatures in CRMScript.

## Constructors

### Rsa()

```crmscript theme={null}
Rsa
```

Initializes a new instance of the Rsa class.

## 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.

```crmscript theme={null}
Bool loadPrivateKey(String privateKey, String password)
```

**Returns:** [CRMScript.Global.Bool](CRMScript.Global.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.

```crmscript theme={null}
Bool loadPublicKey(String publicKey)
```

**Returns:** [CRMScript.Global.Bool](CRMScript.Global.Bool) - True if the key was loaded successfully; otherwise, false.

**Example:**

```crmscript theme={null}
String pub_key = "-----BEGIN PUBLIC KEY-----MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBig....f8kXvJLBEGBR5AgMBAAE=-----END PUBLIC KEY-----";
Rsa rsa;
Bool b = rsa.loadPublicKey(pub_key);
print(b.toString());
```

## 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.

```crmscript theme={null}
Byte[] createSignature(Byte[] data, Integer hashingAlgorithm)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - Generated RSA signature as byte array.

<Note>
  Hashing algorithm:

  | Value | Description |
  | ----- | ----------- |
  | 0     | SHA1        |
  | 1     | SHA256      |
  | 2     | SHA384      |
  | 3     | SHA512      |
</Note>

**Example:**

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

## 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.

```crmscript theme={null}
Byte[] createSignature(String data, Integer hashingAlgorithm)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - Generated RSA signature as byte array.

<Note>
  Hashing algorithm:

  | Value | Description |
  | ----- | ----------- |
  | 0     | SHA1        |
  | 1     | SHA256      |
  | 2     | SHA384      |
  | 3     | SHA512      |
</Note>

**Example:**

```crmscript theme={null}
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);
```

## 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.

```crmscript theme={null}
Bool verifySignature(Byte[] data, Byte[] signature, Integer hashingAlgorithm)
```

**Returns:** [CRMScript.Global.Bool](CRMScript.Global.Bool) - True if valid; otherwise, false.

<Note>
  Hashing algorithm:

  | Value | Description |
  | ----- | ----------- |
  | 0     | SHA1        |
  | 1     | SHA256      |
  | 2     | SHA384      |
  | 3     | SHA512      |
</Note>

**Example:**

```crmscript theme={null}
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));
```

## 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.

```crmscript theme={null}
Bool verifySignature(String data, Byte[] signature, Integer hashingAlgorithm)
```

**Returns:** [CRMScript.Global.Bool](CRMScript.Global.Bool) - True if valid; otherwise, false.

<Note>
  Hashing algorithm:

  | Value | Description |
  | ----- | ----------- |
  | 0     | SHA1        |
  | 1     | SHA256      |
  | 2     | SHA384      |
  | 3     | SHA512      |
</Note>

**Example:**

```crmscript theme={null}
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));
```

## 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.

```crmscript theme={null}
Byte[] encrypt(Byte[] data)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - The returned byte array contains the encrypted data.

**Example:**

```crmscript theme={null}
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);
```

## 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.

```crmscript theme={null}
Byte[] decrypt(Byte[] data)
```

**Returns:** [CRMScript.Global.Byte](CRMScript.Global.Byte)\[] - The returned byte array contains the decrypted data.

**Example:**

```crmscript theme={null}
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));
```


## Related topics

- [Namespace CRMScript.Native](/en/automation/crmscript/reference/CRMScript.Native.md)
- [How to sign system user token](/en/api/authentication/online/auth-application/sign-system-user-token.md)
- [How to set up a DKIM Record](/en/online/mail-services/mailgun/dkim/set-up.md)
- [New certificate](/en/developer-portal/create-app/config/new-certificate.md)
- [Request to publish](/en/developer-portal/create-app/request-to-publish.md)
- [Migrate from the legacy mirroring service](/en/online/mirroring/migrate.md)
