UNPKG

4.88 kBJavaScriptView Raw
1'use strict';
2
3require('chai').should();
4// var bitcore = require('../..');
5// var Hash = bitcore.utils.Hash;
6var Hash = require('../../lib/index').Hash
7
8describe('Hash', function() {
9 var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]);
10 var str = 'test string';
11
12 describe('@sha1', function() {
13
14 it('calculates the hash of this buffer correctly', function() {
15 var hash = Hash.sha1(buf);
16 hash.toString('hex').should.equal('de69b8a4a5604d0486e6420db81e39eb464a17b2');
17 hash = Hash.sha1(new Buffer(0));
18 hash.toString('hex').should.equal('da39a3ee5e6b4b0d3255bfef95601890afd80709');
19 });
20
21 it('throws an error when the input is not a buffer', function() {
22 Hash.sha1.bind(Hash, str).should.throw('Invalid Argument');
23 });
24
25 });
26
27 describe('#sha256', function() {
28
29 it('calculates the hash of this buffer correctly', function() {
30 var hash = Hash.sha256(buf);
31 hash.toString('hex').should.equal('6f2c7b22fd1626998287b3636089087961091de80311b9279c4033ec678a83e8');
32 });
33
34 it('fails when the input is not a buffer', function() {
35 Hash.sha256.bind(Hash, str).should.throw('Invalid Argument');
36 });
37
38 });
39
40 describe('#sha256hmac', function() {
41
42 it('computes this known big key correctly', function() {
43 var key = new Buffer('b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad' +
44 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad' +
45 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad' +
46 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad');
47 var data = new Buffer('');
48 Hash.sha256hmac(data, key).toString('hex')
49 .should.equal('fb1f87218671f1c0c4593a88498e02b6dfe8afd814c1729e89a1f1f6600faa23');
50 });
51
52 it('computes this known empty test vector correctly', function() {
53 var key = new Buffer('');
54 var data = new Buffer('');
55 Hash.sha256hmac(data, key).toString('hex')
56 .should.equal('b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad');
57 });
58
59 it('computes this known non-empty test vector correctly', function() {
60 var key = new Buffer('key');
61 var data = new Buffer('The quick brown fox jumps over the lazy dog');
62 Hash.sha256hmac(data, key).toString('hex')
63 .should.equal('f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8');
64 });
65
66 });
67
68 describe('#sha256sha256', function() {
69
70 it('calculates the hash of this buffer correctly', function() {
71 var hash = Hash.sha256sha256(buf);
72 hash.toString('hex').should.equal('be586c8b20dee549bdd66018c7a79e2b67bb88b7c7d428fa4c970976d2bec5ba');
73 });
74
75 it('fails when the input is not a buffer', function() {
76 Hash.sha256sha256.bind(Hash, str).should.throw('Invalid Argument');
77 });
78
79 });
80
81 describe('#sha256ripemd160', function() {
82
83 it('calculates the hash of this buffer correctly', function() {
84 var hash = Hash.sha256ripemd160(buf);
85 hash.toString('hex').should.equal('7322e2bd8535e476c092934e16a6169ca9b707ec');
86 });
87
88 it('fails when the input is not a buffer', function() {
89 Hash.sha256ripemd160.bind(Hash, str).should.throw('Invalid Argument');
90 });
91
92 });
93
94 describe('#ripemd160', function() {
95
96 it('calculates the hash of this buffer correctly', function() {
97 var hash = Hash.ripemd160(buf);
98 hash.toString('hex').should.equal('fa0f4565ff776fee0034c713cbf48b5ec06b7f5c');
99 });
100
101 it('fails when the input is not a buffer', function() {
102 Hash.ripemd160.bind(Hash, str).should.throw('Invalid Argument');
103 });
104
105 });
106
107 describe('#sha512', function() {
108
109 it('calculates the hash of this buffer correctly', function() {
110 var hash = Hash.sha512(buf);
111 hash.toString('hex')
112 .should.equal('c0530aa32048f4904ae162bc14b9eb535eab6c465e960130005fedd' +
113 'b71613e7d62aea75f7d3333ba06e805fc8e45681454524e3f8050969fe5a5f7f2392e31d0');
114 });
115
116 it('fails when the input is not a buffer', function() {
117 Hash.sha512.bind(Hash, str).should.throw('Invalid Argument');
118 });
119
120 });
121
122 describe('#sha512hmac', function() {
123
124 it('calculates this known empty test vector correctly', function() {
125 var hex = 'b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4a' +
126 'c6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47';
127 Hash.sha512hmac(new Buffer([]), new Buffer([])).toString('hex').should.equal(hex);
128 });
129
130 it('calculates this known non-empty test vector correctly', function() {
131 var hex = 'c40bd7c15aa493b309c940e08a73ffbd28b2e4cb729eb94480d727e4df577' +
132 'b13cc403a78e6150d83595f3b17c4cc331f12ca5952691de3735a63c1d4c69a2bac';
133 var data = new Buffer('test1');
134 var key = new Buffer('test2');
135 Hash.sha512hmac(data, key).toString('hex').should.equal(hex);
136 });
137
138 });
139
140});