feat(node): add crypto KeyObject support

This commit is contained in:
Simon Schick 2019-02-11 09:03:21 -08:00
parent c8dadc971b
commit 4d690da422
7 changed files with 622 additions and 554 deletions

View File

@ -8,8 +8,8 @@
/** This library provides IRC client functionality. */
import events = require('events');
import crypto = require('crypto');
import net = require('net');
import tls = require('tls');
/** This library provides IRC client functionality. */
declare namespace NodeIRC {
@ -287,7 +287,7 @@ declare namespace NodeIRC {
* Should SSL be used? Can either be true or crypto credentials.
* @default false
*/
secure?: boolean | crypto.Credentials;
secure?: boolean | tls.SecureContext;
/**
* Should we accept self-signed certificates?

200
types/node/crypto.d.ts vendored
View File

@ -2,9 +2,9 @@ declare module "crypto" {
import * as stream from "stream";
interface Certificate {
exportChallenge(spkac: string | Buffer | NodeJS.TypedArray | DataView): Buffer;
exportPublicKey(spkac: string | Buffer | NodeJS.TypedArray | DataView): Buffer;
verifySpkac(spkac: Buffer | NodeJS.TypedArray | DataView): boolean;
exportChallenge(spkac: BinaryLike): Buffer;
exportPublicKey(spkac: BinaryLike): Buffer;
verifySpkac(spkac: Binary): boolean;
}
const Certificate: {
new(): Certificate;
@ -14,9 +14,8 @@ declare module "crypto" {
/** @deprecated since v10.0.0 */
const fips: boolean;
interface Credentials { context?: any; }
function createHash(algorithm: string, options?: stream.TransformOptions): Hash;
function createHmac(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Hmac;
function createHmac(algorithm: string, key: BinaryLike, options?: stream.TransformOptions): Hmac;
type Utf8AsciiLatin1Encoding = "utf8" | "ascii" | "latin1";
type HexBase64Latin1Encoding = "latin1" | "hex" | "base64";
@ -25,19 +24,40 @@ declare module "crypto" {
type ECDHKeyFormat = "compressed" | "uncompressed" | "hybrid";
interface Hash extends NodeJS.ReadWriteStream {
update(data: string | Buffer | NodeJS.TypedArray | DataView): Hash;
update(data: BinaryLike): Hash;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hash;
digest(): Buffer;
digest(encoding: HexBase64Latin1Encoding): string;
}
interface Hmac extends NodeJS.ReadWriteStream {
update(data: string | Buffer | NodeJS.TypedArray | DataView): Hmac;
update(data: BinaryLike): Hmac;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hmac;
digest(): Buffer;
digest(encoding: HexBase64Latin1Encoding): string;
}
export type KeyObjectType = 'secret' | 'public' | 'private';
interface KeyObject {
asymmetricKeyType?: KeyType;
export(options?: {
type: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1',
format: KeyFormat,
cipher?: string,
passphrase?: string | Buffer
}): string | Buffer;
symmetricSize?: number;
type: KeyObjectType;
}
type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm';
type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
type Binary = Buffer | NodeJS.TypedArray | DataView;
type BinaryLike = string | Binary;
type CipherKey = BinaryLike | KeyObject;
interface CipherCCMOptions extends stream.TransformOptions {
authTagLength: number;
}
@ -45,21 +65,33 @@ declare module "crypto" {
authTagLength?: number;
}
/** @deprecated since v10.0.0 use createCipheriv() */
function createCipher(algorithm: CipherCCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): CipherCCM;
function createCipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): CipherCCM;
/** @deprecated since v10.0.0 use createCipheriv() */
function createCipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): CipherGCM;
function createCipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): CipherGCM;
/** @deprecated since v10.0.0 use createCipheriv() */
function createCipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Cipher;
function createCipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Cipher;
function createCipheriv(algorithm: CipherCCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): CipherCCM;
function createCipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): CipherGCM;
function createCipheriv(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Cipher;
function createCipheriv(
algorithm: CipherCCMTypes,
key: CipherKey,
iv: BinaryLike,
options: CipherCCMOptions
): CipherCCM;
function createCipheriv(
algorithm: CipherGCMTypes,
key: CipherKey,
iv: BinaryLike,
options?: CipherGCMOptions
): CipherGCM;
function createCipheriv(
algorithm: string, key: CipherKey, iv: BinaryLike, options?: stream.TransformOptions
): Cipher;
interface Cipher extends NodeJS.ReadWriteStream {
update(data: string | Buffer | NodeJS.TypedArray | DataView): Buffer;
update(data: BinaryLike): 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;
update(data: Binary, output_encoding: HexBase64BinaryEncoding): string;
update(data: Binary, input_encoding: any, output_encoding: HexBase64BinaryEncoding): string;
// second arg ignored
update(data: string, input_encoding: Utf8AsciiBinaryEncoding, output_encoding: HexBase64BinaryEncoding): string;
final(): Buffer;
@ -77,74 +109,100 @@ declare module "crypto" {
getAuthTag(): Buffer;
}
/** @deprecated since v10.0.0 use createCipheriv() */
function createDecipher(algorithm: CipherCCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): DecipherCCM;
function createDecipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): DecipherCCM;
/** @deprecated since v10.0.0 use createCipheriv() */
function createDecipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): DecipherGCM;
function createDecipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): DecipherGCM;
/** @deprecated since v10.0.0 use createCipheriv() */
function createDecipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher;
function createDecipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Decipher;
function createDecipheriv(
algorithm: CipherCCMTypes,
key: string | Buffer | NodeJS.TypedArray | DataView,
iv: string | Buffer | NodeJS.TypedArray | DataView,
key: BinaryLike,
iv: BinaryLike,
options: CipherCCMOptions,
): DecipherCCM;
function createDecipheriv(
algorithm: CipherGCMTypes,
key: string | Buffer | NodeJS.TypedArray | DataView,
iv: string | Buffer | NodeJS.TypedArray | DataView,
key: BinaryLike,
iv: BinaryLike,
options?: CipherGCMOptions,
): DecipherGCM;
function createDecipheriv(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher;
function createDecipheriv(algorithm: string, key: BinaryLike, iv: BinaryLike, options?: stream.TransformOptions): Decipher;
interface Decipher extends NodeJS.ReadWriteStream {
update(data: Buffer | NodeJS.TypedArray | DataView): Buffer;
update(data: Binary): Buffer;
update(data: string, input_encoding: HexBase64BinaryEncoding): Buffer;
update(data: Buffer | NodeJS.TypedArray | DataView, input_encoding: any, output_encoding: Utf8AsciiBinaryEncoding): string;
update(data: Binary, input_encoding: any, output_encoding: Utf8AsciiBinaryEncoding): string;
// second arg is ignored
update(data: string, input_encoding: HexBase64BinaryEncoding, output_encoding: Utf8AsciiBinaryEncoding): string;
final(): Buffer;
final(output_encoding: string): string;
setAutoPadding(auto_padding?: boolean): this;
// setAuthTag(tag: Buffer | NodeJS.TypedArray | DataView): this;
// setAAD(buffer: Buffer | NodeJS.TypedArray | DataView): this;
// setAuthTag(tag: Binary): this;
// setAAD(buffer: Binary): this;
}
interface DecipherCCM extends Decipher {
setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView): this;
setAAD(buffer: Buffer | NodeJS.TypedArray | DataView, options: { plaintextLength: number }): this;
setAuthTag(buffer: Binary): this;
setAAD(buffer: Binary, options: { plaintextLength: number }): this;
}
interface DecipherGCM extends Decipher {
setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView): this;
setAAD(buffer: Buffer | NodeJS.TypedArray | DataView, options?: { plaintextLength: number }): this;
setAuthTag(buffer: Binary): this;
setAAD(buffer: Binary, options?: { plaintextLength: number }): this;
}
function createSign(algorithm: string, options?: stream.WritableOptions): Signer;
interface Signer extends NodeJS.WritableStream {
update(data: string | Buffer | NodeJS.TypedArray | DataView): Signer;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Signer;
sign(private_key: string | { key: string; passphrase?: string, padding?: number, saltLength?: number }): Buffer;
sign(private_key: string | { key: string; passphrase?: string, padding?: number, saltLength?: number }, output_format: HexBase64Latin1Encoding): string;
interface PrivateKeyInput {
key: string | Buffer;
format?: KeyFormat;
type?: 'pkcs1' | 'pkcs8' | 'sec1';
passphrase?: string | Buffer;
}
interface PublicKeyInput {
key: string | Buffer;
format?: KeyFormat;
type?: 'pkcs1' | 'spki';
}
function createPrivateKey(key: PrivateKeyInput | string | Buffer): KeyObject;
function createPublicKey(key: PublicKeyInput | string | Buffer): KeyObject;
function createSecretKey(key: Buffer): KeyObject;
function createSign(algorithm: string, options?: stream.WritableOptions): Signer;
interface SignPrivateKeyInput extends PrivateKeyInput {
padding?: number;
saltLength?: number;
}
type KeyLike = string | Buffer | KeyObject;
interface Signer extends NodeJS.WritableStream {
update(data: BinaryLike): Signer;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Signer;
sign(private_key: SignPrivateKeyInput | KeyLike): Buffer;
sign(private_key: SignPrivateKeyInput | KeyLike, output_format: HexBase64Latin1Encoding): string;
}
function createVerify(algorith: string, options?: stream.WritableOptions): Verify;
interface Verify extends NodeJS.WritableStream {
update(data: string | Buffer | NodeJS.TypedArray | DataView): Verify;
update(data: BinaryLike): Verify;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Verify;
verify(object: string | Object, signature: Buffer | NodeJS.TypedArray | DataView): boolean;
verify(object: string | Object, signature: string, signature_format: HexBase64Latin1Encoding): boolean;
verify(object: Object | KeyLike, signature: Binary): boolean;
verify(object: Object | KeyLike, signature: string, signature_format: HexBase64Latin1Encoding): boolean;
// https://nodejs.org/api/crypto.html#crypto_verifier_verify_object_signature_signature_format
// The signature field accepts a TypedArray type, but it is only available starting ES2017
}
function createDiffieHellman(prime_length: number, generator?: number | Buffer | NodeJS.TypedArray | DataView): DiffieHellman;
function createDiffieHellman(prime: Buffer | NodeJS.TypedArray | DataView): DiffieHellman;
function createDiffieHellman(prime_length: number, generator?: number | Binary): DiffieHellman;
function createDiffieHellman(prime: Binary): DiffieHellman;
function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding): DiffieHellman;
function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: number | Buffer | NodeJS.TypedArray | DataView): DiffieHellman;
function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: number | Binary): DiffieHellman;
function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: string, generator_encoding: HexBase64Latin1Encoding): DiffieHellman;
interface DiffieHellman {
generateKeys(): Buffer;
generateKeys(encoding: HexBase64Latin1Encoding): string;
computeSecret(other_public_key: Buffer | NodeJS.TypedArray | DataView): Buffer;
computeSecret(other_public_key: Binary): Buffer;
computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer;
computeSecret(other_public_key: Buffer | NodeJS.TypedArray | DataView, output_encoding: HexBase64Latin1Encoding): string;
computeSecret(other_public_key: Binary, output_encoding: HexBase64Latin1Encoding): string;
computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string;
getPrime(): Buffer;
getPrime(encoding: HexBase64Latin1Encoding): string;
@ -154,32 +212,32 @@ declare module "crypto" {
getPublicKey(encoding: HexBase64Latin1Encoding): string;
getPrivateKey(): Buffer;
getPrivateKey(encoding: HexBase64Latin1Encoding): string;
setPublicKey(public_key: Buffer | NodeJS.TypedArray | DataView): void;
setPublicKey(public_key: Binary): void;
setPublicKey(public_key: string, encoding: string): void;
setPrivateKey(private_key: Buffer | NodeJS.TypedArray | DataView): void;
setPrivateKey(private_key: Binary): void;
setPrivateKey(private_key: string, encoding: string): void;
verifyError: number;
}
function getDiffieHellman(group_name: string): DiffieHellman;
function pbkdf2(
password: string | Buffer | NodeJS.TypedArray | DataView,
salt: string | Buffer | NodeJS.TypedArray | DataView,
password: BinaryLike,
salt: BinaryLike,
iterations: number,
keylen: number,
digest: string,
callback: (err: Error | null, derivedKey: Buffer) => any,
): void;
function pbkdf2Sync(password: string | Buffer | NodeJS.TypedArray | DataView, salt: string | Buffer | NodeJS.TypedArray | DataView, iterations: number, keylen: number, digest: string): Buffer;
function pbkdf2Sync(password: BinaryLike, salt: BinaryLike, iterations: number, keylen: number, digest: string): Buffer;
function randomBytes(size: number): Buffer;
function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
function pseudoRandomBytes(size: number): Buffer;
function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
function randomFillSync<T extends Buffer | NodeJS.TypedArray | DataView>(buffer: T, offset?: number, size?: number): T;
function randomFill<T extends Buffer | NodeJS.TypedArray | DataView>(buffer: T, callback: (err: Error | null, buf: T) => void): void;
function randomFill<T extends Buffer | NodeJS.TypedArray | DataView>(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void;
function randomFill<T extends Buffer | NodeJS.TypedArray | DataView>(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void;
function randomFillSync<T extends Binary>(buffer: T, offset?: number, size?: number): T;
function randomFill<T extends Binary>(buffer: T, callback: (err: Error | null, buf: T) => void): void;
function randomFill<T extends Binary>(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void;
function randomFill<T extends Binary>(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void;
interface ScryptOptions {
N?: number;
@ -188,38 +246,38 @@ declare module "crypto" {
maxmem?: number;
}
function scrypt(
password: string | Buffer | NodeJS.TypedArray | DataView,
salt: string | Buffer | NodeJS.TypedArray | DataView,
password: BinaryLike,
salt: BinaryLike,
keylen: number, callback: (err: Error | null, derivedKey: Buffer) => void,
): void;
function scrypt(
password: string | Buffer | NodeJS.TypedArray | DataView,
salt: string | Buffer | NodeJS.TypedArray | DataView,
password: BinaryLike,
salt: BinaryLike,
keylen: number,
options: ScryptOptions,
callback: (err: Error | null, derivedKey: Buffer) => void,
): void;
function scryptSync(password: string | Buffer | NodeJS.TypedArray | DataView, salt: string | Buffer | NodeJS.TypedArray | DataView, keylen: number, options?: ScryptOptions): Buffer;
function scryptSync(password: BinaryLike, salt: BinaryLike, keylen: number, options?: ScryptOptions): Buffer;
interface RsaPublicKey {
key: string;
key: KeyLike;
padding?: number;
}
interface RsaPrivateKey {
key: string;
key: KeyLike;
passphrase?: string;
padding?: number;
}
function publicEncrypt(public_key: string | RsaPublicKey, buffer: Buffer | NodeJS.TypedArray | DataView): Buffer;
function privateDecrypt(private_key: string | RsaPrivateKey, buffer: Buffer | NodeJS.TypedArray | DataView): Buffer;
function privateEncrypt(private_key: string | RsaPrivateKey, buffer: Buffer | NodeJS.TypedArray | DataView): Buffer;
function publicDecrypt(public_key: string | RsaPublicKey, buffer: Buffer | NodeJS.TypedArray | DataView): Buffer;
function publicEncrypt(public_key: RsaPublicKey | KeyLike, buffer: Binary): Buffer;
function privateDecrypt(private_key: RsaPrivateKey | KeyLike, buffer: Binary): Buffer;
function privateEncrypt(private_key: RsaPrivateKey | KeyLike, buffer: Binary): Buffer;
function publicDecrypt(public_key: RsaPublicKey | KeyLike, buffer: Binary): Buffer;
function getCiphers(): string[];
function getCurves(): string[];
function getHashes(): string[];
class ECDH {
static convertKey(
key: string | Buffer | NodeJS.TypedArray | DataView,
key: BinaryLike,
curve: string,
inputEncoding?: HexBase64Latin1Encoding,
outputEncoding?: "latin1" | "hex" | "base64",
@ -227,19 +285,19 @@ declare module "crypto" {
): Buffer | string;
generateKeys(): Buffer;
generateKeys(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string;
computeSecret(other_public_key: Buffer | NodeJS.TypedArray | DataView): Buffer;
computeSecret(other_public_key: Binary): Buffer;
computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer;
computeSecret(other_public_key: Buffer | NodeJS.TypedArray | DataView, output_encoding: HexBase64Latin1Encoding): string;
computeSecret(other_public_key: Binary, output_encoding: HexBase64Latin1Encoding): string;
computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string;
getPrivateKey(): Buffer;
getPrivateKey(encoding: HexBase64Latin1Encoding): string;
getPublicKey(): Buffer;
getPublicKey(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string;
setPrivateKey(private_key: Buffer | NodeJS.TypedArray | DataView): void;
setPrivateKey(private_key: Binary): void;
setPrivateKey(private_key: string, encoding: HexBase64Latin1Encoding): void;
}
function createECDH(curve_name: string): ECDH;
function timingSafeEqual(a: Buffer | NodeJS.TypedArray | DataView, b: Buffer | NodeJS.TypedArray | DataView): boolean;
function timingSafeEqual(a: Binary, b: Binary): boolean;
/** @deprecated since v10.0.0 */
const DEFAULT_ENCODING: string;

View File

@ -6,6 +6,7 @@
// Alexander T. <https://github.com/a-tarasyuk>
// Alvis HT Tang <https://github.com/alvis>
// Andrew Makarov <https://github.com/r3nya>
// Benjamin Toueg <https://github.com/btoueg>
// Bruno Scheufler <https://github.com/brunoscheufler>
// Chigozirim C. <https://github.com/smac89>
// Christian Vaagland Tellnes <https://github.com/tellnes>

View File

@ -5,7 +5,6 @@ import events2 = require("events");
import * as zlib from "zlib";
import * as url from "url";
import * as util from "util";
import * as crypto from "crypto";
import * as tls from "tls";
import * as http from "http";
import * as https from "https";
@ -817,484 +816,6 @@ async function asyncStreamPipelineFinished() {
await pipeline(process.stdin, process.stdout);
}
////////////////////////////////////////////////////////
/// Crypto tests : http://nodejs.org/api/crypto.html ///
////////////////////////////////////////////////////////
{
{
// crypto_hash_string_test
const hashResult: string = crypto.createHash('md5').update('world').digest('hex');
}
{
// crypto_hash_buffer_test
const hashResult: string = crypto.createHash('md5')
.update(new Buffer('world')).digest('hex');
}
{
// crypto_hash_dataview_test
const hashResult: string = crypto.createHash('md5')
.update(new DataView(new Buffer('world').buffer)).digest('hex');
}
{
// crypto_hash_int8array_test
const hashResult: string = crypto.createHash('md5')
.update(new Int8Array(new Buffer('world').buffer)).digest('hex');
}
{
// crypto_hmac_string_test
const hmacResult: string = crypto.createHmac('md5', 'hello').update('world').digest('hex');
}
{
// crypto_hmac_buffer_test
const hmacResult: string = crypto.createHmac('md5', 'hello')
.update(new Buffer('world')).digest('hex');
}
{
// crypto_hmac_dataview_test
const hmacResult: string = crypto.createHmac('md5', 'hello')
.update(new DataView(new Buffer('world').buffer)).digest('hex');
}
{
// crypto_hmac_int8array_test
const hmacResult: string = crypto.createHmac('md5', 'hello')
.update(new Int8Array(new Buffer('world').buffer)).digest('hex');
}
{
let hmac: crypto.Hmac;
(hmac = crypto.createHmac('md5', 'hello')).end('world', 'utf8', () => {
const hash: Buffer | string = hmac.read();
});
}
{
// crypto_cipher_decipher_string_test
const key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
const clearText = "This is the clear text.";
const cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
let cipherText: string = cipher.update(clearText, "utf8", "hex");
cipherText += cipher.final("hex");
const decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
let clearText2: string = decipher.update(cipherText, "hex", "utf8");
clearText2 += decipher.final("utf8");
assert.equal(clearText2, clearText);
}
{
// crypto_cipher_decipher_buffer_test
const key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
const clearText: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]);
const cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
const cipherBuffers: Buffer[] = [];
cipherBuffers.push(cipher.update(clearText));
cipherBuffers.push(cipher.final());
const cipherText: Buffer = Buffer.concat(cipherBuffers);
const decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
const decipherBuffers: Buffer[] = [];
decipherBuffers.push(decipher.update(cipherText));
decipherBuffers.push(decipher.final());
const clearText2: Buffer = Buffer.concat(decipherBuffers);
assert.deepEqual(clearText2, clearText);
}
{
// crypto_cipher_decipher_dataview_test
const key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
const clearText: DataView = new DataView(new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]).buffer);
const cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
const cipherBuffers: Buffer[] = [];
cipherBuffers.push(cipher.update(clearText));
cipherBuffers.push(cipher.final());
const cipherText: DataView = new DataView(Buffer.concat(cipherBuffers).buffer);
const decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
const decipherBuffers: Buffer[] = [];
decipherBuffers.push(decipher.update(cipherText));
decipherBuffers.push(decipher.final());
const clearText2: Buffer = Buffer.concat(decipherBuffers);
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
const buffer1: Buffer = new Buffer([1, 2, 3, 4, 5]);
const buffer2: Buffer = new Buffer([1, 2, 3, 4, 5]);
const buffer3: Buffer = new Buffer([5, 4, 3, 2, 1]);
assert(crypto.timingSafeEqual(buffer1, buffer2));
assert(!crypto.timingSafeEqual(buffer1, buffer3));
}
{
// crypto_timingsafeequal_uint32array_test
const arr1: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const arr2: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const arr3: Uint32Array = Uint32Array.of(5, 4, 3, 2, 1);
assert(crypto.timingSafeEqual(arr1, arr2));
assert(!crypto.timingSafeEqual(arr1, arr3));
}
{
// crypto_timingsafeequal_safe_typedarray_variant_test
const arr1: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const arr2: Int32Array = Int32Array.of(1, 2, 3, 4, 5);
const arr3: Uint32Array = Uint32Array.of(5, 4, 3, 2, 1);
assert(crypto.timingSafeEqual(arr1, arr2));
assert(!crypto.timingSafeEqual(arr1, arr3));
}
{
// crypto_timingsafeequal_safe_int8array_variant_test
const arr1: Int8Array = Int8Array.of(1, 2, 3, 4, 5, ~0, ~1, ~2, ~3, ~4);
const arr2: Uint8Array = Uint8Array.of(1, 2, 3, 4, 5, ~0, ~1, ~2, ~3, ~4);
const arr3: Uint8ClampedArray = Uint8ClampedArray.of(1, 2, 3, 4, 5, ~0, ~1, ~2, ~3, ~4);
assert(crypto.timingSafeEqual(arr1, arr2)); // binary same
assert(!crypto.timingSafeEqual(arr1, arr3)); // binary differ
}
{
// crypto_timingsafeequal_safe_arraybufferiew_variant_test
/* throws as of v10.4.1 */
// let arr1: Uint8Array = Uint8Array.of(1, 0, 2, 0, 3, 0, 4, 0);
// let arr2: Uint16Array = Uint16Array.of(1, 2, 3, 4);
// let arr3: Uint32Array = Uint8ClampedArray.of(131073, 262147);
// assert(crypto.timingSafeEqual(arr1, arr2)); // binary same
// assert(crypto.timingSafeEqual(arr1, arr3)); // binary same
}
{
// crypto_timingsafeequal_unsafe_arraybufferiew_variant_test
/* dumps core as of v10.4.1 */
// let arr1: Uint8Array = Uint8Array.of(1, 2, 3, 4);
// let arr2: Uint16Array = Uint16Array.of(1, 2, 3, 4);
// let arr3: Uint32Array = Uint8ClampedArray.of(1, 2, 3, 4);
// assert(!crypto.timingSafeEqual(arr1, arr2)); // dumps core
// assert(!crypto.timingSafeEqual(arr1, arr3)); // dumps core
}
{
// crypto_timingsafeequal_dataview_test
const dv1B: Uint8Array = Uint8Array.of(1, 2, 3, 4, 5);
const dv2B: Int8Array = Int8Array.of(1, 2, 3, 4, 5);
const dv3B: Buffer = Buffer.of(5, 4, 3, 2, 1);
const dv4B: Uint8ClampedArray = Uint8ClampedArray.of(5, 4, 3, 2, 1);
const dv1: DataView = new DataView(dv1B.buffer, dv1B.byteOffset, dv1B.byteLength);
const dv2: DataView = new DataView(dv2B.buffer, dv2B.byteOffset, dv2B.byteLength);
const dv3: DataView = new DataView(dv3B.buffer, dv3B.byteOffset, dv3B.byteLength);
const dv4: DataView = new DataView(dv4B.buffer, dv4B.byteOffset, dv4B.byteLength);
assert(crypto.timingSafeEqual(dv1, dv2));
assert(crypto.timingSafeEqual(dv1, dv1B));
assert(crypto.timingSafeEqual(dv2, dv1B));
assert(crypto.timingSafeEqual(dv3, dv4));
assert(!crypto.timingSafeEqual(dv1, dv3));
assert(!crypto.timingSafeEqual(dv2, dv3));
assert(!crypto.timingSafeEqual(dv1, dv4));
// ... I'm not going to write all those tests.
}
{
// crypto_timingsafeequal_uint32array_test
const ui32_1: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const ui32_2: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const ui32_3: Uint32Array = Uint32Array.of(5, 4, 3, 2, 1);
assert(crypto.timingSafeEqual(ui32_1, ui32_2));
assert(!crypto.timingSafeEqual(ui32_1, ui32_3));
}
{
// crypto_randomfill_buffer_test
const buffer: Buffer = new Buffer(10);
crypto.randomFillSync(buffer);
crypto.randomFillSync(buffer, 2);
crypto.randomFillSync(buffer, 2, 3);
crypto.randomFill(buffer, (err: Error | null, buf: Buffer) => void {});
crypto.randomFill(buffer, 2, (err: Error | null, buf: Buffer) => void {});
crypto.randomFill(buffer, 2, 3, (err: Error | null, buf: Buffer) => void {});
// crypto_randomfill_uint8array_test
const ui8arr: Uint8Array = new Uint8Array(10);
crypto.randomFillSync(ui8arr);
crypto.randomFillSync(ui8arr, 2);
crypto.randomFillSync(ui8arr, 2, 3);
crypto.randomFill(ui8arr, (err: Error | null, buf: Uint8Array) => void {});
crypto.randomFill(ui8arr, 2, (err: Error | null, buf: Uint8Array) => void {});
crypto.randomFill(ui8arr, 2, 3, (err: Error | null, buf: Uint8Array) => void {});
// crypto_randomfill_int32array_test
const i32arr: Int32Array = new Int32Array(10);
crypto.randomFillSync(i32arr);
crypto.randomFillSync(i32arr, 2);
crypto.randomFillSync(i32arr, 2, 3);
crypto.randomFill(i32arr, (err: Error | null, buf: Int32Array) => void {});
crypto.randomFill(i32arr, 2, (err: Error | null, buf: Int32Array) => void {});
crypto.randomFill(i32arr, 2, 3, (err: Error | null, buf: Int32Array) => void {});
}
{
// scrypt
const pwd: string | Buffer | Int32Array | DataView = Buffer.alloc(16);
const salt: string | Buffer | Int32Array | DataView = Buffer.alloc(16);
crypto.scrypt(pwd, salt, 64, (err: Error | null, derivedKey: Buffer): void => {});
const opts: crypto.ScryptOptions = {
N: 16384,
r: 8,
p: 1,
maxmem: 32 * 1024 * 1024
};
crypto.scrypt(pwd, salt, 64, opts, (err: Error | null, derivedKey: Buffer): void => {});
crypto.scrypt(pwd, salt, 64, { maxmem: 16 * 1024 * 1024 }, (err: Error | null, derivedKey: Buffer): void => {});
let buf: Buffer = crypto.scryptSync(pwd, salt, 64);
buf = crypto.scryptSync(pwd, salt, 64, opts);
buf = crypto.scryptSync(pwd, salt, 64, { N: 1024 });
}
{
let key: string | Buffer = Buffer.from("buf");
const curve = "secp256k1";
let ret: string | Buffer = crypto.ECDH.convertKey(key, curve);
key = "0xfff";
ret = crypto.ECDH.convertKey(key, curve);
ret = crypto.ECDH.convertKey(key, curve, "hex");
ret = crypto.ECDH.convertKey(key, curve, "hex", "hex");
ret = crypto.ECDH.convertKey(key, curve, "hex", "hex", "uncompressed");
ret = crypto.ECDH.convertKey(key, curve, "hex", "hex", "compressed");
ret = crypto.ECDH.convertKey(key, curve, "hex", "hex", "hybrid");
}
{
const rsaRes: {
publicKey: Buffer;
privateKey: string;
} = crypto.generateKeyPairSync('rsa', {
modulusLength: 123,
publicKeyEncoding: {
format: 'der',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
});
const dsaRes: {
publicKey: string;
privateKey: Buffer;
} = crypto.generateKeyPairSync('dsa', {
modulusLength: 123,
divisorLength: 123,
publicKeyEncoding: {
format: 'pem',
type: 'spki',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'der',
passphrase: 'secret',
type: 'pkcs8',
},
});
const ecRes: {
publicKey: string;
privateKey: string;
} = crypto.generateKeyPairSync('ec', {
namedCurve: 'curve',
publicKeyEncoding: {
format: 'pem',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
});
}
{
crypto.generateKeyPair('rsa', {
modulusLength: 123,
publicKeyEncoding: {
format: 'der',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
}, (err: NodeJS.ErrnoException | null, publicKey: Buffer, privateKey: string) => {});
crypto.generateKeyPair('dsa', {
modulusLength: 123,
divisorLength: 123,
publicKeyEncoding: {
format: 'pem',
type: 'spki',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'der',
passphrase: 'secret',
type: 'pkcs8',
},
}, (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: Buffer) => {});
crypto.generateKeyPair('ec', {
namedCurve: 'curve',
publicKeyEncoding: {
format: 'pem',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
}, (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {});
}
{
const generateKeyPairPromisified = util.promisify(crypto.generateKeyPair);
const rsaRes: Promise<{
publicKey: Buffer;
privateKey: string;
}> = generateKeyPairPromisified('rsa', {
modulusLength: 123,
publicKeyEncoding: {
format: 'der',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
});
const dsaRes: Promise<{
publicKey: string;
privateKey: Buffer;
}> = generateKeyPairPromisified('dsa', {
modulusLength: 123,
divisorLength: 123,
publicKeyEncoding: {
format: 'pem',
type: 'spki',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'der',
passphrase: 'secret',
type: 'pkcs8',
},
});
const ecRes: Promise<{
publicKey: string;
privateKey: string;
}> = generateKeyPairPromisified('ec', {
namedCurve: 'curve',
publicKeyEncoding: {
format: 'pem',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
});
}
}
//////////////////////////////////////////////////
/// TLS tests : http://nodejs.org/api/tls.html ///
//////////////////////////////////////////////////

487
types/node/test/crypto.ts Normal file
View File

@ -0,0 +1,487 @@
import * as crypto from 'crypto';
import * as assert from 'assert';
import { promisify } from 'util';
{
// crypto_hash_string_test
const hashResult: string = crypto.createHash('md5').update('world').digest('hex');
}
{
// crypto_hash_buffer_test
const hashResult: string = crypto.createHash('md5')
.update(new Buffer('world')).digest('hex');
}
{
// crypto_hash_dataview_test
const hashResult: string = crypto.createHash('md5')
.update(new DataView(new Buffer('world').buffer)).digest('hex');
}
{
// crypto_hash_int8array_test
const hashResult: string = crypto.createHash('md5')
.update(new Int8Array(new Buffer('world').buffer)).digest('hex');
}
{
// crypto_hmac_string_test
const hmacResult: string = crypto.createHmac('md5', 'hello').update('world').digest('hex');
}
{
// crypto_hmac_buffer_test
const hmacResult: string = crypto.createHmac('md5', 'hello')
.update(new Buffer('world')).digest('hex');
}
{
// crypto_hmac_dataview_test
const hmacResult: string = crypto.createHmac('md5', 'hello')
.update(new DataView(new Buffer('world').buffer)).digest('hex');
}
{
// crypto_hmac_int8array_test
const hmacResult: string = crypto.createHmac('md5', 'hello')
.update(new Int8Array(new Buffer('world').buffer)).digest('hex');
}
{
let hmac: crypto.Hmac;
(hmac = crypto.createHmac('md5', 'hello')).end('world', 'utf8', () => {
const hash: Buffer | string = hmac.read();
});
}
{
// crypto_cipher_decipher_string_test
const key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
const clearText = "This is the clear text.";
const cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
let cipherText: string = cipher.update(clearText, "utf8", "hex");
cipherText += cipher.final("hex");
const decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
let clearText2: string = decipher.update(cipherText, "hex", "utf8");
clearText2 += decipher.final("utf8");
assert.equal(clearText2, clearText);
}
{
// crypto_cipher_decipher_buffer_test
const key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
const clearText: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]);
const cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
const cipherBuffers: Buffer[] = [];
cipherBuffers.push(cipher.update(clearText));
cipherBuffers.push(cipher.final());
const cipherText: Buffer = Buffer.concat(cipherBuffers);
const decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
const decipherBuffers: Buffer[] = [];
decipherBuffers.push(decipher.update(cipherText));
decipherBuffers.push(decipher.final());
const clearText2: Buffer = Buffer.concat(decipherBuffers);
assert.deepEqual(clearText2, clearText);
}
{
// crypto_cipher_decipher_dataview_test
const key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
const clearText: DataView = new DataView(new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]).buffer);
const cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
const cipherBuffers: Buffer[] = [];
cipherBuffers.push(cipher.update(clearText));
cipherBuffers.push(cipher.final());
const cipherText: DataView = new DataView(Buffer.concat(cipherBuffers).buffer);
const decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
const decipherBuffers: Buffer[] = [];
decipherBuffers.push(decipher.update(cipherText));
decipherBuffers.push(decipher.final());
const clearText2: Buffer = Buffer.concat(decipherBuffers);
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
const buffer1: Buffer = new Buffer([1, 2, 3, 4, 5]);
const buffer2: Buffer = new Buffer([1, 2, 3, 4, 5]);
const buffer3: Buffer = new Buffer([5, 4, 3, 2, 1]);
assert(crypto.timingSafeEqual(buffer1, buffer2));
assert(!crypto.timingSafeEqual(buffer1, buffer3));
}
{
// crypto_timingsafeequal_uint32array_test
const arr1: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const arr2: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const arr3: Uint32Array = Uint32Array.of(5, 4, 3, 2, 1);
assert(crypto.timingSafeEqual(arr1, arr2));
assert(!crypto.timingSafeEqual(arr1, arr3));
}
{
// crypto_timingsafeequal_safe_typedarray_variant_test
const arr1: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const arr2: Int32Array = Int32Array.of(1, 2, 3, 4, 5);
const arr3: Uint32Array = Uint32Array.of(5, 4, 3, 2, 1);
assert(crypto.timingSafeEqual(arr1, arr2));
assert(!crypto.timingSafeEqual(arr1, arr3));
}
{
// crypto_timingsafeequal_safe_int8array_variant_test
const arr1: Int8Array = Int8Array.of(1, 2, 3, 4, 5, ~0, ~1, ~2, ~3, ~4);
const arr2: Uint8Array = Uint8Array.of(1, 2, 3, 4, 5, ~0, ~1, ~2, ~3, ~4);
const arr3: Uint8ClampedArray = Uint8ClampedArray.of(1, 2, 3, 4, 5, ~0, ~1, ~2, ~3, ~4);
assert(crypto.timingSafeEqual(arr1, arr2)); // binary same
assert(!crypto.timingSafeEqual(arr1, arr3)); // binary differ
}
{
// crypto_timingsafeequal_safe_arraybufferiew_variant_test
/* throws as of v10.4.1 */
// let arr1: Uint8Array = Uint8Array.of(1, 0, 2, 0, 3, 0, 4, 0);
// let arr2: Uint16Array = Uint16Array.of(1, 2, 3, 4);
// let arr3: Uint32Array = Uint8ClampedArray.of(131073, 262147);
// assert(crypto.timingSafeEqual(arr1, arr2)); // binary same
// assert(crypto.timingSafeEqual(arr1, arr3)); // binary same
}
{
// crypto_timingsafeequal_unsafe_arraybufferiew_variant_test
/* dumps core as of v10.4.1 */
// let arr1: Uint8Array = Uint8Array.of(1, 2, 3, 4);
// let arr2: Uint16Array = Uint16Array.of(1, 2, 3, 4);
// let arr3: Uint32Array = Uint8ClampedArray.of(1, 2, 3, 4);
// assert(!crypto.timingSafeEqual(arr1, arr2)); // dumps core
// assert(!crypto.timingSafeEqual(arr1, arr3)); // dumps core
}
{
// crypto_timingsafeequal_dataview_test
const dv1B: Uint8Array = Uint8Array.of(1, 2, 3, 4, 5);
const dv2B: Int8Array = Int8Array.of(1, 2, 3, 4, 5);
const dv3B: Buffer = Buffer.of(5, 4, 3, 2, 1);
const dv4B: Uint8ClampedArray = Uint8ClampedArray.of(5, 4, 3, 2, 1);
const dv1: DataView = new DataView(dv1B.buffer, dv1B.byteOffset, dv1B.byteLength);
const dv2: DataView = new DataView(dv2B.buffer, dv2B.byteOffset, dv2B.byteLength);
const dv3: DataView = new DataView(dv3B.buffer, dv3B.byteOffset, dv3B.byteLength);
const dv4: DataView = new DataView(dv4B.buffer, dv4B.byteOffset, dv4B.byteLength);
assert(crypto.timingSafeEqual(dv1, dv2));
assert(crypto.timingSafeEqual(dv1, dv1B));
assert(crypto.timingSafeEqual(dv2, dv1B));
assert(crypto.timingSafeEqual(dv3, dv4));
assert(!crypto.timingSafeEqual(dv1, dv3));
assert(!crypto.timingSafeEqual(dv2, dv3));
assert(!crypto.timingSafeEqual(dv1, dv4));
// ... I'm not going to write all those tests.
}
{
// crypto_timingsafeequal_uint32array_test
const ui32_1: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const ui32_2: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
const ui32_3: Uint32Array = Uint32Array.of(5, 4, 3, 2, 1);
assert(crypto.timingSafeEqual(ui32_1, ui32_2));
assert(!crypto.timingSafeEqual(ui32_1, ui32_3));
}
{
// crypto_randomfill_buffer_test
const buffer: Buffer = new Buffer(10);
crypto.randomFillSync(buffer);
crypto.randomFillSync(buffer, 2);
crypto.randomFillSync(buffer, 2, 3);
crypto.randomFill(buffer, (err: Error | null, buf: Buffer) => void {});
crypto.randomFill(buffer, 2, (err: Error | null, buf: Buffer) => void {});
crypto.randomFill(buffer, 2, 3, (err: Error | null, buf: Buffer) => void {});
// crypto_randomfill_uint8array_test
const ui8arr: Uint8Array = new Uint8Array(10);
crypto.randomFillSync(ui8arr);
crypto.randomFillSync(ui8arr, 2);
crypto.randomFillSync(ui8arr, 2, 3);
crypto.randomFill(ui8arr, (err: Error | null, buf: Uint8Array) => void {});
crypto.randomFill(ui8arr, 2, (err: Error | null, buf: Uint8Array) => void {});
crypto.randomFill(ui8arr, 2, 3, (err: Error | null, buf: Uint8Array) => void {});
// crypto_randomfill_int32array_test
const i32arr: Int32Array = new Int32Array(10);
crypto.randomFillSync(i32arr);
crypto.randomFillSync(i32arr, 2);
crypto.randomFillSync(i32arr, 2, 3);
crypto.randomFill(i32arr, (err: Error | null, buf: Int32Array) => void {});
crypto.randomFill(i32arr, 2, (err: Error | null, buf: Int32Array) => void {});
crypto.randomFill(i32arr, 2, 3, (err: Error | null, buf: Int32Array) => void {});
}
{
// scrypt
const pwd: string | Buffer | Int32Array | DataView = Buffer.alloc(16);
const salt: string | Buffer | Int32Array | DataView = Buffer.alloc(16);
crypto.scrypt(pwd, salt, 64, (err: Error | null, derivedKey: Buffer): void => {});
const opts: crypto.ScryptOptions = {
N: 16384,
r: 8,
p: 1,
maxmem: 32 * 1024 * 1024
};
crypto.scrypt(pwd, salt, 64, opts, (err: Error | null, derivedKey: Buffer): void => {});
crypto.scrypt(pwd, salt, 64, { maxmem: 16 * 1024 * 1024 }, (err: Error | null, derivedKey: Buffer): void => {});
let buf: Buffer = crypto.scryptSync(pwd, salt, 64);
buf = crypto.scryptSync(pwd, salt, 64, opts);
buf = crypto.scryptSync(pwd, salt, 64, { N: 1024 });
}
{
let key: string | Buffer = Buffer.from("buf");
const curve = "secp256k1";
let ret: string | Buffer = crypto.ECDH.convertKey(key, curve);
key = "0xfff";
ret = crypto.ECDH.convertKey(key, curve);
ret = crypto.ECDH.convertKey(key, curve, "hex");
ret = crypto.ECDH.convertKey(key, curve, "hex", "hex");
ret = crypto.ECDH.convertKey(key, curve, "hex", "hex", "uncompressed");
ret = crypto.ECDH.convertKey(key, curve, "hex", "hex", "compressed");
ret = crypto.ECDH.convertKey(key, curve, "hex", "hex", "hybrid");
}
{
const rsaRes: {
publicKey: Buffer;
privateKey: string;
} = crypto.generateKeyPairSync('rsa', {
modulusLength: 123,
publicKeyEncoding: {
format: 'der',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
});
const dsaRes: {
publicKey: string;
privateKey: Buffer;
} = crypto.generateKeyPairSync('dsa', {
modulusLength: 123,
divisorLength: 123,
publicKeyEncoding: {
format: 'pem',
type: 'spki',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'der',
passphrase: 'secret',
type: 'pkcs8',
},
});
const ecRes: {
publicKey: string;
privateKey: string;
} = crypto.generateKeyPairSync('ec', {
namedCurve: 'curve',
publicKeyEncoding: {
format: 'pem',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
});
}
{
crypto.generateKeyPair('rsa', {
modulusLength: 123,
publicKeyEncoding: {
format: 'der',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
}, (err: NodeJS.ErrnoException | null, publicKey: Buffer, privateKey: string) => {});
crypto.generateKeyPair('dsa', {
modulusLength: 123,
divisorLength: 123,
publicKeyEncoding: {
format: 'pem',
type: 'spki',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'der',
passphrase: 'secret',
type: 'pkcs8',
},
}, (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: Buffer) => {});
crypto.generateKeyPair('ec', {
namedCurve: 'curve',
publicKeyEncoding: {
format: 'pem',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
}, (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {});
}
{
const generateKeyPairPromisified = promisify(crypto.generateKeyPair);
const rsaRes: Promise<{
publicKey: Buffer;
privateKey: string;
}> = generateKeyPairPromisified('rsa', {
modulusLength: 123,
publicKeyEncoding: {
format: 'der',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
});
const dsaRes: Promise<{
publicKey: string;
privateKey: Buffer;
}> = generateKeyPairPromisified('dsa', {
modulusLength: 123,
divisorLength: 123,
publicKeyEncoding: {
format: 'pem',
type: 'spki',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'der',
passphrase: 'secret',
type: 'pkcs8',
},
});
const ecRes: Promise<{
publicKey: string;
privateKey: string;
}> = generateKeyPairPromisified('ec', {
namedCurve: 'curve',
publicKeyEncoding: {
format: 'pem',
type: 'pkcs1',
},
privateKeyEncoding: {
cipher: 'some-cipher',
format: 'pem',
passphrase: 'secret',
type: 'pkcs8',
},
});
}
{
crypto.createPrivateKey(Buffer.from('asdf'));
crypto.createPrivateKey({
key: 'asd',
format: 'der',
});
}
{
crypto.createSecretKey(Buffer.from('asdf'));
}

2
types/node/tls.d.ts vendored
View File

@ -382,7 +382,7 @@ declare module "tls" {
function connect(options: ConnectionOptions, secureConnectListener?: () => void): TLSSocket;
function connect(port: number, host?: string, options?: ConnectionOptions, secureConnectListener?: () => void): TLSSocket;
function connect(port: number, options?: ConnectionOptions, secureConnectListener?: () => void): TLSSocket;
function createSecurePair(credentials?: crypto.Credentials, isServer?: boolean, requestCert?: boolean, rejectUnauthorized?: boolean): SecurePair;
function createSecurePair(credentials?: SecureContext, isServer?: boolean, requestCert?: boolean, rejectUnauthorized?: boolean): SecurePair;
function createSecureContext(details: SecureContextOptions): SecureContext;
function getCiphers(): string[];

View File

@ -51,7 +51,8 @@
"test/net.ts",
"test/dgram.ts",
"test/buffer.ts",
"test/tty.ts"
"test/tty.ts",
"test/crypto.ts"
],
"compilerOptions": {
"module": "commonjs",