Definitelytyped: [@types/node] createDecipheriv should accept CipherKey

Created on 5 Sep 2019  路  3Comments  路  Source: DefinitelyTyped/DefinitelyTyped

Authors: @jkomyno @a-tarasyuk @alvis @r3nya @btoueg @brunoscheufler @smac89 @tellnes @touffy @DeividasBakanas @eyqs @Flarna @Hannes-Magnusson-CK @KSXGitHub @hoo29 @kjin @ajafff @islishude @mwiktorczyk @matthieusieben @mohsen1 @n-e @octo-sniffle @parambirs @eps1lon @SimonSchick @ThomasdenH @WilcoBakker @wwwy3y3 @ZaneHannanAU @samuela @kuehlein @j-oliveras @bhongy @chyzwar

createCipheriv accepts CipherKey (BinaryLike | KeyObject), but createDecipheriv only accepts BinaryLike. I have confirmed from testing that createDecipheriv also accepts KeyObjects, so I believe these definitions should be changed to match createCipheriv.

Code which works at runtime:

import crypto from 'crypto';

const key = crypto.createSecretKey(crypto.randomBytes(32)); // KeyObject
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update('Something', 'utf8');
encrypted = Buffer.concat([encrypted, cipher.final()]);

const decipher = crypto.createDecipheriv('aes-256-cbc', key as any, iv); // note 'as any' is needed here
let decrypted = decipher.update(encrypted, undefined, 'utf8');
decrypted += decipher.final('utf8');

Relevant updated declarations:

    function createDecipheriv(
        algorithm: CipherCCMTypes,
        key: CipherKey,
        iv: BinaryLike | null,
        options: CipherCCMOptions,
    ): DecipherCCM;
    function createDecipheriv(
        algorithm: CipherGCMTypes,
        key: CipherKey,
        iv: BinaryLike | null,
        options?: CipherGCMOptions,
    ): DecipherGCM;
    function createDecipheriv(algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions): Decipher;

I can't say for sure that this is fine for all algorithms, but it seems likely.

Most helpful comment

This issue also exists for the createHmac function. createHmac should accept BinaryLike | KeyObject, not just BinaryLike.

All 3 comments

This issue also exists for the createHmac function. createHmac should accept BinaryLike | KeyObject, not just BinaryLike.

+1

This has been resolved by #42604

Was this page helpful?
0 / 5 - 0 ratings