|
| 1 | +var test = require('tape') |
| 2 | +var Buffer = require('buffer').Buffer |
| 3 | + |
| 4 | +var crypto = require('../') |
| 5 | + |
| 6 | +var algorithms = ['sha1', 'sha256', 'md5']; |
| 7 | +var encodings = ['binary', 'hex', 'base64']; |
| 8 | + |
| 9 | + |
| 10 | +// We can't compare against node's crypto library directly because when |
| 11 | +// using testling we only have another version of crypto-browserify to |
| 12 | +// check against. So we'll use a cached version of the expected values |
| 13 | +// generated by node crypto. |
| 14 | +var EXPECTED = {}; |
| 15 | + |
| 16 | +EXPECTED['sha1-hash-binary'] = atob('qvTGHdzF6KLavt4PO0gs2a6pQ00='); |
| 17 | +EXPECTED['sha1-hash-hex'] = 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'; |
| 18 | +EXPECTED['sha1-hash-base64'] = 'qvTGHdzF6KLavt4PO0gs2a6pQ00='; |
| 19 | + |
| 20 | +EXPECTED['sha256-hash-binary'] = atob('LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ='); |
| 21 | +EXPECTED['sha256-hash-hex'] = '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'; |
| 22 | +EXPECTED['sha256-hash-base64'] = 'LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ='; |
| 23 | + |
| 24 | +EXPECTED['md5-hash-binary'] = atob('XUFAKrxLKna5cZ2REBfFkg=='); |
| 25 | +EXPECTED['md5-hash-hex'] = '5d41402abc4b2a76b9719d911017c592'; |
| 26 | +EXPECTED['md5-hash-base64'] = 'XUFAKrxLKna5cZ2REBfFkg=='; |
| 27 | + |
| 28 | +EXPECTED['sha1-hmac-binary'] = atob('URIFXAX5RPhXVe/FzYlw4ZTp9Fs='); |
| 29 | +EXPECTED['sha1-hmac-hex'] = '5112055c05f944f85755efc5cd8970e194e9f45b'; |
| 30 | +EXPECTED['sha1-hmac-base64'] = 'URIFXAX5RPhXVe/FzYlw4ZTp9Fs='; |
| 31 | + |
| 32 | +EXPECTED['sha256-hmac-binary'] = atob('iKqz7ejTrflNJquQ07r9SiCDBww7zOnAFO4EpEOEfAs='); |
| 33 | +EXPECTED['sha256-hmac-hex'] = '88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b'; |
| 34 | +EXPECTED['sha256-hmac-base64'] = 'iKqz7ejTrflNJquQ07r9SiCDBww7zOnAFO4EpEOEfAs='; |
| 35 | + |
| 36 | +EXPECTED['md5-hmac-binary'] = atob('ut5jhjxh7QsxZYBuzWrO/A=='); |
| 37 | +EXPECTED['md5-hmac-hex'] = 'bade63863c61ed0b3165806ecd6acefc'; |
| 38 | +EXPECTED['md5-hmac-base64'] = 'ut5jhjxh7QsxZYBuzWrO/A=='; |
| 39 | + |
| 40 | +algorithms.forEach(function (algorithm) { |
| 41 | + encodings.forEach(function (encoding) { |
| 42 | + test(algorithm + ' hash using ' + encoding, function (t) { |
| 43 | + t.plan(1); |
| 44 | + var actual = crypto.createHash(algorithm).update('hello', 'utf-8').digest(encoding); |
| 45 | + var expected = EXPECTED[algorithm + '-hash-' + encoding]; |
| 46 | + t.equal(actual, expected); |
| 47 | + t.end(); |
| 48 | + }); |
| 49 | + |
| 50 | + test(algorithm + ' hmac using ' + encoding, function (t) { |
| 51 | + t.plan(1); |
| 52 | + var actual = crypto.createHmac(algorithm, 'secret').update('hello', 'utf-8').digest(encoding); |
| 53 | + var expected = EXPECTED[algorithm + '-hmac-' + encoding]; |
| 54 | + t.equal(actual, expected); |
| 55 | + t.end(); |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +test('randomBytes', function (t) { |
| 61 | + t.plan(5) |
| 62 | + t.equal(crypto.randomBytes(10).length, 10) |
| 63 | + t.ok(crypto.randomBytes(10) instanceof Buffer) |
| 64 | + crypto.randomBytes(10, function(ex, bytes) { |
| 65 | + t.error(ex) |
| 66 | + t.equal(bytes.length, 10) |
| 67 | + t.ok(bytes instanceof Buffer) |
| 68 | + t.end() |
| 69 | + }) |
| 70 | +}) |
0 commit comments