Encryption

Shade402 provides RSA encryption utilities for resource field privacy.

Overview

Resource encryption prevents payment request replay attacks by ensuring the resource field is encrypted and can only be decrypted by the server.

Generating Keys

Generate an RSA key pair:

import { generateKeyPair } from '@shade402/core';

const { publicKey, privateKey } = generateKeyPair();

// Keys are in PEM format
console.log(publicKey);
// -----BEGIN PUBLIC KEY-----
// ...
// -----END PUBLIC KEY-----

console.log(privateKey);
// -----BEGIN PRIVATE KEY-----
// ...
// -----END PRIVATE KEY-----

Store keys securely:

  • Public key: Can be shared (included in 402 responses)

  • Private key: Must be kept secret (server-side only)

Encrypting Resources

Encrypt a resource string using the server's public key:

The encryption uses:

  • RSA-OAEP padding

  • SHA-256 hash algorithm

  • 2048-bit keys

Decrypting Resources

Decrypt an encrypted resource using the server's private key:

Server-side Setup

Generate and Store Keys

Load Keys in Application

Using Environment Variables

Client-side Usage

The client automatically handles encryption:

Manual Encryption (Explicit Client)

Complete Example

Server

Client

Security Considerations

  1. Keep private key secure: Never expose the private key

  2. Use strong keys: 2048-bit keys are recommended

  3. Rotate keys periodically: Generate new keys and update configuration

  4. Use secure storage: Store keys in environment variables or key management service

  5. Separate keys per environment: Use different keys for dev/staging/production

Key Management

Using Key Management Service

Using Environment Variables

Error Handling

Best Practices

  1. Generate keys once and reuse them

  2. Store keys securely (environment variables, key management service)

  3. Rotate keys periodically

  4. Use separate keys for different environments

  5. Never commit keys to version control

  6. Monitor for encryption/decryption failures

  7. Test encryption/decryption in your application

Next Steps

Last updated