Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,11 @@ console.log(aliceSecret === bobSecret);

<!-- YAML
added: v0.1.92
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67490
description: Calling the `Hash` constructor directly now throws an
`ERR_ILLEGAL_CONSTRUCTOR` error.
-->

* Extends: {stream.Transform}
Expand Down Expand Up @@ -2096,6 +2101,11 @@ This can be called many times with new data as it is streamed.

<!-- YAML
added: v0.1.94
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67490
description: Calling the `Hmac` constructor directly now throws an
`ERR_ILLEGAL_CONSTRUCTOR` error.
-->

* Extends: {stream.Transform}
Expand Down
26 changes: 18 additions & 8 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4031,6 +4031,9 @@ npx codemod@latest @nodejs/dirent-path-to-parent-path

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67490
description: End-of-Life.
- version: v22.0.0
pr-url: https://github.com/nodejs/node/pull/51880
description: Runtime deprecation.
Expand All @@ -4041,11 +4044,13 @@ changes:
description: Documentation-only deprecation.
-->

Type: Runtime
Type: End-of-Life

Calling `Hash` class directly with `Hash()` or `new Hash()` is
deprecated due to being internals, not intended for public use.
Please use the [`crypto.createHash()`][] method to create Hash instances.
Calling `Hash` class directly with `Hash()` or `new Hash()` throws an
`ERR_ILLEGAL_CONSTRUCTOR` error. The constructor is internal and not intended
for public use. Use the [`crypto.createHash()`][] method to create Hash
instances. The class remains exported, so `instanceof crypto.Hash` checks
continue to work.

### DEP0180: `fs.Stats` constructor

Expand All @@ -4068,6 +4073,9 @@ deprecated due to being internals, not intended for public use.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67490
description: End-of-Life.
- version: v22.0.0
pr-url: https://github.com/nodejs/node/pull/52071
description: Runtime deprecation.
Expand All @@ -4076,11 +4084,13 @@ changes:
description: Documentation-only deprecation.
-->

Type: Runtime
Type: End-of-Life

Calling `Hmac` class directly with `Hmac()` or `new Hmac()` is
deprecated due to being internals, not intended for public use.
Please use the [`crypto.createHmac()`][] method to create Hmac instances.
Calling `Hmac` class directly with `Hmac()` or `new Hmac()` throws an
`ERR_ILLEGAL_CONSTRUCTOR` error. The constructor is internal and not intended
for public use. Use the [`crypto.createHmac()`][] method to create Hmac
instances. The class remains exported, so `instanceof crypto.Hmac` checks
continue to work.

### DEP0182: Short GCM authentication tags without explicit `authTagLength`

Expand Down
14 changes: 4 additions & 10 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ const {
const {
Hash,
Hmac,
createHash,
createHmac,
hash,
} = require('internal/crypto/hash');
const {
Expand Down Expand Up @@ -160,10 +162,6 @@ function lazyOwnsProcessState() {

// These helper functions are needed because the constructors can
// use new, in which case V8 cannot inline the recursive constructor call
function createHash(algorithm, options) {
return new Hash(algorithm, options);
}

function createCipheriv(cipher, key, iv, options) {
return new Cipheriv(cipher, key, iv, options);
}
Expand All @@ -184,10 +182,6 @@ function createECDH(curve) {
return new ECDH(curve);
}

function createHmac(hmac, key, options) {
return new Hmac(hmac, key, options);
}

function createMac(algorithm, key, options) {
return new Mac(algorithm, key, options);
}
Expand Down Expand Up @@ -267,8 +261,8 @@ module.exports = {
DiffieHellman,
DiffieHellmanGroup,
ECDH,
Hash: deprecate(Hash, 'crypto.Hash constructor is deprecated.', 'DEP0179'),
Hmac: deprecate(Hmac, 'crypto.Hmac constructor is deprecated.', 'DEP0181'),
Hash,
Hmac,
KeyObject,
Sign,
Verify,
Expand Down
26 changes: 19 additions & 7 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const {
codes: {
ERR_CRYPTO_HASH_FINALIZED,
ERR_CRYPTO_HASH_UPDATE_FAILED,
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_UNKNOWN_ENCODING,
Expand All @@ -71,6 +72,7 @@ const LazyTransform = require('internal/streams/lazy_transform');

const kState = Symbol('kState');
const kFinalized = Symbol('kFinalized');
const kPrivateConstructor = Symbol('kPrivateConstructor');

const emitHmacDigestDeprecation = getDeprecationWarningEmitter(
'DEP0206',
Expand All @@ -88,9 +90,9 @@ function normalizeCShakeParameter(value, name) {
return value;
}

function Hash(algorithm, options) {
if (!new.target)
return new Hash(algorithm, options);
function Hash(privateSymbol, algorithm, options) {
if (privateSymbol !== kPrivateConstructor)
throw new ERR_ILLEGAL_CONSTRUCTOR();
const isCopy = algorithm instanceof _Hash;
if (!isCopy)
validateString(algorithm, 'algorithm');
Expand Down Expand Up @@ -145,7 +147,7 @@ Hash.prototype.copy = function copy(options) {
if (state[kFinalized])
throw new ERR_CRYPTO_HASH_FINALIZED();

return new Hash(this[kHandle], options);
return new Hash(kPrivateConstructor, this[kHandle], options);
};

Hash.prototype._transform = function _transform(chunk, encoding, callback) {
Expand Down Expand Up @@ -198,9 +200,9 @@ Hash.prototype.digest = function digest(outputEncoding) {
return ret;
};

function Hmac(hmac, key, options) {
if (!(this instanceof Hmac))
return new Hmac(hmac, key, options);
function Hmac(privateSymbol, hmac, key, options) {
if (privateSymbol !== kPrivateConstructor)
throw new ERR_ILLEGAL_CONSTRUCTOR();
validateString(hmac, 'hmac');
const encoding = getStringOption(options, 'encoding');
key = prepareSecretKey(key, encoding);
Expand Down Expand Up @@ -241,6 +243,14 @@ Hmac.prototype._flush = function _flush(callback) {
};
Hmac.prototype._transform = Hash.prototype._transform;

function createHash(algorithm, options) {
return new Hash(kPrivateConstructor, algorithm, options);
}

function createHmac(hmac, key, options) {
return new Hmac(kPrivateConstructor, hmac, key, options);
}

// Implementation for WebCrypto subtle.digest()

function asyncDigest(algorithm, data) {
Expand Down Expand Up @@ -382,5 +392,7 @@ module.exports = {
Hash,
Hmac,
asyncDigest,
createHash,
createHmac,
hash,
};
19 changes: 0 additions & 19 deletions test/parallel/test-crypto-dep0181.js

This file was deleted.

28 changes: 28 additions & 0 deletions test/parallel/test-crypto-hash-hmac-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const crypto = require('crypto');

// The Hash and Hmac constructors are not intended for public use. Instances
// must be created with crypto.createHash() and crypto.createHmac().
const illegalConstructor = {
code: 'ERR_ILLEGAL_CONSTRUCTOR',
name: 'TypeError',
};

assert.throws(() => crypto.Hash('sha256'), illegalConstructor);
assert.throws(() => new crypto.Hash('sha256'), illegalConstructor);
assert.throws(() => crypto.Hmac('sha256', 'key'), illegalConstructor);
assert.throws(() => new crypto.Hmac('sha256', 'key'), illegalConstructor);

// The classes remain exported so that instanceof checks keep working.
{
const hash = crypto.createHash('sha256');
assert(hash instanceof crypto.Hash);
assert(hash.copy() instanceof crypto.Hash);
assert(crypto.createHmac('sha256', 'key') instanceof crypto.Hmac);
}
18 changes: 0 additions & 18 deletions test/parallel/test-crypto-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ if (!common.hasCrypto) {
common.skip('missing crypto');
}

common.expectWarning({
DeprecationWarning: [
['crypto.Hash constructor is deprecated.',
'DEP0179'],
]
});

const assert = require('assert');
const crypto = require('crypto');
const fs = require('fs');
Expand Down Expand Up @@ -183,13 +176,6 @@ assert.throws(
}
);

{
const Hash = crypto.Hash;
const instance = crypto.Hash('sha256');
assert(instance instanceof Hash, 'Hash is expected to return a new instance' +
' when called without `new`');
}

// Test XOF hash functions and the outputLength option.
if (!isBoringSSL) {
const invalidXofLength = {
Expand Down Expand Up @@ -301,7 +287,3 @@ if (!isBoringSSL) {
assert.strictEqual(a.digest('hex'), b.digest('hex'));
assert.strictEqual(c.digest('hex'), d.digest('hex'));
}

{
crypto.Hash('sha256');
}
Loading