test for enc

This commit is contained in:
fyears 2021-11-06 12:57:40 +08:00
parent ec2fec9bf8
commit 8fbbae6c2e
7 changed files with 63 additions and 3 deletions

5
.gitattributes vendored Normal file
View File

@ -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

View File

@ -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 ...

View File

@ -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

View File

@ -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;
});
});