encryptionpackage
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -0,1 +1,52 @@ | ||
// // encryption.js | ||
// // 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 | ||
// ); | ||
// } | ||
// // Encryption using Web Crypto API | ||
// export async function encrypt(text, key) { | ||
// const encoder = new TextEncoder(); | ||
// const iv = crypto.getRandomValues(new Uint8Array(12)); // Generate random IV (12 bytes for GCM) | ||
// // Encrypt the text | ||
// const encrypted = await crypto.subtle.encrypt( | ||
// { name: 'AES-GCM', iv }, | ||
// key, | ||
// encoder.encode(text) | ||
// ); | ||
// return { | ||
// iv: Array.from(iv), // Convert to array for easy storage | ||
// content: Array.from(new Uint8Array(encrypted)) // Convert to array | ||
// }; | ||
// } | ||
// // Decryption using Web Crypto API | ||
// export async function decrypt(encryptedData, key) { | ||
// const decoder = new TextDecoder(); | ||
// const iv = new Uint8Array(encryptedData.iv); | ||
// const encryptedBytes = new Uint8Array(encryptedData.content); | ||
// const decrypted = await crypto.subtle.decrypt( | ||
// { name: 'AES-GCM', iv }, | ||
// key, | ||
// encryptedBytes | ||
// ); | ||
// return decoder.decode(decrypted); | ||
// } | ||
// // Function to create a new key and return it | ||
// export async function createNewKey() { | ||
// const key = await generateKey(); | ||
// return key; | ||
// } | ||
// encryption.js | ||
@@ -12,2 +63,23 @@ | ||
// Function to convert a Uint8Array to a Base64 string | ||
function uint8ArrayToBase64(uint8Array) { | ||
let binary = ''; | ||
const len = uint8Array.byteLength; | ||
for (let i = 0; i < len; i++) { | ||
binary += String.fromCharCode(uint8Array[i]); | ||
} | ||
return window.btoa(binary); | ||
} | ||
// Function to convert a Base64 string back to a Uint8Array | ||
function base64ToUint8Array(base64) { | ||
const binary_string = window.atob(base64); | ||
const len = binary_string.length; | ||
const bytes = new Uint8Array(len); | ||
for (let i = 0; i < len; i++) { | ||
bytes[i] = binary_string.charCodeAt(i); | ||
} | ||
return bytes; | ||
} | ||
// Encryption using Web Crypto API | ||
@@ -25,5 +97,9 @@ export async function encrypt(text, key) { | ||
// Convert to Base64 string for easy storage/transmission | ||
const encryptedBase64 = uint8ArrayToBase64(new Uint8Array(encrypted)); | ||
const ivBase64 = uint8ArrayToBase64(iv); // Convert IV to Base64 | ||
return { | ||
iv: Array.from(iv), // Convert to array for easy storage | ||
content: Array.from(new Uint8Array(encrypted)) // Convert to array | ||
iv: ivBase64, // Store IV as a Base64 string | ||
content: encryptedBase64 // Store encrypted content as a Base64 string | ||
}; | ||
@@ -35,4 +111,4 @@ } | ||
const decoder = new TextDecoder(); | ||
const iv = new Uint8Array(encryptedData.iv); | ||
const encryptedBytes = new Uint8Array(encryptedData.content); | ||
const iv = base64ToUint8Array(encryptedData.iv); // Convert Base64 IV back to Uint8Array | ||
const encryptedBytes = base64ToUint8Array(encryptedData.content); // Convert Base64 content back to Uint8Array | ||
@@ -53,1 +129,2 @@ const decrypted = await crypto.subtle.decrypt( | ||
} | ||
{ | ||
"name": "encryptionpackage", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"main": "encryption.js", | ||
@@ -5,0 +5,0 @@ "module": "encryption.js", |
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
5419
111