|
| 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',''), |
| 15 | +'7f9c2ba4e88f827d616045507605853e' |
| 16 | +); |
| 17 | + |
| 18 | +assert.strictEqual( |
| 19 | +crypto.hash('shake256',''), |
| 20 | +'46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f' |
| 21 | +); |
| 22 | + |
| 23 | +// outputEncoding as an option. |
| 24 | +assert.strictEqual( |
| 25 | +crypto.hash('shake128','',{outputEncoding: 'base64url'}), |
| 26 | +'f5wrpOiPgn1hYEVQdgWFPg' |
| 27 | +); |
| 28 | + |
| 29 | +assert.strictEqual( |
| 30 | +crypto.hash('shake256','',{outputEncoding: 'base64url'}), |
| 31 | +'RrndKwuojRMjOz_rdD7rJD_NUupiuBuCtQwnZG7Vdi8' |
| 32 | +); |
| 33 | + |
| 34 | +assert.deepStrictEqual( |
| 35 | +crypto.hash('shake128','',{outputEncoding: 'buffer'}), |
| 36 | +Buffer.from('f5wrpOiPgn1hYEVQdgWFPg','base64url') |
| 37 | +); |
| 38 | + |
| 39 | +assert.deepStrictEqual( |
| 40 | +crypto.hash('shake256','',{outputEncoding: 'buffer'}), |
| 41 | +Buffer.from('RrndKwuojRMjOz_rdD7rJD_NUupiuBuCtQwnZG7Vdi8','base64url') |
| 42 | +); |
| 43 | + |
| 44 | +// Short outputLengths. |
| 45 | +assert.strictEqual(crypto.hash('shake128','',{outputLength: 0}),''); |
| 46 | +assert.deepStrictEqual(crypto.hash('shake128','',{outputEncoding: 'buffer',outputLength: 0}), |
| 47 | +Buffer.alloc(0)); |
| 48 | + |
| 49 | +assert.strictEqual( |
| 50 | +crypto.hash('shake128','',{outputLength: 5}), |
| 51 | +crypto.createHash('shake128',{outputLength: 5}).update('').digest('hex') |
| 52 | +); |
| 53 | +// Check length |
| 54 | +assert.strictEqual( |
| 55 | +crypto.hash('shake128','',{outputLength: 5}).length, |
| 56 | +crypto.createHash('shake128',{outputLength: 5}).update('').digest('hex') |
| 57 | +.length |
| 58 | +); |
| 59 | + |
| 60 | +assert.strictEqual( |
| 61 | +crypto.hash('shake128','',{outputLength: 15}), |
| 62 | +crypto.createHash('shake128',{outputLength: 15}).update('').digest('hex') |
| 63 | +); |
| 64 | +// Check length |
| 65 | +assert.strictEqual( |
| 66 | +crypto.hash('shake128','',{outputLength: 15}).length, |
| 67 | +crypto.createHash('shake128',{outputLength: 15}).update('').digest('hex') |
| 68 | +.length |
| 69 | +); |
| 70 | + |
| 71 | +assert.strictEqual( |
| 72 | +crypto.hash('shake256','',{outputLength: 16}), |
| 73 | +crypto.createHash('shake256',{outputLength: 16}).update('').digest('hex') |
| 74 | +); |
| 75 | +// Check length |
| 76 | +assert.strictEqual( |
| 77 | +crypto.hash('shake256','',{outputLength: 16}).length, |
| 78 | +crypto.createHash('shake256',{outputLength: 16}).update('').digest('hex') |
| 79 | +.length |
| 80 | +); |
| 81 | + |
| 82 | +// Large outputLengths. |
| 83 | +assert.strictEqual( |
| 84 | +crypto.hash('shake128','',{outputLength: 128}), |
| 85 | +crypto |
| 86 | +.createHash('shake128',{outputLength: 128}).update('') |
| 87 | +.digest('hex') |
| 88 | +); |
| 89 | +// Check length without encoding |
| 90 | +assert.strictEqual( |
| 91 | +crypto.hash('shake128','',{outputLength: 128}).length, |
| 92 | +crypto |
| 93 | +.createHash('shake128',{outputLength: 128}).update('') |
| 94 | +.digest('hex').length |
| 95 | +); |
| 96 | +assert.strictEqual( |
| 97 | +crypto.hash('shake256','',{outputLength: 128}), |
| 98 | +crypto |
| 99 | +.createHash('shake256',{outputLength: 128}).update('') |
| 100 | +.digest('hex') |
| 101 | +); |
| 102 | + |
| 103 | +constactual=crypto.hash('shake256','The message is shorter than the hash!',{outputLength: 1024*1024}); |
| 104 | +constexpected=crypto |
| 105 | +.createHash('shake256',{ |
| 106 | +outputLength: 1024*1024, |
| 107 | +}) |
| 108 | +.update('The message is shorter than the hash!') |
| 109 | +.digest('hex'); |
| 110 | +assert.strictEqual(actual,expected); |
| 111 | + |
| 112 | +// Non-XOF hash functions should accept valid outputLength options as well. |
| 113 | +assert.strictEqual(crypto.hash('sha224','',{outputLength: 28}), |
| 114 | +'d14a028c2a3a2bc9476102bb288234c4'+ |
| 115 | +'15a2b01f828ea62ac5b3e42f'); |
| 116 | + |
| 117 | +// Non-XOF hash functions should fail when outputLength isn't their actual outputLength |
| 118 | +assert.throws(()=>crypto.hash('sha224','',{outputLength: 32}), |
| 119 | +{message: 'Output length 32 is invalid for sha224, which does not support XOF'}); |
| 120 | +assert.throws(()=>crypto.hash('sha224','',{outputLength: 0}), |
| 121 | +{message: 'Output length 0 is invalid for sha224, which does not support XOF'}); |
| 122 | +} |
0 commit comments