Projekt

Obecné

Profil

Stáhnout (1.99 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var assert = require('assert');
4
var crypto = require('crypto');
5
var Buffer = require('buffer').Buffer;
6

    
7
var des = require('../');
8

    
9
var fixtures = require('./fixtures');
10
var bin = fixtures.bin;
11

    
12
describe('DES-EDE-CBC', function() {
13
  var CBC = des.CBC.instantiate(des.EDE);
14

    
15
  describe('encryption/decryption', function() {
16
    var vectors = [
17
      {
18
        key: new Array(4).join('133457799bbcdff1'),
19
        iv: '0102030405060708',
20
        input: '0123456789abcdef'
21
      },
22
      {
23
        key: new Array(4).join('0000000000000000'),
24
        iv: 'ffffffffffffffff',
25
        input: '0000000000000000'
26
      },
27
      {
28
        key: new Array(4).join('a3a3a3a3b3b3b3b3'),
29
        iv: 'cdcdcdcdcdcdcdcd',
30
        input: 'cccccccccccccccc'
31
      },
32
      {
33
        key: new Array(4).join('deadbeefabbadead'),
34
        iv: 'a0da0da0da0da0da',
35
        input: '0102030405060708090a'
36
      },
37
      {
38
        key: 'aabbccddeeff0011' + '1111222233334444' + 'ffffeeeeddddcccc',
39
        iv: 'fefefefefefefefe',
40
        input: '0102030405060708090a0102030405060708090a0102030405060708090a' +
41
               '0102030405060708090a0102030405060607080a0102030405060708090a'
42
      }
43
    ];
44

    
45
    vectors.forEach(function(vec, i) {
46
      it('should encrypt vector ' + i, function() {
47
        var key = new Buffer(vec.key, 'hex');
48
        var iv = new Buffer(vec.iv, 'hex');
49
        var input = new Buffer(vec.input, 'hex');
50

    
51
        var enc = CBC.create({
52
          type: 'encrypt',
53
          key: key,
54
          iv: iv
55
        });
56
        var out = new Buffer(enc.update(input).concat(enc.final()));
57

    
58
        var cipher = crypto.createCipheriv('des-ede3-cbc', key, iv);
59
        var expected = Buffer.concat([ cipher.update(input), cipher.final() ]);
60

    
61
        assert.deepEqual(out, expected);
62

    
63
        var dec = CBC.create({
64
          type: 'decrypt',
65
          key: key,
66
          iv: iv
67
        });
68
        assert.deepEqual(new Buffer(dec.update(out).concat(dec.final())),
69
                         input);
70
      });
71
    });
72
  });
73
});
(3-3/5)