# RSA Signature Verification

## Download the Public Key File

Download the public key file by clicking the link below and store it somewhere on your server

{% embed url="<https://govbill.ams3.cdn.digitaloceanspaces.com/govbill-uat.public.key.pem>" %}
Public key for sandbox webhook signature verification
{% endembed %}

{% embed url="<https://govbill.ams3.cdn.digitaloceanspaces.com/govbill-prod.public.key.pem>" %}
Public key for production webhook signature verification
{% endembed %}

## Next Steps

Below is the sample callback data for this demonstration (sample redirect data is available [here](https://docs.govbill.ug/utility-functions/handling-card-redirects));

```json
{
  "id": 266,
  "merchant_reference": "CSTREF2NZQQW53KJMQPE",
  "internal_reference": "GOVNETJFTKL9BSYQQKVKRU",
  "transaction_type": "COLLECTION",
  "request_currency": "UGX",
  "request_amount": 23000,
  "transaction_currency": "UGX",
  "transaction_amount": 23000,
  "transaction_fee": 690,
  "charge_customer": false,
  "total_credit": 22310,
  "provider_code": "airtel_money_ug",
  "transaction_status": "COMPLETED",
  "status_message": "Transaction Completed Successfully",
  "transaction_account": "256752000001",
  "customer_name": "SANDBOX CUSTOMER",
  "institution_name": "Airtel Money"
}
```

1. Obtain the value of the `rsa-signature` header (if callback) OR the value of the `rsa_signature` query parameter (if redirect).
2. Form the string payload to be used in signature verification. This is obtained by concatenating values of the callback/redirect data in the format; `id:internal_reference:transaction_status:merchant_reference`and these values are obtained from the callback/redirect data. The string payload would therefore be `266:GOVNETJFTKL9BSYQQKVKRU:COMPLETED:CSTREF2NZQQW53KJMQPE`
3. Use the public key obtained above to verify the signature as described in the sample source codes below;&#x20;

{% tabs %}
{% tab title="PHP" %}

```php
<?php

public function isValidSignature() {
    $file = "path-to-file/govnet.public.key.pem";
    $keyContent = file_get_contents($file);
    $publicKey = openssl_get_publickey($keyContent);
    $strPayload = "266:GOVNETJFTKL9BSYQQKVKRU:COMPLETED:CSTREF2NZQQW53KJMQPE";
    $signature = base64_decode("value-of-rsa-signature");

    /*true or false*/
    return openssl_verify($strPayload, $signature, $publicKey, "sha256") == 1;
}

?>
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
const crypto = require('crypto');
const fs = require('fs');

function isValidSignature() {
    const strPayload = "266:GOVNETJFTKL9BSYQQKVKRU:COMPLETED:CSTREF2NZQQW53KJMQPE";
    const signature = "value-of-rsa-signature";
    const publicKeyFile = "path-to-file/govnet.public.key.pem";
    const publicKey = fs.readFileSync(publicKeyFile).toString().replace(/\\n/g, '\n');

    const verify = crypto.createVerify("SHA256");
    verify.write(strPayload);
    verify.end();

    /*true or false*/
    return verify.verify(publicKey, signature, 'base64');
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.govbill.ug/callbacks-and-redirects/rsa-signature-verification.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
