Projekt

Obecné

Profil

Stáhnout (1.8 KB) Statistiky
| Větev: | Revize:
1
var test = require('tape')
2
var crypto = require('crypto')
3
var desModes = require('browserify-des/modes')
4
var aesModes = require('browserify-aes/modes')
5
var ourCrypto = require('./browser')
6

    
7
function runIvTest (mode, keyLen, ivLen) {
8
  test('mode: ' + mode, function (t) {
9
    var i = 0
10
    while (++i < 10) {
11
      run(i)
12
    }
13
    function run (i) {
14
      t.test('run: ' + i, function (t) {
15
        t.plan(2)
16
        var key = crypto.randomBytes(keyLen)
17
        var iv = crypto.randomBytes(ivLen)
18
        var text = crypto.randomBytes(200)
19
        var ourEncrypt
20
        try {
21
          ourEncrypt = ourCrypto.createCipheriv(mode, key, iv)
22
        } catch (e) {
23
          t.notOk(e, e.stack)
24
        }
25
        var nodeEncrypt
26
        try {
27
          nodeEncrypt = crypto.createCipheriv(mode, key, iv)
28
        } catch (e) {
29
          t.notOk(e, e.stack)
30
        }
31
        var ourCipherText = Buffer.concat([ourEncrypt.update(text), ourEncrypt.final()])
32
        var authTag
33
        if (mode.slice(-3) === 'gcm') {
34
          authTag = ourEncrypt.getAuthTag()
35
        }
36
        var nodeCipherText = Buffer.concat([nodeEncrypt.update(text), nodeEncrypt.final()])
37
        t.equals(nodeCipherText.toString('hex'), ourCipherText.toString('hex'))
38
        var ourDecrypt = ourCrypto.createDecipheriv(mode, key, iv)
39
        if (mode.slice(-3) === 'gcm') {
40
          ourDecrypt.setAuthTag(authTag)
41
        }
42
        var plainText = Buffer.concat([ourDecrypt.update(ourCipherText), ourDecrypt.final()])
43
        t.equals(text.toString('hex'), plainText.toString('hex'))
44
      })
45
    }
46
  })
47
}
48
Object.keys(aesModes).forEach(function (modeName) {
49
  var mode = aesModes[modeName]
50
  runIvTest(modeName, mode.key / 8, mode.iv)
51
})
52
Object.keys(desModes).forEach(function (modeName) {
53
  var mode = desModes[modeName]
54
  runIvTest(modeName, mode.key, mode.iv)
55
})
(7-7/7)