node.js - Typescript Cipher.getAuthTag finns inte
Jag försöker kryptera/dekryptera med hjälp av detta https://github.com/luke-park/SecureCompatibleEncryptionExamples men Cipher-definitionen har inte getAuthTag.
finns det en @types jag ska installera?
REDIGERA Såg precis node/index.d.ts, getAuthTag och setAAD är kommenterade. Jag tycker inte att det är rätt att ta bort kommentaren.
Svar #1
Anledningen till problemet du har är att du definierar algoritmen som en sträng och den borde vara Cipher.CipherGCMTypes.
När du definierar variabeln deklarerar du typen. Som Matt sa, måste du skicka in den som den typen.
private ALGORITHM:crypto.CipherGCMTypes = 'aes-256-gcm';
let cipher:crypto.Cipher = crypto.createCipheriv(this.ALGORITHM, Buffer.from(this.encryptionKeyMessage, 'base64'), iv)
Svar #2
Om du går till deklarationen av Cipher i @types/node, kommer du att märka att den har subgränssnitt som har 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;
}
Och createCipheriv har motsvarande överlagringar: https://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;
Så om du skickar ett algorithm-argument som är känd vid kompileringstid att tillhöra CipherGCMTypes, bör du kunna anropa getAuthTag. Om det inte fungerar, vänligen lägg till din kod i frågan.
