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
Keep private key secure: Never expose the private key
Use strong keys: 2048-bit keys are recommended
Rotate keys periodically: Generate new keys and update configuration
Use secure storage: Store keys in environment variables or key management service
Separate keys per environment: Use different keys for dev/staging/production
Key Management
Using Key Management Service
Using Environment Variables
Error Handling
Best Practices
Generate keys once and reuse them
Store keys securely (environment variables, key management service)
Rotate keys periodically
Use separate keys for different environments
Never commit keys to version control
Monitor for encryption/decryption failures
Test encryption/decryption in your application
Next Steps
Learn about Security best practices
Check out Core Package overview
See Examples for usage
Last updated
