diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9eb6d8d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +*.png filter=lfs diff=lfs merge=lfs -text +*.jpg filter=lfs diff=lfs merge=lfs -text +*.jpeg filter=lfs diff=lfs merge=lfs -text +*.pdf filter=lfs diff=lfs merge=lfs -text +*.enc filter=lfs diff=lfs merge=lfs -text diff --git a/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.LICENSE.txt b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.LICENSE.txt new file mode 100644 index 0000000..b6d34e7 --- /dev/null +++ b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.LICENSE.txt @@ -0,0 +1,3 @@ +The file 1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg is downloaded from Wikimedia Commons: https://commons.wikimedia.org/wiki/File:Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg . + +Quoted from the web address: ... This photographic reproduction is therefore also considered to be in the public domain in the United States. In other jurisdictions, re-use of this content may be restricted ... diff --git a/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg new file mode 100644 index 0000000..87baf8b --- /dev/null +++ b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b332c298be98be9968715952822431a051f9d07e67460cdcf91a5a3a305b5df1 +size 1141550 diff --git a/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc new file mode 100644 index 0000000..a28a5b7 --- /dev/null +++ b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cf67abd791f1e230bce57dd47150f262424247cbefb8be16efcd57d13955576a +size 1141568 diff --git a/tests/static_assets/mona_lisa/openssl_command.sh b/tests/static_assets/mona_lisa/openssl_command.sh new file mode 100644 index 0000000..4e0df02 --- /dev/null +++ b/tests/static_assets/mona_lisa/openssl_command.sh @@ -0,0 +1,3 @@ +# The encryption file is produced by the following command. +# A salt is explictly provided because we need reproducible output in tests. +openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 10000 -pass pass:somepassword -in '1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg' -out 1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc diff --git a/tests/sometext.txt b/tests/static_assets/sometext.txt similarity index 100% rename from tests/sometext.txt rename to tests/static_assets/sometext.txt diff --git a/tests/test-encrypt.ts b/tests/test-encrypt.ts index 9f5ba44..0977b41 100644 --- a/tests/test-encrypt.ts +++ b/tests/test-encrypt.ts @@ -1,8 +1,11 @@ import * as fs from "fs"; +import * as path from "path"; import { expect } from "chai"; -import { base64ToBase32 } from "../src/misc"; +import { base64ToBase32, bufferToArrayBuffer } from "../src/misc"; import { + decryptArrayBuffer, decryptBase32ToString, + encryptArrayBuffer, encryptStringToBase32, } from "../src/encrypt"; @@ -29,9 +32,11 @@ describe("Encryption tests", () => { expect(dec).equal(k); }); - it("should encrypt and get the same result as openssl", async () => { + it("should encrypt text file and get the same result as openssl", async () => { const fileContent = ( - await fs.readFileSync(__dirname + "/sometext.txt") + await fs.readFileSync( + path.join(__dirname, "static_assets", "sometext.txt") + ) ).toString("utf-8"); const password = "somepassword"; const saltHex = "8302F586FAB491EC"; @@ -52,4 +57,42 @@ describe("Encryption tests", () => { expect(enc).equal(opensslBase32Res); }); + + it("should encrypt binary file and get the same result as openssl", async () => { + const testFolder = path.join(__dirname, "static_assets", "mona_lisa"); + const testFileName = + "1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg"; + const fileArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName)) + ); + const password = "somepassword"; + const saltHex = "8302F586FAB491EC"; + const enc = await encryptArrayBuffer( + fileArrBuf, + password, + undefined, + saltHex + ); + const opensslArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName + ".enc")) + ); + + expect(Buffer.from(enc).equals(Buffer.from(opensslArrBuf))).to.be.true; + }); + + it("should descypt binary file and get the same result as openssl", async () => { + const testFolder = path.join(__dirname, "static_assets", "mona_lisa"); + const testFileName = + "1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg"; + const fileArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName + ".enc")) + ); + const password = "somepassword"; + const dec = await decryptArrayBuffer(fileArrBuf, password); + const opensslArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName)) + ); + + expect(Buffer.from(dec).equals(Buffer.from(opensslArrBuf))).to.be.true; + }); });