|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +if (!common.hasCrypto) { |
| 6 | + common.skip('missing crypto'); |
| 7 | + return; |
| 8 | +} |
| 9 | + |
| 10 | +const crypto = require('crypto'); |
| 11 | + |
| 12 | +const UTF_INPUT = 'öäü'; |
| 13 | +const TEST_CASES = [ |
| 14 | + { |
| 15 | + options: undefined, |
| 16 | + // Hash of "öäü" in default format (utf8) |
| 17 | + output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c', |
| 18 | + }, |
| 19 | + { |
| 20 | + options: {}, |
| 21 | + // Hash of "öäü" in default format (utf8) |
| 22 | + output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c', |
| 23 | + }, |
| 24 | + { |
| 25 | + options: { |
| 26 | + defaultEncoding: 'latin1', |
| 27 | + }, |
| 28 | + // Hash of "öäü" in latin1 format |
| 29 | + output: 'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830', |
| 30 | + }, |
| 31 | + { |
| 32 | + options: { |
| 33 | + defaultEncoding: 'utf8', |
| 34 | + }, |
| 35 | + // Hash of "öäü" in utf8 format |
| 36 | + output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c', |
| 37 | + }, |
| 38 | + { |
| 39 | + options: { |
| 40 | + defaultEncoding: 'ascii', |
| 41 | + }, |
| 42 | + // Hash of "öäü" in ascii format |
| 43 | + output: 'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830', |
| 44 | + }, |
| 45 | +]; |
| 46 | + |
| 47 | +for (const test of TEST_CASES) { |
| 48 | + const hashValue = crypto.createHash('sha256') |
| 49 | + .update(UTF_INPUT, test.options && test.options.defaultEncoding) |
| 50 | + .digest('hex'); |
| 51 | + |
| 52 | + assert.equal(hashValue, test.output); |
| 53 | +} |
| 54 | + |
| 55 | +for (const test of TEST_CASES) { |
| 56 | + const hash = crypto.createHash('sha256', test.options); |
| 57 | + let hashValue = ''; |
| 58 | + |
| 59 | + hash.on('data', (data) => { |
| 60 | + hashValue += data.toString('hex'); |
| 61 | + }); |
| 62 | + |
| 63 | + hash.on('end', common.mustCall(() => { |
| 64 | + assert.equal(hashValue, test.output); |
| 65 | + })); |
| 66 | + |
| 67 | + hash.write(UTF_INPUT); |
| 68 | + hash.end(); |
| 69 | +} |
0 commit comments