Skip to content

Commit

Permalink
Merge pull request #22 from AnandBaburajan/email
Browse files Browse the repository at this point in the history
Fix encrypted email
  • Loading branch information
anandbaburajan authored Jan 9, 2021
2 parents fb2b37b + b0caaf9 commit 5c2fc44
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .env.local.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
NEXT_PUBLIC_ENCRYPTION_KEY=your_32_bit_key_here
NEXT_PUBLIC_ENCRYPTION_KEY=your key of length 32 characters here
NEXT_PUBLIC_ENCRYPTION_IV=your key of length 16 characters here
2 changes: 1 addition & 1 deletion pages/poll/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Poll = (props: {
pollid: string;
}): JSX.Element => {
const { pollFromDB, pollid } = props;
const pollCreatorEmailID = decrypt(pollFromDB.emailID);
const pollCreatorEmailID = decrypt(pollFromDB.encryptedEmailID);
const loggedInUserEmailID = useSelector(
(state: RootState) => state.authReducer.username
);
Expand Down
2 changes: 1 addition & 1 deletion pages/poll/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const Create = (): JSX.Element => {
const poll: RocketMeetPoll = {
title: pollTitle,
description: pollDescription,
emailID: encryptedEmailID,
encryptedEmailID,
choices: pollChoices,
};
const payload = JSON.stringify(poll);
Expand Down
3 changes: 1 addition & 2 deletions src/components/PollInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ dayjs.extend(localizedFormat);

const PollInfo = (props: { poll: RocketMeetPollFromDB }): JSX.Element => {
const { poll } = props;

return (
<div className="justify-content-center">
<h1>{poll.title}</h1>
{poll.description}
<Alert variant={poll.open ? "success" : "secondary"}>
{poll.open ? "OPEN" : "CLOSED"}
</Alert>
By {decrypt(poll.emailID)} | Created on{" "}
By {decrypt(poll.encryptedEmailID)} | Created on{" "}
{dayjs(poll.createdAt).format("DD/MM/YYYY")}
</div>
);
Expand Down
14 changes: 5 additions & 9 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,25 @@ export const isChoicePresentInPollChoices = (
};

const ENCRYPTION_KEY = process.env.NEXT_PUBLIC_ENCRYPTION_KEY || "";
const ENCRYPTION_IV = process.env.NEXT_PUBLIC_ENCRYPTION_IV || "";

export const encrypt = (text: string): string => {
const iv = crypto.randomBytes(16);
let cipher = crypto.createCipheriv(
"aes-256-cbc",
Buffer.from(ENCRYPTION_KEY),
iv
ENCRYPTION_IV
);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return JSON.stringify({
iv: iv.toString("hex"),
encryptedText: encrypted.toString("hex"),
});
return encrypted.toString("hex");
};

export const decrypt = (text: string): string => {
const iv = Buffer.from(JSON.parse(text).iv, "hex");
const encryptedText = Buffer.from(JSON.parse(text).encryptedText, "hex");
const encryptedText = Buffer.from(text, "hex");
const decipher = crypto.createDecipheriv(
"aes-256-cbc",
Buffer.from(ENCRYPTION_KEY),
iv
ENCRYPTION_IV
);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
Expand Down
8 changes: 4 additions & 4 deletions src/models/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface RocketMeetPoll {
title: string;
description?: string;
open?: boolean;
emailID: string; // encrypted email ID
encryptedEmailID: string;
choices: Choice[];
finalChoice?: Choice;
votes?: Vote[];
Expand All @@ -35,7 +35,7 @@ export interface RocketMeetPollFromDB {
title: string;
description?: string;
open?: boolean;
emailID: string; // encrypted email ID
encryptedEmailID: string;
choices: ChoiceFromDB[];
finalChoice?: ChoiceFromDB;
votes?: VoteFromDB[];
Expand All @@ -46,6 +46,6 @@ export interface RocketMeetPollFromDB {

export interface MailerArgs {
pollid: string;
receiverIDs: string[],
senderID: string
receiverIDs: string[];
senderID: string;
}

0 comments on commit 5c2fc44

Please sign in to comment.