Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

encryptionpackage

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

encryptionpackage - npm Package Compare versions

Comparing version 1.0.6 to 1.0.7

generate_key.js

174

encryption.js

@@ -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

@@ -54,77 +130,23 @@ // const decrypted = await crypto.subtle.decrypt(

// encrypt.js
import { publicEncrypt } from 'crypto';
// 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
/**
* Encrypts the given message using RSA-OAEP.
* @param {Object} data - The data to encrypt (should include uuid and token).
* @returns {string} - The encrypted message as a Base64 string.
*/
export function encryptData(data, publicKey) {
// Encrypt the message using RSA-OAEP
const encryptedMessage = publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: 'sha256', // Hash algorithm used in OAEP
},
Buffer.from(JSON.stringify(data))
);
}
// 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);
// Convert to base64 for easier transport
return encryptedMessage.toString('base64');
}
// 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
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)
);
// Convert to Base64 string for easy storage/transmission
const encryptedBase64 = uint8ArrayToBase64(new Uint8Array(encrypted));
const ivBase64 = uint8ArrayToBase64(iv); // Convert IV to Base64
return {
iv: ivBase64, // Store IV as a Base64 string
content: encryptedBase64 // Store encrypted content as a Base64 string
};
}
// Decryption using Web Crypto API
export async function decrypt(encryptedData, key) {
const decoder = new TextDecoder();
const iv = base64ToUint8Array(encryptedData.iv); // Convert Base64 IV back to Uint8Array
const encryptedBytes = base64ToUint8Array(encryptedData.content); // Convert Base64 content back to Uint8Array
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;
}
{
"name": "encryptionpackage",
"version": "1.0.6",
"version": "1.0.7",
"main": "encryption.js",

@@ -5,0 +5,0 @@ "module": "encryption.js",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc