Typescript Cipher.getAuthTag不存在

问题描述 投票:1回答:1

我正在尝试使用此https://github.com/luke-park/SecureCompatibleEncryptionExamples进行加密/解密,但Cipher定义没有getAuthTag

我应该安装@types吗?

编辑刚看到node / index.d.ts,getAuthTagsetAAD被评论。我不认为删除评论是正确的。

node.js typescript encryption
1个回答
1
投票

如果你去the declaration of Cipher in @types/node,你会注意到它有子接口有getAuthTag

    export interface Cipher extends NodeJS.ReadWriteStream {
        update(data: string | Buffer | NodeJS.TypedArray | DataView): Buffer;
        update(data: string, input_encoding: Utf8AsciiBinaryEncoding): Buffer;
        update(data: Buffer | NodeJS.TypedArray | DataView, output_encoding: HexBase64BinaryEncoding): string;
        update(data: Buffer | NodeJS.TypedArray | DataView, input_encoding: any, output_encoding: HexBase64BinaryEncoding): string;
        // second arg ignored
        update(data: string, input_encoding: Utf8AsciiBinaryEncoding, output_encoding: HexBase64BinaryEncoding): string;
        final(): Buffer;
        final(output_encoding: string): string;
        setAutoPadding(auto_padding?: boolean): this;
        // getAuthTag(): Buffer;
        // setAAD(buffer: Buffer): this; // docs only say buffer
    }
    export interface CipherCCM extends Cipher {
        setAAD(buffer: Buffer, options: { plaintextLength: number }): this;
        getAuthTag(): Buffer;
    }
    export interface CipherGCM extends Cipher {
        setAAD(buffer: Buffer, options?: { plaintextLength: number }): this;
        getAuthTag(): Buffer;
}

createCipheriv has corresponding overloadshttps://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts#L5914-L5916

export function createCipheriv(algorithm: CipherCCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): CipherCCM;
export function createCipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): CipherGCM;
export function createCipheriv(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Cipher;

因此,如果你传递一个在编译时已知的algorithm参数属于CipherGCMTypes,那么你应该能够调用getAuthTag。如果它不起作用,请将您的代码添加到问题中。

© www.soinside.com 2019 - 2024. All rights reserved.