Projekt

Obecné

Profil

Stáhnout (4.4 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2
/* global describe it */
3

    
4
var assert = require('assert');
5
var crypto = require('crypto');
6
var hash = require('../');
7

    
8
describe('Hash', function() {
9
  function test(fn, cases) {
10
    for (var i = 0; i < cases.length; i++) {
11
      var msg = cases[i][0];
12
      var res = cases[i][1];
13
      var enc = cases[i][2];
14

    
15
      var dgst = fn().update(msg, enc).digest('hex');
16
      assert.equal(dgst, res);
17

    
18
      // Split message
19
      dgst = fn().update(msg.slice(0, 2), enc)
20
        .update(msg.slice(2), enc)
21
        .digest('hex');
22
      assert.equal(dgst, res);
23
    }
24
  }
25

    
26
  it('should support sha256', function() {
27
    assert.equal(hash.sha256.blockSize, 512);
28
    assert.equal(hash.sha256.outSize, 256);
29

    
30
    test(hash.sha256, [
31
      [ 'abc',
32
        'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' ],
33
      [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
34
        '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1' ],
35
      [ 'deadbeef',
36
        '5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953',
37
        'hex' ],
38
    ]);
39
  });
40

    
41
  it('should support sha224', function() {
42
    assert.equal(hash.sha224.blockSize, 512);
43
    assert.equal(hash.sha224.outSize, 224);
44

    
45
    test(hash.sha224, [
46
      [ 'abc',
47
        '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7' ],
48
      [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
49
        '75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525' ],
50
      [ 'deadbeef',
51
        '55b9eee5f60cc362ddc07676f620372611e22272f60fdbec94f243f8',
52
        'hex' ],
53
    ]);
54
  });
55

    
56
  it('should support ripemd160', function() {
57
    assert.equal(hash.ripemd160.blockSize, 512);
58
    assert.equal(hash.ripemd160.outSize, 160);
59

    
60
    test(hash.ripemd160, [
61
      [ '', '9c1185a5c5e9fc54612808977ee8f548b2258d31'],
62
      [ 'abc',
63
        '8eb208f7e05d987a9b044a8e98c6b087f15a0bfc' ],
64
      [ 'message digest',
65
        '5d0689ef49d2fae572b881b123a85ffa21595f36' ],
66
      [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
67
        '12a053384a9c0c88e405a06c27dcf49ada62eb2b' ],
68
      [ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
69
        'b0e20b6e3116640286ed3a87a5713079b21f5189' ],
70
    ]);
71
  });
72

    
73
  it('should support sha1', function() {
74
    assert.equal(hash.sha1.blockSize, 512);
75
    assert.equal(hash.sha1.outSize, 160);
76

    
77
    test(hash.sha1, [
78
      [ '',
79
        'da39a3ee5e6b4b0d3255bfef95601890afd80709' ],
80
      [ 'abc',
81
        'a9993e364706816aba3e25717850c26c9cd0d89d' ],
82
      [ 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
83
        '84983e441c3bd26ebaae4aa1f95129e5e54670f1' ],
84
      [ 'deadbeef',
85
        'd78f8bb992a56a597f6c7a1fb918bb78271367eb',
86
        'hex' ],
87
    ]);
88
  });
89

    
90
  it('should support sha512', function() {
91
    assert.equal(hash.sha512.blockSize, 1024);
92
    assert.equal(hash.sha512.outSize, 512);
93

    
94
    test(hash.sha512, [
95
      [ 'abc',
96
        'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a' +
97
            '2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f'
98
      ],
99
      [
100
        'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn' +
101
          'hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
102
        '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018' +
103
          '501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'
104
      ]
105
    ]);
106
  });
107

    
108
  it('should support sha384', function() {
109
    assert.equal(hash.sha384.blockSize, 1024);
110
    assert.equal(hash.sha384.outSize, 384);
111

    
112
    test(hash.sha384, [
113
      [ 'abc',
114
        'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed' +
115
            '8086072ba1e7cc2358baeca134c825a7'
116
      ],
117
      [
118
        'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn' +
119
          'hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
120
        '09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712' +
121
          'fcc7c71a557e2db966c3e9fa91746039'
122
      ]
123
    ]);
124
  });
125

    
126
  it('handles utf8 in strings just like crypto', function() {
127
    const algorithm = 'sha256';
128
    test(hash[algorithm], [
129
      'hello', // one byte per character
130
      'привет', // two bytes per character
131
      '您好', // three bytes per character
132
      '👋', // four bytes per character
133
      'hello привет 您好 👋!!!' // mixed character lengths
134
    ].map(str => [str, crypto
135
      .createHash(algorithm)
136
      .update(str)
137
      .digest('hex')]));
138
  });
139

    
140
});
(1-1/2)