Browse Source

TST: encryption

tst
Overtorment 5 years ago
parent
commit
6589ccca58
  1. 7
      encryption.js
  2. 9
      screen/selftest.js
  3. 17
      tests/unit/encryption.js

7
encryption.js

@ -1,6 +1,7 @@
let CryptoJS = require('crypto-js'); let CryptoJS = require('crypto-js');
module.exports.encrypt = function(data, password) { module.exports.encrypt = function(data, password) {
if (data.length < 10) throw new Error('data length cant be < 10');
let ciphertext = CryptoJS.AES.encrypt(data, password); let ciphertext = CryptoJS.AES.encrypt(data, password);
return ciphertext.toString(); return ciphertext.toString();
}; };
@ -11,5 +12,11 @@ module.exports.decrypt = function(data, password) {
try { try {
str = bytes.toString(CryptoJS.enc.Utf8); str = bytes.toString(CryptoJS.enc.Utf8);
} catch (e) {} } catch (e) {}
// for some reason, sometimes decrypt would succeed with wrong password and return random couple of characters.
// at least in nodejs environment. so with this little hack we are not alowing to encrypt data that is shorter than
// 10 characters, and thus if decrypted data is less than 10 characters we assume that decrypt actually failed.
if (str.length < 10) return false;
return str; return str;
}; };

9
screen/selftest.js

@ -202,13 +202,12 @@ export default class Selftest extends Component {
} }
// //
const data2encrypt = 'really long data string';
let crypted = encryption.encrypt('data', 'password'); let crypted = encryption.encrypt(data2encrypt, 'password');
let decrypted = encryption.decrypt(crypted, 'password'); let decrypted = encryption.decrypt(crypted, 'password');
if (decrypted !== 'data' && crypted && decrypted) { if (decrypted !== data2encrypt && crypted && decrypted) {
errorMessage += 'encryption lib is not ok; '; throw new Error('encryption lib is not ok');
isOk = false;
} }
// //

17
tests/unit/encryption.js

@ -5,19 +5,28 @@ let c = require('../../encryption')
describe('unit - encryption', function () { describe('unit - encryption', function () {
it('encrypts and decrypts', function () { it('encrypts and decrypts', function () {
let crypted = c.encrypt('data', 'password'); const data2encrypt = 'really long data string bla bla really long data string bla bla really long data string bla bla';
let decrypted = c.decrypt(crypted, 'password'); const crypted = c.encrypt(data2encrypt, 'password');
const decrypted = c.decrypt(crypted, 'password');
assert.ok(crypted); assert.ok(crypted);
assert.ok(decrypted); assert.ok(decrypted);
assert.equal(decrypted, 'data'); assert.equal(decrypted, data2encrypt);
assert.ok(crypted !== 'data'); assert.ok(crypted !== data2encrypt);
let decryptedWithBadPassword let decryptedWithBadPassword
try { try {
decryptedWithBadPassword = c.decrypt(crypted, 'passwordBad'); decryptedWithBadPassword = c.decrypt(crypted, 'passwordBad');
} catch (e) {} } catch (e) {}
assert.ok(!decryptedWithBadPassword) assert.ok(!decryptedWithBadPassword)
let exceptionRaised = false;
try {
c.encrypt('yolo', 'password');
} catch (_) {
exceptionRaised = true;
}
assert.ok(exceptionRaised);
}) })
it('handles ok malformed data', function() { it('handles ok malformed data', function() {

Loading…
Cancel
Save