mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-10 09:40:21 +08:00
[node] fix crypto cipher/decipher overloads and typos
This commit is contained in:
32
types/node/index.d.ts
vendored
32
types/node/index.d.ts
vendored
@@ -5903,13 +5903,15 @@ declare module "crypto" {
|
||||
authTagLength?: number;
|
||||
}
|
||||
/** @deprecated since v10.0.0 use createCipheriv() */
|
||||
export function createCipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Cipher;
|
||||
export function createCipher(algorithm: CipherCCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): CipherCCM;
|
||||
export function createCipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherGCMOptions): CipherGCM;
|
||||
/** @deprecated since v10.0.0 use createCipheriv() */
|
||||
export function createCipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): CipherGCM;
|
||||
/** @deprecated since v10.0.0 use createCipheriv() */
|
||||
export function createCipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Cipher;
|
||||
|
||||
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;
|
||||
export function createCipheriv(algorithm: CipherGCMTypes, 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 interface Cipher extends NodeJS.ReadWriteStream {
|
||||
update(data: string | Buffer | NodeJS.TypedArray | DataView): Buffer;
|
||||
@@ -5925,21 +5927,23 @@ declare module "crypto" {
|
||||
// setAAD(buffer: Buffer): this; // docs only say buffer
|
||||
}
|
||||
export interface CipherCCM extends Cipher {
|
||||
setAAD(buffer: Buffer, options: { plainTextLength: number }): this;
|
||||
setAAD(buffer: Buffer, options: { plaintextLength: number }): this;
|
||||
getAuthTag(): Buffer;
|
||||
}
|
||||
export interface CipherGCM extends Cipher {
|
||||
setAAD(buffer: Buffer, options?: { plainTextLength: number }): this;
|
||||
setAAD(buffer: Buffer, options?: { plaintextLength: number }): this;
|
||||
getAuthTag(): Buffer;
|
||||
}
|
||||
/** @deprecated since v10.0.0 use createCipheriv() */
|
||||
export function createDecipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher;
|
||||
export function createDecipher(algorithm: CipherCCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): DecipherCCM;
|
||||
export function createDecipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherGCMOptions): DecipherGCM;
|
||||
/** @deprecated since v10.0.0 use createCipheriv() */
|
||||
export function createDecipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): DecipherGCM;
|
||||
/** @deprecated since v10.0.0 use createCipheriv() */
|
||||
export function createDecipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher;
|
||||
|
||||
export function createDecipheriv(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher;
|
||||
export function createDecipheriv(algorithm: CipherCCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): DecipherCCM;
|
||||
export function createDecipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherGCMOptions): DecipherGCM;
|
||||
export function createDecipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): DecipherGCM;
|
||||
export function createDecipheriv(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher;
|
||||
|
||||
export interface Decipher extends NodeJS.ReadWriteStream {
|
||||
update(data: Buffer | NodeJS.TypedArray | DataView): Buffer;
|
||||
@@ -5954,12 +5958,12 @@ declare module "crypto" {
|
||||
// setAAD(buffer: Buffer | NodeJS.TypedArray | DataView): this;
|
||||
}
|
||||
export interface DecipherCCM extends Decipher {
|
||||
setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView, options: { plainTextLength: number }): this;
|
||||
setAAD(buffer: Buffer | NodeJS.TypedArray | DataView): this;
|
||||
setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView): this;
|
||||
setAAD(buffer: Buffer | NodeJS.TypedArray | DataView, options: { plaintextLength: number }): this;
|
||||
}
|
||||
export interface DecipherGCM extends Decipher {
|
||||
setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView, options?: { plainTextLength: number }): this;
|
||||
setAAD(buffer: Buffer | NodeJS.TypedArray | DataView): this;
|
||||
setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView): this;
|
||||
setAAD(buffer: Buffer | NodeJS.TypedArray | DataView, options?: { plaintextLength: number }): this;
|
||||
}
|
||||
|
||||
export function createSign(algorithm: string, options?: stream.WritableOptions): Signer;
|
||||
|
||||
@@ -1205,8 +1205,7 @@ namespace crypto_tests {
|
||||
{
|
||||
// crypto_cipher_decipher_dataview_test
|
||||
let key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
|
||||
let clearText: DataView = new DataView(
|
||||
new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]).buffer);
|
||||
let clearText: DataView = new DataView(new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]).buffer);
|
||||
let cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
|
||||
let cipherBuffers: Buffer[] = [];
|
||||
cipherBuffers.push(cipher.update(clearText));
|
||||
@@ -1224,6 +1223,56 @@ namespace crypto_tests {
|
||||
assert.deepEqual(clearText2, clearText);
|
||||
}
|
||||
|
||||
{
|
||||
const key = 'keykeykeykeykeykeykeykey';
|
||||
const nonce = crypto.randomBytes(12);
|
||||
const aad = Buffer.from('0123456789', 'hex');
|
||||
|
||||
const cipher = crypto.createCipheriv('aes-192-ccm', key, nonce, {
|
||||
authTagLength: 16
|
||||
});
|
||||
const plaintext = 'Hello world';
|
||||
cipher.setAAD(aad, {
|
||||
plaintextLength: Buffer.byteLength(plaintext)
|
||||
});
|
||||
const ciphertext = cipher.update(plaintext, 'utf8');
|
||||
cipher.final();
|
||||
const tag = cipher.getAuthTag();
|
||||
|
||||
const decipher = crypto.createDecipheriv('aes-192-ccm', key, nonce, {
|
||||
authTagLength: 16
|
||||
});
|
||||
decipher.setAuthTag(tag);
|
||||
decipher.setAAD(aad, {
|
||||
plaintextLength: ciphertext.length
|
||||
});
|
||||
const receivedPlaintext: string = decipher.update(ciphertext, null, 'utf8');
|
||||
decipher.final();
|
||||
}
|
||||
|
||||
{
|
||||
const key = 'keykeykeykeykeykeykeykey';
|
||||
const nonce = crypto.randomBytes(12);
|
||||
const aad = Buffer.from('0123456789', 'hex');
|
||||
|
||||
const cipher = crypto.createCipheriv('aes-192-gcm', key, nonce);
|
||||
const plaintext = 'Hello world';
|
||||
cipher.setAAD(aad, {
|
||||
plaintextLength: Buffer.byteLength(plaintext)
|
||||
});
|
||||
const ciphertext = cipher.update(plaintext, 'utf8');
|
||||
cipher.final();
|
||||
const tag = cipher.getAuthTag();
|
||||
|
||||
const decipher = crypto.createDecipheriv('aes-192-gcm', key, nonce);
|
||||
decipher.setAuthTag(tag);
|
||||
decipher.setAAD(aad, {
|
||||
plaintextLength: ciphertext.length
|
||||
});
|
||||
const receivedPlaintext: string = decipher.update(ciphertext, null, 'utf8');
|
||||
decipher.final();
|
||||
}
|
||||
|
||||
{
|
||||
// crypto_timingsafeequal_buffer_test
|
||||
let buffer1: Buffer = new Buffer([1, 2, 3, 4, 5]);
|
||||
|
||||
Reference in New Issue
Block a user