|
| 1 | +'use strict'; |
| 2 | +// This tests crypto.hash() works. |
| 3 | +constcommon=require('../common'); |
| 4 | + |
| 5 | +if(!common.hasCrypto)common.skip('missing crypto'); |
| 6 | + |
| 7 | +constassert=require('assert'); |
| 8 | +constcrypto=require('crypto'); |
| 9 | + |
| 10 | +// Test XOF hash functions and the outputLength option. |
| 11 | +{ |
| 12 | +// Default outputLengths. |
| 13 | +assert.strictEqual( |
| 14 | +crypto.hash('shake128','','hex'), |
| 15 | +crypto.createHash('shake128').update('').digest('hex') |
| 16 | +); |
| 17 | + |
| 18 | +assert.strictEqual( |
| 19 | +crypto.hash('shake256','','hex'), |
| 20 | +crypto.createHash('shake256').update('').digest('hex') |
| 21 | +); |
| 22 | + |
| 23 | +// Short outputLengths. |
| 24 | +assert.strictEqual(crypto.hash('shake128','',{encoding: 'hex',outputLength: 0}), |
| 25 | +crypto.createHash('shake128',{outputLength: 0}) |
| 26 | +.update('').digest('hex')); |
| 27 | + |
| 28 | +assert.strictEqual( |
| 29 | +crypto.hash('shake128','',{outputEncoding: 'hex',outputLength: 5}), |
| 30 | +crypto.createHash('shake128',{outputLength: 5}).update('').digest('hex') |
| 31 | +); |
| 32 | +// Check length |
| 33 | +assert.strictEqual( |
| 34 | +crypto.hash('shake128','',{outputEncoding: 'hex',outputLength: 5}).length, |
| 35 | +crypto.createHash('shake128',{outputLength: 5}).update('').digest('hex') |
| 36 | +.length |
| 37 | +); |
| 38 | + |
| 39 | +assert.strictEqual( |
| 40 | +crypto.hash('shake128','',{outputEncoding: 'hex',outputLength: 15}), |
| 41 | +crypto.createHash('shake128',{outputLength: 15}).update('').digest('hex') |
| 42 | +); |
| 43 | +// Check length |
| 44 | +assert.strictEqual( |
| 45 | +crypto.hash('shake128','',{outputEncoding: 'hex',outputLength: 15}).length, |
| 46 | +crypto.createHash('shake128',{outputLength: 15}).update('').digest('hex') |
| 47 | +.length |
| 48 | +); |
| 49 | + |
| 50 | +assert.strictEqual( |
| 51 | +crypto.hash('shake256','',{outputEncoding: 'hex',outputLength: 16}), |
| 52 | +crypto.createHash('shake256',{outputLength: 16}).update('').digest('hex') |
| 53 | +); |
| 54 | +// Check length |
| 55 | +assert.strictEqual( |
| 56 | +crypto.hash('shake256','',{outputEncoding: 'hex',outputLength: 16}).length, |
| 57 | +crypto.createHash('shake256',{outputLength: 16}).update('').digest('hex') |
| 58 | +.length |
| 59 | +); |
| 60 | + |
| 61 | +// Large outputLengths. |
| 62 | +assert.strictEqual( |
| 63 | +crypto.hash('shake128','',{outputEncoding: 'hex',outputLength: 128}), |
| 64 | +crypto |
| 65 | +.createHash('shake128',{outputLength: 128}).update('') |
| 66 | +.digest('hex') |
| 67 | +); |
| 68 | +// Check length without encoding |
| 69 | +assert.strictEqual( |
| 70 | +crypto.hash('shake128','',{outputLength: 128}).length, |
| 71 | +crypto |
| 72 | +.createHash('shake128',{outputLength: 128}).update('') |
| 73 | +.digest('hex').length |
| 74 | +); |
| 75 | +assert.strictEqual( |
| 76 | +crypto.hash('shake256','',{outputLength: 128}), |
| 77 | +crypto |
| 78 | +.createHash('shake256',{outputLength: 128}).update('') |
| 79 | +.digest('hex') |
| 80 | +); |
| 81 | + |
| 82 | +constactual=crypto.hash('shake256','The message is shorter than the hash!',{outputLength: 1024*1024}); |
| 83 | +constexpected=crypto |
| 84 | +.createHash('shake256',{ |
| 85 | +outputLength: 1024*1024, |
| 86 | +}) |
| 87 | +.update('The message is shorter than the hash!') |
| 88 | +.digest('hex'); |
| 89 | +assert.strictEqual(actual,expected); |
| 90 | + |
| 91 | +// Non-XOF hash functions should accept valid outputLength options as well. |
| 92 | +assert.strictEqual(crypto.hash('sha224','','hex',28), |
| 93 | +'d14a028c2a3a2bc9476102bb288234c4'+ |
| 94 | +'15a2b01f828ea62ac5b3e42f'); |
| 95 | +} |
0 commit comments