encryptionpackage
Advanced tools
Comparing version 1.0.4 to 1.0.5
// encryption.js | ||
// Helper function to derive a key from a passphrase | ||
async function deriveKey(passphrase) { | ||
const encoder = new TextEncoder(); | ||
const keyMaterial = await crypto.subtle.importKey( | ||
'raw', | ||
encoder.encode(passphrase), | ||
{ name: 'PBKDF2' }, | ||
false, | ||
['deriveBits'] | ||
// Helper function to generate a random key | ||
async function generateKey() { | ||
return await crypto.subtle.generateKey( | ||
{ name: 'AES-GCM', length: 256 }, // Use AES-GCM with a 256-bit key | ||
true, // Extractable | ||
['encrypt', 'decrypt'] // Usages for the key | ||
); | ||
// Derive a 256-bit AES key with appropriate usages | ||
const key = await crypto.subtle.deriveKey( | ||
{ | ||
name: 'PBKDF2', | ||
salt: crypto.getRandomValues(new Uint8Array(16)), // Random salt for security | ||
iterations: 100000, | ||
hash: 'SHA-256' | ||
}, | ||
keyMaterial, | ||
{ name: 'AES-CBC', length: 256 }, // Specify the key type and length | ||
true, // The key is extractable | ||
['encrypt', 'decrypt'] // Usages for the derived key | ||
); | ||
return key; | ||
} | ||
// Encryption using Web Crypto API | ||
export async function encrypt(text, passphrase) { | ||
export async function encrypt(text, key) { | ||
const encoder = new TextEncoder(); | ||
const cryptoKey = await deriveKey(passphrase); // Derive key from passphrase | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); // Generate random IV (12 bytes for GCM) | ||
const iv = crypto.getRandomValues(new Uint8Array(16)); // Generate random IV | ||
// Encrypt the text | ||
const encrypted = await crypto.subtle.encrypt( | ||
{ name: 'AES-CBC', iv }, | ||
cryptoKey, | ||
{ name: 'AES-GCM', iv }, | ||
key, | ||
encoder.encode(text) | ||
@@ -52,6 +31,4 @@ ); | ||
// Decryption using Web Crypto API | ||
export async function decrypt(encryptedData, passphrase) { | ||
export async function decrypt(encryptedData, key) { | ||
const decoder = new TextDecoder(); | ||
const cryptoKey = await deriveKey(passphrase); // Derive key from passphrase | ||
const iv = new Uint8Array(encryptedData.iv); | ||
@@ -61,4 +38,4 @@ const encryptedBytes = new Uint8Array(encryptedData.content); | ||
const decrypted = await crypto.subtle.decrypt( | ||
{ name: 'AES-CBC', iv }, | ||
cryptoKey, | ||
{ name: 'AES-GCM', iv }, | ||
key, | ||
encryptedBytes | ||
@@ -69,1 +46,7 @@ ); | ||
} | ||
// Function to create a new key and return it | ||
export async function createNewKey() { | ||
const key = await generateKey(); | ||
return key; | ||
} |
{ | ||
"name": "encryptionpackage", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"main": "encryption.js", | ||
@@ -5,0 +5,0 @@ "module": "encryption.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2908
48