encryption-service
Framework: angular
📦 Installation / Import
ts
import { EncryptionService } from 'universe-code/angular';
⚙️ Step 1: Create Encryption Service
Create file: src/app/services/encryption.service.ts
ts
import { CONFIG } from 'config';
import { EncryptionService } from 'universe-code/angular';
export const encryptionService = new EncryptionService(
CONFIG.encryptionKey // your encryption key
);
📥 Step 2: Use Encryption Service in Any Component / Service
ts
import { encryptionService } from 'src/app/services/encryption.service';
🔑 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