# Date Preparatoin and Encryption

#### RSA Encryption

To ensure the security and integrity of the data being sent to the Hyperhashing API, we use RSA encryption. This section outlines the steps required to generate RSA keys and how to use them for encrypting data before making API requests.

**Generating RSA Keys**

1. **Generate RSA Key Pair**: Users need to generate an RSA key pair (public and private keys). The public key will be shared with Hyperhashing, while the private key will be kept secure by the user.
2. **Share Public Key**: Once the RSA key pair is generated, the user must provide Hyperhashing with their public key. This key will be used by Hyperhashing to decrypt the data sent by the user.

#### Generating RSA Keys Using OpenSSL

**Step 1: Install OpenSSL**

If you don't have OpenSSL installed, you can install it using your package manager. For example, on Ubuntu, you can install it with:

```bash
sudo apt-get update
sudo apt-get install openssl
```

**Step 2: Generate the RSA Private Key**

To generate a 2048-bit RSA private key, use the following command:

```bash
openssl genpkey -algorithm RSA -out private_key.pem -aes256
```

This command generates a private key and encrypts it with AES-256. You will be prompted to enter a passphrase to secure the key.

**Step 3: Generate the RSA Public Key**

Once you have the private key, you can generate the corresponding public key with:

```bash
openssl rsa -pubout -in private_key.pem -out public_key.pem
```

This command reads the private key and outputs the corresponding public key.

**Sharing the Public Key**

The public key generated in the above step should be securely shared with Hyperhashing.&#x20;

#### Encrypting Data

Once the RSA keys are set up, the user can proceed to encrypt the data before sending it to the API. The data is first converted to a JSON string and then encrypted using the private key.

#### Encrypting Data

Once the RSA keys are set up, the user can proceed to encrypt the data before sending it to the API. The data is first converted to a JSON string and then encrypted using the private key.

Here is an example of how to encrypt data using the `node-rsa` library:

```javascript
const NodeRSA = require('node-rsa');

function rsa_encrypt(text) {
  // Replace with your RSA private key
  let privateKey = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----';
  let key = new NodeRSA(privateKey);
  
  // Convert the text to a Buffer
  let buf = Buffer.from(text, 'utf8');
  
  // Encrypt the buffer and convert it to a Base64 string
  let encrypted = key.sign(buf);
  
  return encrypted.toString('base64');
}

const data = {
  order_id: "17188041436139639757",
  nonce: makeid(10),
  timestamp: Math.floor(Date.now() / 1000),
};

const hash = rsa_encrypt(JSON.stringify(data));
console.log('Encrypted Data:', hash);

function makeid(length) {
  let result = "";
  const characters = "abcdefghijklmnopqrstuvwxyz0123456789";
  const charactersLength = characters.length;
  let counter = 0;
  while (counter < length) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
    counter += 1;
  }
  return result;
}
```

#### Example Request Preparation

After encrypting the data, the user can prepare the API request as follows:

```javascript
const axios = require('axios');

async function main() {
  const data = {
    order_id: "17188041436139639757",
    nonce: makeid(10),
    timestamp: Math.floor(Date.now() / 1000),
  };

  const hash = rsa_encrypt(JSON.stringify(data));

  axios.post("https://payment.hyperhashing.com/v1/token/withdraw", {
    merchant_id: "xxxx-xxxx-xxxx-xxxx",
    hash: hash,
    data: data,
  })
  .then(function (response) {
    console.log(response.config.data);
    console.log(response.data);
  })
  .catch(function (error) {
    console.log(error);
  });
}

main();
```

In this example, the data object is encrypted using the user's private RSA key, and the resulting hash is included in the API request.


---

# 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://hyperhashing.gitbook.io/hyperhashing-api/withdraw-order-v2/date-preparatoin-and-encryption.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.
