summaryrefslogtreecommitdiff
path: root/school/node_modules/node-forge/nodejs/test/des.js
blob: 8be2c6879a950d0096d287c028dede0cc74959b5 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
(function() {

function Tests(ASSERT, CIPHER, DES, UTIL) {
  describe('des', function() {
    // OpenSSL equivalent:
    // openssl enc -des-ecb -K a1c06b381adf3651 -nosalt
    it('should des-ecb encrypt: foobar', function() {
      var key = new UTIL.createBuffer(
        UTIL.hexToBytes('a1c06b381adf3651'));

      var cipher = CIPHER.createCipher('DES-ECB', key);
      cipher.start();
      cipher.update(UTIL.createBuffer('foobar'));
      cipher.finish();
      ASSERT.equal(cipher.output.toHex(), 'b705ffcf3dff06b3');
    });

    // OpenSSL equivalent:
    // openssl enc -d -des-ecb -K a1c06b381adf3651 -nosalt
    it('should des-ecb decrypt: b705ffcf3dff06b3', function() {
      var key = new UTIL.createBuffer(
        UTIL.hexToBytes('a1c06b381adf3651'));

      var decipher = CIPHER.createDecipher('DES-ECB', key);
      decipher.start();
      decipher.update(UTIL.createBuffer(UTIL.hexToBytes('b705ffcf3dff06b3')));
      decipher.finish();
      ASSERT.equal(decipher.output.getBytes(), 'foobar');
    });

    // OpenSSL equivalent:
    // openssl enc -des -K a1c06b381adf3651 -iv 818bcf76efc59662 -nosalt
    it('should des-cbc encrypt: foobar', function() {
      var key = new UTIL.createBuffer(
        UTIL.hexToBytes('a1c06b381adf3651'));
      var iv = new UTIL.createBuffer(
        UTIL.hexToBytes('818bcf76efc59662'));

      var cipher = CIPHER.createCipher('DES-CBC', key);
      cipher.start({iv: iv});
      cipher.update(UTIL.createBuffer('foobar'));
      cipher.finish();
      ASSERT.equal(cipher.output.toHex(), '3261e5839a990454');
    });

    // OpenSSL equivalent:
    // openssl enc -d -des -K a1c06b381adf3651 -iv 818bcf76efc59662 -nosalt
    it('should des-cbc decrypt: 3261e5839a990454', function() {
      var key = new UTIL.createBuffer(
        UTIL.hexToBytes('a1c06b381adf3651'));
      var iv = new UTIL.createBuffer(
        UTIL.hexToBytes('818bcf76efc59662'));

      var decipher = CIPHER.createDecipher('DES-CBC', key);
      decipher.start({iv: iv});
      decipher.update(UTIL.createBuffer(UTIL.hexToBytes('3261e5839a990454')));
      decipher.finish();
      ASSERT.equal(decipher.output.getBytes(), 'foobar');
    });

    // OpenSSL equivalent:
    // openssl enc -des-ede3 -K a1c06b381adf36517e84575552777779da5e3d9f994b05b5 -nosalt
    it('should 3des-ecb encrypt: foobar', function() {
      var key = new UTIL.createBuffer(
        UTIL.hexToBytes('a1c06b381adf36517e84575552777779da5e3d9f994b05b5'));

      var cipher = CIPHER.createCipher('3DES-ECB', key);
      cipher.start();
      cipher.update(UTIL.createBuffer('foobar'));
      cipher.finish();
      ASSERT.equal(cipher.output.toHex(), 'fce8b1ee8c6440d1');
    });

    // OpenSSL equivalent:
    // openssl enc -d -des-ede3 -K a1c06b381adf36517e84575552777779da5e3d9f994b05b5 -nosalt
    it('should 3des-ecb decrypt: fce8b1ee8c6440d1', function() {
      var key = new UTIL.createBuffer(
        UTIL.hexToBytes('a1c06b381adf36517e84575552777779da5e3d9f994b05b5'));

      var decipher = CIPHER.createDecipher('3DES-ECB', key);
      decipher.start();
      decipher.update(UTIL.createBuffer(UTIL.hexToBytes('fce8b1ee8c6440d1')));
      decipher.finish();
      ASSERT.equal(decipher.output.getBytes(), 'foobar');
    });

    // OpenSSL equivalent:
    // openssl enc -des3 -K a1c06b381adf36517e84575552777779da5e3d9f994b05b5 -iv 818bcf76efc59662 -nosalt
    it('should 3des-cbc encrypt "foobar", restart, and encrypt "foobar,,"', function() {
      var key = new UTIL.createBuffer(
        UTIL.hexToBytes('a1c06b381adf36517e84575552777779da5e3d9f994b05b5'));
      var iv = new UTIL.createBuffer(
        UTIL.hexToBytes('818bcf76efc59662'));

      var cipher = CIPHER.createCipher('3DES-CBC', key);
      cipher.start({iv: iv.copy()});
      cipher.update(UTIL.createBuffer('foobar'));
      cipher.finish();
      ASSERT.equal(cipher.output.toHex(), '209225f7687ca0b2');

      cipher.start({iv: iv.copy()});
      cipher.update(UTIL.createBuffer('foobar,,'));
      cipher.finish();
      ASSERT.equal(cipher.output.toHex(), '57156174c48dfc37293831bf192a6742');
    });

    // OpenSSL equivalent:
    // openssl enc -d -des3 -K a1c06b381adf36517e84575552777779da5e3d9f994b05b5 -iv 818bcf76efc59662 -nosalt
    it('should 3des-cbc decrypt "209225f7687ca0b2", restart, and decrypt "57156174c48dfc37293831bf192a6742,,"', function() {
      var key = new UTIL.createBuffer(
        UTIL.hexToBytes('a1c06b381adf36517e84575552777779da5e3d9f994b05b5'));
      var iv = new UTIL.createBuffer(
        UTIL.hexToBytes('818bcf76efc59662'));

      var decipher = CIPHER.createDecipher('3DES-CBC', key);
      decipher.start({iv: iv.copy()});
      decipher.update(UTIL.createBuffer(UTIL.hexToBytes('209225f7687ca0b2')));
      decipher.finish();
      ASSERT.equal(decipher.output.getBytes(), 'foobar');

      decipher.start({iv: iv.copy()});
      decipher.update(
        UTIL.createBuffer(UTIL.hexToBytes('57156174c48dfc37293831bf192a6742')));
      decipher.finish();
      ASSERT.equal(decipher.output.getBytes(), 'foobar,,');
    });
  });
}

// check for AMD
var forge = {};
if(typeof define === 'function') {
  define([
    'forge/cipher',
    'forge/des',
    'forge/util'
  ], function(CIPHER, DES, UTIL) {
    Tests(
      // Global provided by test harness
      ASSERT,
      CIPHER(forge),
      DES(forge),
      UTIL(forge)
    );
  });
} else if(typeof module === 'object' && module.exports) {
  // assume NodeJS
  Tests(
    require('assert'),
    require('../../js/cipher')(forge),
    require('../../js/des')(forge),
    require('../../js/util')(forge));
}

})();