Skip to content

Commit 2c0ebba

Browse files
committed
Fix issue with gcm
1 parent ee91930 commit 2c0ebba

5 files changed

Lines changed: 38 additions & 40 deletions

File tree

OpenFlow/src/Crypt.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,28 @@ export class Crypt {
4444
}
4545
static encrypt(text: string): string {
4646
let iv: Buffer = crypto.randomBytes(Crypt.iv_length);
47-
let cipher: crypto.Cipher = crypto.createCipheriv("AES-256-GCM", Buffer.from(Crypt.encryption_key), iv);
47+
let cipher: crypto.CipherGCM = crypto.createCipheriv('aes-256-gcm', Buffer.from(Crypt.encryption_key), iv);
4848
let encrypted: Buffer = cipher.update((text as any));
4949
encrypted = Buffer.concat([encrypted, cipher.final()]);
50-
return iv.toString("hex") + ":" + encrypted.toString("hex");
50+
const authTag = cipher.getAuthTag()
51+
return iv.toString("hex") + ":" + encrypted.toString("hex") + ":" + authTag.toString("hex");
5152
}
5253
static decrypt(text: string): string {
5354
let textParts: string[] = text.split(":");
5455
let iv: Buffer = Buffer.from(textParts.shift(), "hex");
55-
let encryptedText: Buffer = Buffer.from(textParts.join(":"), "hex");
56+
let encryptedText: Buffer = Buffer.from(textParts.shift(), "hex");
57+
let authTag: Buffer = null;
58+
if (textParts.length > 0) authTag = Buffer.from(textParts.shift(), "hex");
5659
let decrypted: Buffer
57-
try {
58-
let decipher: crypto.Decipher = crypto.createDecipheriv("AES-256-GCM", Buffer.from(this.encryption_key), iv);
59-
decrypted = decipher.update(encryptedText);
60-
decrypted = Buffer.concat([decrypted, decipher.final()]);
61-
} catch {
62-
let decipher: crypto.Decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(this.encryption_key), iv);
60+
if (authTag != null) {
61+
let decipher: crypto.DecipherGCM = crypto.createDecipheriv('aes-256-gcm', Buffer.from(Crypt.encryption_key), iv);
62+
decipher.setAuthTag(authTag);
6363
decrypted = decipher.update(encryptedText);
6464
decrypted = Buffer.concat([decrypted, decipher.final()]);
65+
} else {
66+
let decipher2: crypto.Decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(this.encryption_key), iv);
67+
decrypted = decipher2.update(encryptedText);
68+
decrypted = Buffer.concat([decrypted, decipher2.final()]);
6569
}
6670
return decrypted.toString();
6771
}

OpenFlowNodeRED/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openiap/nodered",
3-
"version": "1.3.14",
3+
"version": "1.3.16",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

OpenFlowNodeRED/src/node-red-contrib-openflow-storage.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,31 @@ export class noderedcontribopenflowstorage {
6969
private static iv_length: number = 16; // for AES, this is always 16
7070
private static encryption_key: string = ("smurfkicks-to-anyone-hating-on-nodejs").substr(0, 32);
7171
static encrypt(text: string): string {
72-
try {
73-
let iv: Buffer = crypto.randomBytes(this.iv_length);
74-
let cipher: crypto.Cipher = crypto.createCipheriv("AES-256-GCM", Buffer.from(this.encryption_key), iv);
75-
let encrypted: Buffer = cipher.update((text as any));
76-
encrypted = Buffer.concat([encrypted, cipher.final()]);
77-
return iv.toString("hex") + ":" + encrypted.toString("hex");
78-
} catch (error) {
79-
console.error(error);
80-
}
81-
return text;
72+
let iv: Buffer = crypto.randomBytes(this.iv_length);
73+
let cipher: crypto.CipherGCM = crypto.createCipheriv('aes-256-gcm', Buffer.from(this.encryption_key), iv);
74+
let encrypted: Buffer = cipher.update((text as any));
75+
encrypted = Buffer.concat([encrypted, cipher.final()]);
76+
const authTag = cipher.getAuthTag()
77+
return iv.toString("hex") + ":" + encrypted.toString("hex") + ":" + authTag.toString("hex");
8278
}
8379
static decrypt(text: string): string {
84-
try {
85-
let textParts: string[] = text.split(":");
86-
let iv: Buffer = Buffer.from(textParts.shift(), "hex");
87-
let encryptedText: Buffer = Buffer.from(textParts.join(":"), "hex");
88-
let decrypted: Buffer
89-
try {
90-
let decipher: crypto.Decipher = crypto.createDecipheriv("AES-256-GCM", Buffer.from(this.encryption_key), iv);
91-
decrypted = decipher.update(encryptedText);
92-
decrypted = Buffer.concat([decrypted, decipher.final()]);
93-
} catch {
94-
let decipher: crypto.Decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(this.encryption_key), iv);
95-
decrypted = decipher.update(encryptedText);
96-
decrypted = Buffer.concat([decrypted, decipher.final()]);
97-
}
98-
return decrypted.toString();
99-
} catch (error) {
100-
console.error(error);
80+
let textParts: string[] = text.split(":");
81+
let iv: Buffer = Buffer.from(textParts.shift(), "hex");
82+
let encryptedText: Buffer = Buffer.from(textParts.shift(), "hex");
83+
let authTag: Buffer = null;
84+
if (textParts.length > 0) authTag = Buffer.from(textParts.shift(), "hex");
85+
let decrypted: Buffer
86+
if (authTag != null) {
87+
let decipher: crypto.DecipherGCM = crypto.createDecipheriv('aes-256-gcm', Buffer.from(this.encryption_key), iv);
88+
decipher.setAuthTag(authTag);
89+
decrypted = decipher.update(encryptedText);
90+
decrypted = Buffer.concat([decrypted, decipher.final()]);
91+
} else {
92+
let decipher2: crypto.Decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(this.encryption_key), iv);
93+
decrypted = decipher2.update(encryptedText);
94+
decrypted = Buffer.concat([decrypted, decipher2.final()]);
10195
}
102-
return text;
96+
return decrypted.toString();
10397
}
10498

10599

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.3.14
1+
1.3.16

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openiap/openflow",
3-
"version": "1.3.14",
3+
"version": "1.3.16",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)