Projekt

Obecné

Profil

Stáhnout (1.64 KB) Statistiky
| Větev: | Revize:
1
var test = require('tape')
2

    
3
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
4
var encodings = ['hex', 'base64'] // FIXME: test binary
5
var vectors = require('hash-test-vectors')
6

    
7
testLib('createHash in crypto-browserify', require('../').createHash)
8
testLib('create-hash/browser', require('create-hash/browser'))
9

    
10
function testLib (name, createHash) {
11
  algorithms.forEach(function (algorithm) {
12
    runTest(name, createHash, algorithm)
13
  })
14
}
15
function runTest (name, createHash, algorithm) {
16
  test(name + ' test ' + algorithm + ' against test vectors', function (t) {
17
    run(0)
18
    function run (i) {
19
      if (i >= vectors.length) {
20
        return t.end()
21
      }
22
      var obj = vectors[i]
23

    
24
      var input = new Buffer(obj.input, 'base64')
25
      var node = obj[algorithm]
26
      var js = createHash(algorithm).update(input).digest('hex')
27
      if (js !== node) {
28
        t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
29
      }
30

    
31
      encodings.forEach(function (encoding) {
32
        var input = new Buffer(obj.input, 'base64').toString(encoding)
33
        var node = obj[algorithm]
34
        var js = createHash(algorithm).update(input, encoding).digest('hex')
35
        if (js !== node) {
36
          t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
37
        }
38
      })
39
      input = new Buffer(obj.input, 'base64')
40
      node = obj[algorithm]
41
      var hash = createHash(algorithm)
42
      hash.end(input)
43
      js = hash.read().toString('hex')
44
      if (js !== node) {
45
        t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
46
      }
47
      setTimeout(run, 0, i + 1)
48
    }
49
  })
50
}
(2-2/11)