-
Notifications
You must be signed in to change notification settings - Fork 3
/
mcrypt.test.js
70 lines (60 loc) · 2.53 KB
/
mcrypt.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var base64 = require('base64-js');
var mcrypt = require('./mcrypt');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var should = require('chai').should();
var testData = require('./test-data');
describe('mcrypt', function() {
describe('listAlgorithms', function () {
it('should return all available algorithms', function () {
mcrypt.listAlgorithms().should.deep.equal(['rijndael-128', 'rijndael-192', 'rijndael-256']);
});
});
describe('listModes', function () {
it('should return all available modes', function () {
mcrypt.listModes().should.deep.equal(['ecb', 'cbc', 'cfb', 'ncfb', 'nofb', 'ctr']);
});
});
describe('getBlockSize', function () {
it('should return false if cipher does not exist', function () {
mcrypt.getBlockSize('fakeCipher').should.be.false;
});
it('should return block size for all ciphers', function () {
mcrypt.getBlockSize('rijndael-128').should.equal(16);
mcrypt.getBlockSize('rijndael-192').should.equal(24);
mcrypt.getBlockSize('rijndael-256').should.equal(32);
});
});
describe('getIVSize', function () {
it('should return false if cipher does not exist', function () {
mcrypt.getIVSize('fakeCipher').should.be.false;
});
it('should return iv size for all ciphers', function () {
mcrypt.getIVSize('rijndael-128').should.equal(16);
mcrypt.getIVSize('rijndael-192').should.equal(24);
mcrypt.getIVSize('rijndael-256').should.equal(32);
});
});
describe('getKeySize', function () {
it('should return false if cipher does not exist', function () {
mcrypt.getKeySize('fakeCipher').should.be.false;
});
it('should return iv size for all ciphers', function () {
mcrypt.getKeySize('rijndael-128').should.equal(32);
mcrypt.getKeySize('rijndael-192').should.equal(32);
mcrypt.getKeySize('rijndael-256').should.equal(32);
});
});
describe('decrypt', function () {
var expectedText = testData.plaintext;
testData.encoded.forEach(function(cipher) {
it('should give the exact same result as libmcrypt, ' + cipher.cipher + ' ' + cipher.mode, function () {
var key = [].slice.call(base64.toByteArray(cipher.key));
var iv = [].slice.call(base64.toByteArray(cipher.iv));
var message = [].slice.call(base64.toByteArray(cipher.message));
var clearText = String.fromCharCode.apply(this, mcrypt.decrypt(message, iv, key, cipher.cipher, cipher.mode));
clearText.toString().should.equal(expectedText);
});
});
});
});