remotely-save/tests/encryptOpenSSL.test.ts

164 lines
5.9 KiB
TypeScript
Raw Normal View History

2024-04-26 19:28:39 +00:00
import { strict as assert } from "assert";
2021-12-31 15:46:52 +00:00
import * as fs from "fs";
import * as path from "path";
2021-11-05 16:23:30 +00:00
import {
2021-11-06 04:57:40 +00:00
decryptArrayBuffer,
2021-11-05 16:23:30 +00:00
decryptBase32ToString,
2021-11-06 04:57:40 +00:00
encryptArrayBuffer,
2021-11-05 16:23:30 +00:00
encryptStringToBase32,
2021-12-31 12:55:13 +00:00
encryptStringToBase64url,
2022-03-20 15:01:32 +00:00
getSizeFromEncToOrig,
getSizeFromOrigToEnc,
2024-03-23 08:38:58 +00:00
} from "../src/encryptOpenSSL";
2021-12-31 15:46:52 +00:00
import { base64ToBase64url, bufferToArrayBuffer } from "../src/misc";
2021-11-04 17:21:58 +00:00
2024-03-23 08:38:58 +00:00
describe("Encryption OpenSSL tests", () => {
2024-05-07 16:20:15 +00:00
beforeEach(() => {
2021-11-04 17:29:44 +00:00
global.window = {
crypto: require("crypto").webcrypto,
} as any;
2021-11-04 17:21:58 +00:00
});
it("should encrypt string", async () => {
const k = "dkjdhkfhdkjgsdklxxd";
const password = "hey";
2024-04-26 19:28:39 +00:00
assert.notEqual(await encryptStringToBase32(k, password), k);
2021-11-04 17:21:58 +00:00
});
2021-11-05 16:23:30 +00:00
it("should encrypt string and return different results each time", async () => {
const k = "dkjdhkfhdkjgsdklxxd";
const password = "hey";
const res1 = await encryptStringToBase32(k, password);
const res2 = await encryptStringToBase32(k, password);
2024-04-26 19:28:39 +00:00
assert.notEqual(res1, res2);
});
2021-11-08 02:00:06 +00:00
it("should raise error using different password", async () => {
const k = "secret text";
const password = "hey";
const password2 = "hey2";
const enc = await encryptStringToBase32(k, password);
2024-04-26 19:28:39 +00:00
await assert.rejects(decryptBase32ToString(enc, password2));
2021-11-08 02:00:06 +00:00
});
2021-11-05 16:23:30 +00:00
it("should encrypt and decrypt string and get the same result returned", async () => {
const k = "jfkkjkjbce7983ycdeknkkjckooAIUHIDIBIE((*BII)njD/d/dd/d/sjxhux";
const password = "hfiuibec989###oiu982bj1`";
const enc = await encryptStringToBase32(k, password);
2021-11-05 16:26:44 +00:00
// console.log(enc);
2021-11-05 16:23:30 +00:00
const dec = await decryptBase32ToString(enc, password);
2021-11-05 16:26:44 +00:00
// console.log(dec);
2024-04-26 19:28:39 +00:00
assert.equal(dec, k);
2021-11-05 16:23:30 +00:00
});
2021-11-06 04:57:40 +00:00
it("should encrypt text file and get the same result as openssl", async () => {
2021-11-05 16:23:30 +00:00
const fileContent = (
2021-11-06 04:57:40 +00:00
await fs.readFileSync(
path.join(__dirname, "static_assets", "sometext.txt")
)
2021-11-05 16:23:30 +00:00
).toString("utf-8");
const password = "somepassword";
const saltHex = "8302F586FAB491EC";
2021-12-31 12:55:13 +00:00
const enc = await encryptStringToBase64url(
2021-11-05 16:23:30 +00:00
fileContent,
password,
undefined,
saltHex
);
// two command returns same result:
2021-11-14 16:26:40 +00:00
// cat ./sometext.txt | openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -base64 -pass pass:somepassword
// openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -base64 -pass pass:somepassword -in ./sometext.txt
2021-11-05 16:23:30 +00:00
const opensslBase64Res =
2021-11-14 16:26:40 +00:00
"U2FsdGVkX1+DAvWG+rSR7BPXMnlvSSVGMdjsx7kE1CTH+28P+yAZRdDGgFWMGkMd";
2021-11-05 16:23:30 +00:00
// we output base32, so we need some transformation
2021-12-31 12:55:13 +00:00
const opensslBase64urlRes = base64ToBase64url(opensslBase64Res);
2021-11-05 16:23:30 +00:00
2024-04-26 19:28:39 +00:00
assert.equal(enc, opensslBase64urlRes);
2021-11-05 16:23:30 +00:00
});
2021-11-06 04:57:40 +00:00
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"))
);
2021-11-14 16:26:40 +00:00
// openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -pass pass:somepassword -in mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg -out mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc
2024-04-26 19:28:39 +00:00
assert.ok(Buffer.from(enc).equals(Buffer.from(opensslArrBuf)));
2021-11-06 04:57:40 +00:00
});
it("should encrypt binary file not deterministically", 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 res1 = await encryptArrayBuffer(fileArrBuf, password);
const res2 = await encryptArrayBuffer(fileArrBuf, password);
2024-04-26 19:28:39 +00:00
assert.ok(!Buffer.from(res1).equals(Buffer.from(res2)));
});
it("should decrypt binary file and get the same result as openssl", async () => {
2021-11-06 04:57:40 +00:00
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))
);
2024-04-26 19:28:39 +00:00
assert.deepEqual(Buffer.from(dec), Buffer.from(opensslArrBuf));
2021-11-06 04:57:40 +00:00
});
2022-03-20 15:01:32 +00:00
it("should get size from origin to encrypted correctly", () => {
2024-04-26 19:28:39 +00:00
assert.throws(() => getSizeFromOrigToEnc(-1));
assert.throws(() => getSizeFromOrigToEnc(0.5));
assert.equal(getSizeFromOrigToEnc(0), 32);
assert.equal(getSizeFromOrigToEnc(15), 32);
assert.equal(getSizeFromOrigToEnc(16), 48);
assert.equal(getSizeFromOrigToEnc(31), 48);
assert.equal(getSizeFromOrigToEnc(32), 64);
assert.equal(getSizeFromOrigToEnc(14787203), 14787232);
2022-03-20 15:01:32 +00:00
});
it("should get size from encrypted to origin correctly", () => {
2024-04-26 19:28:39 +00:00
assert.throws(() => getSizeFromEncToOrig(-1));
assert.throws(() => getSizeFromEncToOrig(30));
2022-03-20 15:01:32 +00:00
2024-04-26 19:28:39 +00:00
assert.deepEqual(getSizeFromEncToOrig(32), {
2022-03-20 15:01:32 +00:00
minSize: 0,
maxSize: 15,
});
2024-04-26 19:28:39 +00:00
assert.deepEqual(getSizeFromEncToOrig(48), {
2022-03-20 15:01:32 +00:00
minSize: 16,
maxSize: 31,
});
2024-04-26 19:28:39 +00:00
assert.throws(() => getSizeFromEncToOrig(14787231));
2022-03-20 15:01:32 +00:00
2024-05-07 16:20:15 +00:00
const { minSize, maxSize } = getSizeFromEncToOrig(14787232);
2024-04-26 19:28:39 +00:00
assert.ok(minSize <= 14787203 && 14787203 <= maxSize);
2022-03-20 15:01:32 +00:00
});
2021-11-04 17:21:58 +00:00
});