• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24if (!common.hasCrypto)
25  common.skip('missing crypto');
26
27const assert = require('assert');
28const stream = require('stream');
29const crypto = require('crypto');
30
31if (!common.hasFipsCrypto) {
32  // Small stream to buffer converter
33  class Stream2buffer extends stream.Writable {
34    constructor(callback) {
35      super();
36
37      this._buffers = [];
38      this.once('finish', function() {
39        callback(null, Buffer.concat(this._buffers));
40      });
41    }
42
43    _write(data, encoding, done) {
44      this._buffers.push(data);
45      return done(null);
46    }
47  }
48
49  // Create an md5 hash of "Hallo world"
50  const hasher1 = crypto.createHash('md5');
51  hasher1.pipe(new Stream2buffer(common.mustCall(function end(err, hash) {
52    assert.strictEqual(err, null);
53    assert.strictEqual(
54      hash.toString('hex'), '06460dadb35d3d503047ce750ceb2d07'
55    );
56  })));
57  hasher1.end('Hallo world');
58
59  // Simpler check for unpipe, setEncoding, pause and resume
60  crypto.createHash('md5').unpipe({});
61  crypto.createHash('md5').setEncoding('utf8');
62  crypto.createHash('md5').pause();
63  crypto.createHash('md5').resume();
64}
65
66// Decipher._flush() should emit an error event, not an exception.
67const key = Buffer.from('48fb56eb10ffeb13fc0ef551bbca3b1b', 'hex');
68const badkey = Buffer.from('12341234123412341234123412341234', 'hex');
69const iv = Buffer.from('6d358219d1f488f5f4eb12820a66d146', 'hex');
70const cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
71const decipher = crypto.createDecipheriv('aes-128-cbc', badkey, iv);
72
73cipher.pipe(decipher)
74  .on('error', common.expectsError({
75    message: /bad decrypt/,
76    function: 'EVP_DecryptFinal_ex',
77    library: 'digital envelope routines',
78    reason: 'bad decrypt',
79  }));
80
81cipher.end('Papaya!');  // Should not cause an unhandled exception.
82