| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 'use strict';
- const common = require('../common');
- const fc = require('fast-check');
- const fs = require('fs');
- describe('CryptTool', function () {
- describe('cipher & decipher', function () {
- afterEach(async function () {
- // pause to let async functions conclude
- await new Promise(resolve => setTimeout(resolve, 1900));
- });
- this.timeout(30000);
- it('can en- and decrypt any message', async function () {
- await fc.assert(fc.asyncProperty(
- fc.string(),
- fc.string(),
- fc.string(),
- async function (key, password, message) {
- const clean = globalThis.cleanup();
- // ensure zlib is getting loaded
- PrivateBin.Controller.initZ();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- configurable: true,
- enumerable: true,
- writable: false
- });
- global.atob = common.atob;
- global.btoa = common.btoa;
- message = message.trim();
- const cipherMessage = await PrivateBin.CryptTool.cipher(
- key, password, message, []
- ),
- plaintext = await PrivateBin.CryptTool.decipher(
- key, password, cipherMessage
- );
- clean();
- const result = (message === plaintext);
- if (!result) console.log(plaintext, cipherMessage);
- return result;
- }
- ),
- {numRuns: 3});
- });
- it('does not truncate messages', async function () {
- const message = fs.readFileSync('test/compression-sample.txt', 'ascii').trim(),
- clean = globalThis.cleanup();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- configurable: true,
- enumerable: true,
- writable: false
- });
- // ensure zlib is getting loaded
- PrivateBin.Controller.initZ();
- global.atob = common.atob;
- global.btoa = common.btoa;
- const cipherMessage = await PrivateBin.CryptTool.cipher(
- 'foo', 'bar', message, []
- ),
- plaintext = await PrivateBin.CryptTool.decipher(
- 'foo', 'bar', cipherMessage
- );
- clean();
- if (message !== plaintext) {
- console.log(plaintext, cipherMessage);
- }
- assert.strictEqual(message, plaintext);
- });
- it('can en- and decrypt a particular message (#260)', async function () {
- await fc.assert(fc.asyncProperty(
- fc.string(),
- fc.string(),
- async function (key, password) {
- const message = `
- 1 subgoal
- inv : Assert
- expr : Expr
- sBody : Instr
- deduction : (|- [|inv /\ assertOfExpr expr|] sBody [|inv|])%assert
- IHdeduction : (|= [|inv /\ assertOfExpr expr |] sBody [|inv|])%assert
- mem : Mem
- preInMem : inv mem
- m : Mem
- n : nat
- interpRel : interp (nth_iterate sBody n) (MemElem mem) = CpoElem Mem m
- lastIter : interp (nth_iterate sBody n) (MemElem mem) |=e expr_neg expr
- notLastIter : forall p : nat,
- p < n -> interp (nth_iterate sBody p) (MemElem mem) |=e expr
- isWhile : interp (while expr sBody) (MemElem mem) =
- interp (nth_iterate sBody n) (MemElem mem)
- ======================== ( 1 / 1 )
- conseq_or_bottom inv (interp (nth_iterate sBody n) (MemElem mem))
- `;
- const clean = globalThis.cleanup();
- // ensure zlib is getting loaded
- PrivateBin.Controller.initZ();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- configurable: true,
- enumerable: true,
- writable: false
- });
- const cipherMessage = await PrivateBin.CryptTool.cipher(
- key, password, message, []
- ),
- plaintext = await PrivateBin.CryptTool.decipher(
- key, password, cipherMessage
- );
- clean();
- const result = (message === plaintext);
- if (!result) console.log(plaintext, cipherMessage);
- return result;
- }
- ),
- {numRuns: 3});
- });
- });
- describe('getSymmetricKey', function () {
- this.timeout(10000);
- let keys = [];
- // the parameter is used to ensure the test is run more then one time
- it('returns random, non-empty keys', () => {
- fc.assert(fc.property(
- fc.integer(),
- function(counter) {
- const clean = globalThis.cleanup();
- Object.defineProperty(window, 'crypto', {
- value: new WebCrypto(),
- configurable: true,
- enumerable: true,
- writable: false
- });
- const key = PrivateBin.CryptTool.getSymmetricKey(),
- result = (key !== '' && keys.indexOf(key) === -1);
- keys.push(key);
- clean();
- return result;
- }
- ),
- {numRuns: 10});
- });
- });
- });
|