Skip to content

Commit

Permalink
Merge pull request #34 from shubhvjain/main
Browse files Browse the repository at this point in the history
new version compatible with json inputs for major methods
shubhvjain authored Jan 4, 2025
2 parents 07ef10d + f706bf8 commit 4b5f624
Showing 7 changed files with 751 additions and 1,121 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beanbagdb",
"version": "0.5.80",
"version": "0.6.0",
"description": "A JS library to introduce a schema layer to a No-SQL local database",
"main": "src/index.js",
"module": "src/index.js",
325 changes: 210 additions & 115 deletions src/index.js

Large diffs are not rendered by default.

191 changes: 0 additions & 191 deletions src/plugins/text_command.js

This file was deleted.

360 changes: 207 additions & 153 deletions src/system_schema.js

Large diffs are not rendered by default.

851 changes: 264 additions & 587 deletions test/operations.test.js

Large diffs are not rendered by default.

52 changes: 0 additions & 52 deletions test/plugin.test.js

This file was deleted.

91 changes: 69 additions & 22 deletions test/pouchdb.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import Ajv from 'ajv';
import PouchDB from 'pouchdb';
import pouchdbFind from 'pouchdb-find';
PouchDB.plugin(pouchdbFind)
import { scryptSync, randomBytes, createCipheriv, createDecipheriv } from 'crypto';
import crypto from 'crypto'


export const get_pdb_doc = (dbname,secret)=>{
@@ -42,28 +42,75 @@ const pdb = new PouchDB(dbname);
},
utils: {
encrypt: async (text, encryptionKey) => {
//console.log(encryptionKey)
const key = scryptSync(encryptionKey, "salt", 32); // Derive a 256-bit key
const iv = randomBytes(16); // Initialization vector
const cipher = createCipheriv("aes-256-cbc", key, iv);
let encrypted = cipher.update(text, "utf8", "hex");
encrypted += cipher.final("hex");
//console.log("IV:", iv.toString("hex"));
//console.log("Encrypted:", encrypted);
return iv.toString("hex") + ":" + encrypted; // Prepend the IV for decryption
const encoder = new TextEncoder();
const data = encoder.encode(text); // Encode the text into bytes

// Ensure the encryption key is of valid length (16, 24, or 32 bytes for AES-GCM)
const keyBytes = encoder.encode(encryptionKey);
if (
keyBytes.length !== 16 &&
keyBytes.length !== 24 &&
keyBytes.length !== 32
) {
throw new Error("Encryption key must be 16, 24, or 32 bytes long.");
}

// Convert encryptionKey to CryptoKey
const key = await crypto.subtle.importKey(
"raw",
keyBytes,
{ name: "AES-GCM" },
false,
["encrypt"]
);

// Create a random initialization vector (IV)
const iv = crypto.getRandomValues(new Uint8Array(12)); // 12 bytes for AES-GCM

// Encrypt the data
const encrypted = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv: iv },
key,
data
);

// Convert encrypted data and IV to base64 for storage
const encryptedArray = new Uint8Array(encrypted);
const encryptedText = btoa(String.fromCharCode(...encryptedArray));
const ivText = btoa(String.fromCharCode(...iv));

return ivText + ":" + encryptedText; // Store IV and encrypted text together
},
decrypt: async (encryptedText, encryptionKey) => {
//console.log(encryptedText, encryptionKey)
const key = scryptSync(encryptionKey, "salt", 32); // Derive a 256-bit key
const [ivHex, encryptedHex] = encryptedText.split(":");
const iv = Buffer.from(ivHex, "hex");
const encrypted = Buffer.from(encryptedHex, "hex");
//console.log("IV:", iv.toString("hex"));
//console.log("Encrypted:", encrypted.toString("hex"));
const decipher = createDecipheriv("aes-256-cbc", key, iv);
let decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
decrypt: async (encryptedText, encryptionKey) => {
const [ivText, encryptedData] = encryptedText.split(":");

// Convert IV and encrypted data from base64 to byte arrays
const iv = Uint8Array.from(atob(ivText), (c) => c.charCodeAt(0));
const encryptedArray = Uint8Array.from(atob(encryptedData), (c) =>
c.charCodeAt(0)
);

const encoder = new TextEncoder();

// Convert encryptionKey to CryptoKey
const key = await crypto.subtle.importKey(
"raw",
encoder.encode(encryptionKey),
{ name: "AES-GCM" },
false,
["decrypt"]
);

// Decrypt the data
const decrypted = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv: iv },
key,
encryptedArray
);

// Convert decrypted data back to a string
const decoder = new TextDecoder();
return decoder.decode(decrypted);
},
ping: () => {
// @TODO ping the database to check connectivity when class is ready to use

0 comments on commit 4b5f624

Please sign in to comment.