From b97ac0800352e085ecdf5070b42b1536aef30139 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sat, 1 Sep 2018 22:22:10 +0200 Subject: [PATCH] improving tests, correcting cipher --- js/privatebin.js | 5 +++-- js/test/CryptTool.js | 22 ++++++---------------- tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index 05f8ec0..c125f0b 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -764,7 +764,8 @@ jQuery.PrivateBin = (function($, sjcl, RawDeflate) { derivedKey, StrToArr(compress(message)) // compressed plain text to encrypt ) - return btoa(ArrToStr(encrypted)); + object.ct = btoa(ArrToStr(encrypted)); + return JSON.stringify(object); }; /** @@ -780,7 +781,7 @@ jQuery.PrivateBin = (function($, sjcl, RawDeflate) { me.decipher = async function(key, password, data) { try { - if (password.length > 0) { + if ((password || '').trim().length > 0) { key += sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(password)); } var object = JSON.parse(data), diff --git a/js/test/CryptTool.js b/js/test/CryptTool.js index 756c0e9..d4fbbf0 100644 --- a/js/test/CryptTool.js +++ b/js/test/CryptTool.js @@ -1,5 +1,7 @@ 'use strict'; require('../common'); +jsdom(); +window.crypto = new WebCrypto(); describe('CryptTool', function () { describe('cipher & decipher', function () { @@ -9,18 +11,14 @@ describe('CryptTool', function () { 'string', 'string', 'string', - function (key, password, message) { - jsdom(); - window.crypto = new WebCrypto(); - return message === $.PrivateBin.CryptTool.decipher( + async function (key, password, message) { + return message.trim() === await $.PrivateBin.CryptTool.decipher( key, password, - $.PrivateBin.CryptTool.cipher(key, password, message.trim()) + await $.PrivateBin.CryptTool.cipher(key, password, message.trim()) ); } - ), - // reducing amount of checks as running 100 takes about 5 minutes - {tests: 5, quiet: true}); + )); }); // The below static unit tests are included to ensure deciphering of "classic" @@ -30,9 +28,7 @@ describe('CryptTool', function () { async function () { delete global.Base64; // make btoa available - jsdom(); global.btoa = window.btoa; - window.crypto = new WebCrypto(); // Of course you can easily decipher the following texts, if you like. // Bonus points for finding their sources and hidden meanings. @@ -106,8 +102,6 @@ describe('CryptTool', function () { 'supports ZeroBin ciphertext (SJCL & Base64 1.7)', async function () { global.Base64 = require('../base64-1.7').Base64; - jsdom(); - window.crypto = new WebCrypto(); // Of course you can easily decipher the following texts, if you like. // Bonus points for finding their sources and hidden meanings. @@ -172,8 +166,6 @@ describe('CryptTool', function () { 'returns random, non-empty keys', 'integer', function(counter) { - jsdom(); - window.crypto = new WebCrypto(); var key = $.PrivateBin.CryptTool.getSymmetricKey(), result = (key !== '' && keys.indexOf(key) === -1); keys.push(key); @@ -187,8 +179,6 @@ describe('CryptTool', function () { 'these all return the same base64 string', 'string', function(string) { - // make btoa/atob available - jsdom(); // not comparing Base64.js v1.7 encode/decode, that has known issues var Base64 = require('../base64-1.7').Base64, sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)), diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index 4c14115..83de5f4 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -71,7 +71,7 @@ if ($MARKDOWN): endif; ?> - + diff --git a/tpl/page.php b/tpl/page.php index fdce1ec..87af603 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -49,7 +49,7 @@ if ($MARKDOWN): endif; ?> - +