Skip to content

Commit bf11258

Browse files
committed
changed async processing of files to synchronous
1 parent 17d934f commit bf11258

3 files changed

Lines changed: 55 additions & 69 deletions

File tree

codeanalysis/lcov.info

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,9 @@ end_of_record
2828
TN:
2929
SF:C:\Users\Peter\Documents\Bachelor\javascript-forge-cryptoexamples\src\allinone\ExampleFileEncryption.js
3030
FN:29,(anonymous_0)
31-
FN:49,(anonymous_1)
32-
FN:65,(anonymous_2)
33-
FN:80,(anonymous_3)
34-
FNF:4
35-
FNH:4
31+
FNF:1
32+
FNH:1
3633
FNDA:2,(anonymous_0)
37-
FNDA:2,(anonymous_1)
38-
FNDA:2,(anonymous_2)
39-
FNDA:2,(anonymous_3)
4034
DA:12,1
4135
DA:13,1
4236
DA:14,1
@@ -49,35 +43,34 @@ DA:35,2
4943
DA:40,2
5044
DA:41,2
5145
DA:44,2
46+
DA:47,2
47+
DA:48,2
5248
DA:49,2
53-
DA:50,2
49+
DA:51,2
50+
DA:52,2
5451
DA:53,2
5552
DA:54,2
56-
DA:56,2
53+
DA:55,2
5754
DA:57,2
58-
DA:58,2
59-
DA:59,2
6055
DA:60,2
6156
DA:62,2
62-
DA:65,2
63-
DA:67,2
57+
DA:63,2
6458
DA:68,2
59+
DA:69,2
60+
DA:70,2
61+
DA:71,2
6562
DA:73,2
66-
DA:74,2
6763
DA:75,2
6864
DA:76,2
69-
DA:78,2
70-
DA:80,2
71-
DA:81,2
72-
DA:89,0
73-
DA:93,1
74-
DA:96,1
75-
LF:35
76-
LH:34
65+
DA:81,0
66+
DA:85,1
67+
DA:88,1
68+
LF:34
69+
LH:33
7770
BRDA:34,0,0,2
7871
BRDA:34,0,1,0
79-
BRDA:83,1,0,2
80-
BRDA:83,1,1,0
72+
BRDA:78,1,0,2
73+
BRDA:78,1,1,0
8174
BRF:4
8275
BRH:2
8376
end_of_record

src/allinone/ExampleFileEncryption.js

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,48 +43,40 @@ const demonstrateFileEncryption = () => {
4343
//create random initialization vector
4444
let iv = forge.random.getBytesSync(16);
4545

46-
// read file and encrypt/decrypt with an ansynchronous control flow
47-
let input;
48-
let encrypted;
49-
fs.readFile("file.txt", (error, data) => {
50-
input = data;
46+
// ENCRYPT the file
47+
let input = fs.readFileSync("file.txt");
48+
let cipher = forge.cipher.createCipher("AES-GCM", key);
49+
cipher.start({ iv: iv });
50+
// node buffer and forge buffer differ, so the node buffer must be converted to a forge Buffer
51+
cipher.update(forge.util.createBuffer(input.toString("binary")));
52+
cipher.finish();
53+
let tag = cipher.mode.tag;
54+
encrypted = forge.util.createBuffer();
55+
encrypted.putBuffer(cipher.output);
56+
// node buffer and forge buffer differ, so the forge buffer must be converted to a node Buffer
57+
encrypted = Buffer.from(encrypted.getBytes(), "binary");
5158

52-
// ENCRYPT the file
53-
let cipher = forge.cipher.createCipher("AES-GCM", key);
54-
cipher.start({ iv: iv });
55-
// node buffer and forge buffer differ, so the node buffer must be converted to a forge Buffer
56-
cipher.update(forge.util.createBuffer(data.toString("binary")));
57-
cipher.finish();
58-
let tag = cipher.mode.tag;
59-
encrypted = forge.util.createBuffer();
60-
encrypted.putBuffer(cipher.output);
61-
// node buffer and forge buffer differ, so the forge buffer must be converted to a node Buffer
62-
encrypted = Buffer.from(encrypted.getBytes(), "binary");
63-
64-
// write encrypted file
65-
fs.writeFile("file.enc.txt", encrypted, error => {
66-
// DECRYPT the file
67-
let decipher = forge.cipher.createDecipher("AES-GCM", key);
68-
decipher.start({
69-
iv: iv,
70-
tag: tag
71-
});
72-
// node buffer and forge buffer differ, so the node buffer must be converted to a forge Buffer
73-
decipher.update(forge.util.createBuffer(encrypted.toString("binary")));
74-
decipher.finish();
75-
let decrypted = forge.util.createBuffer();
76-
decrypted.putBuffer(decipher.output);
77-
// node buffer and forge buffer differ, so the forge buffer must be converted to a node Buffer
78-
decrypted = Buffer.from(decrypted.getBytes(), "binary");
79-
80-
fs.writeFile("file.dec.txt", decrypted, error => {
81-
logger.info(
82-
"Decrypted file content and original file content are the same: %s",
83-
Buffer.compare(input, decrypted) === 0 ? "yes" : "no"
84-
);
85-
});
86-
});
59+
// write encrypted file
60+
fs.writeFileSync("file.enc.txt", encrypted);
61+
// DECRYPT the file
62+
let decipher = forge.cipher.createDecipher("AES-GCM", key);
63+
decipher.start({
64+
iv: iv,
65+
tag: tag
8766
});
67+
// node buffer and forge buffer differ, so the node buffer must be converted to a forge Buffer
68+
decipher.update(forge.util.createBuffer(encrypted.toString("binary")));
69+
decipher.finish();
70+
let decrypted = forge.util.createBuffer();
71+
decrypted.putBuffer(decipher.output);
72+
// node buffer and forge buffer differ, so the forge buffer must be converted to a node Buffer
73+
decrypted = Buffer.from(decrypted.getBytes(), "binary");
74+
75+
fs.writeFileSync("file.dec.txt", decrypted);
76+
logger.info(
77+
"Decrypted file content and original file content are the same: %s",
78+
Buffer.compare(input, decrypted) === 0 ? "yes" : "no"
79+
);
8880
} catch (error) {
8981
logger.error(error.message);
9082
}

test/allinone/ExampleFileEncryption.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ chai.use(sinonChai);
1010
describe("ExampleFileEncryption allInOne crypto Test runs", function() {
1111
beforeEach(function() {
1212
sinon.spy(testee.logger, "error");
13+
sinon.spy(testee.logger, "info");
1314
});
1415

1516
afterEach(function() {
1617
testee.logger.error.restore();
18+
testee.logger.info.restore();
1719
});
1820

19-
it("logger output should confirm that files are the same", function() {
20-
testee.demonstrateFileEncryption();
21-
chai.expect(testee.logger.error).to.not.be.called;
22-
// chai.assert.include(logger.info.getCall(0).args, "yes");
21+
it("logger output should confirm that files are the same", async function() {
22+
await testee.demonstrateFileEncryption();
23+
chai.assert.include(testee.logger.info.getCall(0).args, "yes");
2324
});
2425
});

0 commit comments

Comments
 (0)