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 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b332c298be98be9968715952822431a051f9d07e67460cdcf91a5a3a305b5df1
size 1141550

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cf67abd791f1e230bce57dd47150f262424247cbefb8be16efcd57d13955576a
size 1141568

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 fs from "fs";
import * as path from "path";
import { expect } from "chai"; import { expect } from "chai";
import { base64ToBase32 } from "../src/misc"; import { base64ToBase32, bufferToArrayBuffer } from "../src/misc";
import { import {
decryptArrayBuffer,
decryptBase32ToString, decryptBase32ToString,
encryptArrayBuffer,
encryptStringToBase32, encryptStringToBase32,
} from "../src/encrypt"; } from "../src/encrypt";
@ -29,9 +32,11 @@ describe("Encryption tests", () => {
expect(dec).equal(k); 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 = ( const fileContent = (
await fs.readFileSync(__dirname + "/sometext.txt") await fs.readFileSync(
path.join(__dirname, "static_assets", "sometext.txt")
)
).toString("utf-8"); ).toString("utf-8");
const password = "somepassword"; const password = "somepassword";
const saltHex = "8302F586FAB491EC"; const saltHex = "8302F586FAB491EC";
@ -52,4 +57,42 @@ describe("Encryption tests", () => {
expect(enc).equal(opensslBase32Res); 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;
});
}); });