-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#172078979] Add encrypt/decrypt AES/RSA utility #319
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm apart minor changes
src/__tests__/encrypt.test.ts
Outdated
describe("encrypt", () => { | ||
it("should encrypt and decrypt a string with combination AES/RSA", async () => { | ||
const encryptedPayload = toEncryptedPayload(rsaPublicKey, aTextToEncrypt); | ||
expect(encryptedPayload).toHaveProperty("encryptedOutput"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(encryptedPayload).toHaveProperty("encryptedOutput"); | |
expect(encryptedPayload).toHaveProperty("encryptedPayload"); |
src/encrypt.ts
Outdated
} as EncryptTuple; | ||
} | ||
|
||
export function hybridDecrypt( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I use ecrypt or decrypt function with wrong keys pair, I will get an exception.
For example this code raise an exception:
const encryptedPayload = toEncryptedPayload(rsaPublicKey, aTextToEncrypt);
expect(encryptedPayload).toHaveProperty("encryptedPayload");
const decryptedString = fromEncryptedPayload(
rsaPrivateKey + "x", //wrong private key
encryptedPayload
);
Could this implementation be more error friendly?
export function fromEncryptedPayload(
rsaPrivateKey: string,
encryptedPayload: EncryptedPayload
): Either<Error, string> {
return tryCatch(
() => {
const iv = Buffer.from(encryptedPayload.iv, "base64");
const aesKey = crypto.privateDecrypt(
rsaPrivateKey,
Buffer.from(encryptedPayload.encryptedKey, "base64")
);
const decipher = crypto.createDecipheriv(
"aes-128-cbc",
Buffer.from(aesKey),
iv
);
const decrypted = decipher.update(
Buffer.from(encryptedPayload.encryptedPayload, "base64")
);
const output = Buffer.concat([decrypted, decipher.final()]);
return output.toString("utf-8");
},
(e: unknown) => (e instanceof Error ? e : new Error(String(e)))
);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a very good point, probably yes give me a moment :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Added utility functions to provide hybrid AES/RSA encrypt/decrypt.