Skip to content

Commit

Permalink
increase flexability of decryptData
Browse files Browse the repository at this point in the history
  • Loading branch information
ipbyrne committed Jun 7, 2023
1 parent ce39590 commit f73497e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Isaac Byrne (me@ipbyrne.com)",
"module": "dist/index.esm.js",
"license": "Apache-2.0",
"version": "0.0.5",
"version": "0.0.6",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"keywords": [
Expand Down
4 changes: 1 addition & 3 deletions src/cipher/cipher.sanity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { generate, encrypt, decrypt } from "./cipher";
describe("X25519", () => {
it("encrypt and decrypt", async () => {
const { privateKeyJwk } = await generate();
const message = {
message: "It’s a dangerous business, Frodo, going out your door.",
};
const message = "It’s a dangerous business, Frodo, going out your door.";
const jwe = encrypt(message, privateKeyJwk as PrivateKeyJwk);
const jwe2 = encrypt(message, privateKeyJwk as PrivateKeyJwk);
expect(jwe).toStrictEqual(jwe2);
Expand Down
32 changes: 20 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createEncryptedObject,
createDecryptedObject,
} from "./utils";
import { generate } from "./cipher/cipher";
import { decrypt, generate } from "./cipher/cipher";
import { PrivateKeyJwk, Data } from "./types";
import * as Types from "./types";

Expand All @@ -17,18 +17,26 @@ export const encryptData = (data: Data, privateKeyJwk: PrivateKeyJwk) => {
return encryptedData;
};

export const decryptData = (data: Data, privateKeyJwk: PrivateKeyJwk) => {
let id;
if (data._id) {
id = data._id;
delete data._id;
export const decryptData = (data: any, privateKeyJwk: PrivateKeyJwk): any => {
const type = typeof data;
if (Array.isArray(data)) {
return data.map((d: any) => decryptData(d, privateKeyJwk));
}
const decryptedData = createDecryptedObject(
data,
privateKeyJwk as PrivateKeyJwk
);
decryptedData._id = id;
return decryptedData;

if (type === "object") {
let id;
if (data._id) {
id = data._id;
delete data._id;
}
const decryptedData = createDecryptedObject(
data,
privateKeyJwk as PrivateKeyJwk
);
decryptedData._id = id;
return decryptedData;
}
return decrypt(data, privateKeyJwk as PrivateKeyJwk);
};

export default {
Expand Down

0 comments on commit f73497e

Please sign in to comment.