encryption-service
Framework: react
π§© Step 1: Create Encryption Initialization Component
First, create a provider-style component that initializes the encryption service once when the application loads.
π File: src/providers/EncryptionServiceProvider.tsx
tsx
"use client";
import React from "react";
import { CONFIG } from "@/config";
import { initEncryptionService } from "universe-code/react";
/**
* Initializes the encryption service with a secret key.
* This component does not render UI.
*/
const EncryptionServiceProvider = () => {
initEncryptionService(CONFIG.secretKey); //your encryption key
return null;
};
export default EncryptionServiceProvider;
π What Does This Component Do?
- Calls initEncryptionService() once
- Registers the encryption secret key
- Makes encryption available globally
- Does not render any UI (logic-only component)
- Must run on the client side ("use client")
β οΈ Important
- The encryption key is set here
- Without this component, encryption will not work
- The same key is required for both encryption and decryption
ποΈ Step 2: Call the Provider in layout.tsx
Add the provider to the Root Layout so encryption is available throughout the app.
π File: src/app/layout.tsx
tsx
import EncryptionServiceProvider from "@/providers/EncryptionServiceProvider";
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
{children}
{/* Initialize encryption globally */}
<EncryptionServiceProvider />
</body>
</html>
);
}
β Why Use layout.tsx?
layout.tsxruns only once- Ensures encryption is ready before any component uses it
- Prevents multiple initializations
- Makes encryption available app-wide
π Step 3: Use Encryption Anywhere in the App
Once initialized, encryption can be used in any component, hook, or service.
π₯ Import the Encryption Service
ts
import { encryptionService } from "universe-code/react";
π Available Functions
1οΈβ£ Encrypt Data (High Security)
Use this for user data, credentials, sensitive information.
ts
const data = { userId: 123, email: 'user@example.com' };
const encrypted = await encryptionService.encryption(data);
// Returns: { iv: "...", payload: "..." }
β Uses 100,000 iterations (more secure)
2οΈβ£ Decrypt Data (Matching encryption())
ts
const encrypted = { iv: "...", payload: "..." };
const decrypted = await encryptionService.decryption(encrypted);
// Returns: { userId: 123, email: 'user@example.com' }
β οΈ Rule:
encryption() β decryption()
3οΈβ£ Encrypt Data (Fast Mode)
Best for API responses or less sensitive data.
ts
const data = { status: 'success', token: 'xyz789' };
const encrypted = await encryptionService.encryption1(data);
// Returns: { iv: "...", payload: "..." }
β Uses 50,000 iterations (faster)
4οΈβ£ Decrypt Data (Matching encryption1())
ts
const encrypted = { iv: "...", payload: "..." };
const decrypted = await encryptionService.decryption1(encrypted);
// Returns: { status: 'success', token: 'xyz789' }
β οΈ Rule:
encryption1() β decryption1()
5οΈβ£ Decrypt API Response (iv###payload format)
Use this when API returns data in "iv###payload" format.
ts
const apiResponse = "dGVzdGl2###ZW5jcnlwdGVkUGF5bG9hZA==";
const decrypted = await encryptionService.decrptApiResponse(apiResponse);
// Returns: decrypted object or null if format is invalid
π Returns
- β Decrypted object (if format is valid)
- β null (if format is invalid)
π§ Which Method Should I Use?
| Method | Use Case |
|---|---|
encryption() | General data encryption (user data, sensitive info) |
decryption() | Decrypt data encrypted with encryption() |
encryption1() | API responses, faster encryption |
decryption1() | Decrypt data encrypted with encryption1() |
decrptApiResponse() | Decrypt API responses in "iv###payload" format |
β Key Points (Must Read)
encryption()uses 100,000 iterations (more secure)encryption1()uses 50,000 iterations (faster)- Always use matching encrypt/decrypt pair
decrptApiResponse()only works with"iv###payload"format- Requires async/await or Promise handling