[node] crypto.Verify and Signer should be classes (#34223)

* [node] crypto.Verify and Signer should be classes

* convert more crypto interfaces to classes

* replace redundant type checks by a comment
This commit is contained in:
Gerhard Stöbich
2019-03-26 16:58:19 +01:00
committed by timolinn
parent 1317f00f36
commit c9260eadb6
2 changed files with 56 additions and 9 deletions

View File

@@ -118,13 +118,15 @@ declare module "crypto" {
type HexBase64BinaryEncoding = "binary" | "base64" | "hex";
type ECDHKeyFormat = "compressed" | "uncompressed" | "hybrid";
interface Hash extends NodeJS.ReadWriteStream {
class Hash extends stream.Duplex {
private constructor();
update(data: BinaryLike): Hash;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hash;
digest(): Buffer;
digest(encoding: HexBase64Latin1Encoding): string;
}
interface Hmac extends NodeJS.ReadWriteStream {
class Hmac extends stream.Duplex {
private constructor();
update(data: BinaryLike): Hmac;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hmac;
digest(): Buffer;
@@ -133,7 +135,8 @@ declare module "crypto" {
export type KeyObjectType = 'secret' | 'public' | 'private';
interface KeyObject {
class KeyObject {
private constructor();
asymmetricKeyType?: KeyType;
export(options?: {
type: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1',
@@ -182,7 +185,8 @@ declare module "crypto" {
algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions
): Cipher;
interface Cipher extends NodeJS.ReadWriteStream {
class Cipher extends stream.Duplex {
private constructor();
update(data: BinaryLike): Buffer;
update(data: string, input_encoding: Utf8AsciiBinaryEncoding): Buffer;
update(data: Binary, input_encoding: undefined, output_encoding: HexBase64BinaryEncoding): string;
@@ -222,7 +226,8 @@ declare module "crypto" {
): DecipherGCM;
function createDecipheriv(algorithm: string, key: BinaryLike, iv: BinaryLike | null, options?: stream.TransformOptions): Decipher;
interface Decipher extends NodeJS.ReadWriteStream {
class Decipher extends stream.Duplex {
private constructor();
update(data: Binary): Buffer;
update(data: string, input_encoding: HexBase64BinaryEncoding): Buffer;
update(data: Binary, input_encoding: undefined, output_encoding: Utf8AsciiBinaryEncoding): string;
@@ -268,7 +273,9 @@ declare module "crypto" {
type KeyLike = string | Buffer | KeyObject;
interface Signer extends NodeJS.WritableStream {
class Signer extends stream.Writable {
private constructor();
update(data: BinaryLike): Signer;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Signer;
sign(private_key: SignPrivateKeyInput | KeyLike): Buffer;
@@ -276,11 +283,13 @@ declare module "crypto" {
}
function createVerify(algorith: string, options?: stream.WritableOptions): Verify;
interface Verify extends NodeJS.WritableStream {
class Verify extends stream.Writable {
private constructor();
update(data: BinaryLike): Verify;
update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Verify;
verify(object: Object | KeyLike, signature: Binary): boolean;
verify(object: Object | KeyLike, signature: string, signature_format: HexBase64Latin1Encoding): 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
}
@@ -289,7 +298,8 @@ declare module "crypto" {
function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding): 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 {
class DiffieHellman {
private constructor();
generateKeys(): Buffer;
generateKeys(encoding: HexBase64Latin1Encoding): string;
computeSecret(other_public_key: Binary): Buffer;
@@ -368,6 +378,7 @@ declare module "crypto" {
function getCurves(): string[];
function getHashes(): string[];
class ECDH {
private constructor();
static convertKey(
key: BinaryLike,
curve: string,

View File

@@ -486,6 +486,42 @@ import { promisify } from 'util';
crypto.createSecretKey(Buffer.from('asdf'));
}
{
const { privateKey, publicKey } = crypto.generateKeyPairSync('ec', {
namedCurve: 'sect239k1'
});
const sign: crypto.Signer = crypto.createSign('SHA256');
sign.write('some data to sign');
sign.end();
const signature: string = sign.sign(privateKey, 'hex');
const verify: crypto.Verify = crypto.createVerify('SHA256');
verify.write('some data to sign');
verify.end();
verify.verify(publicKey, signature); // $ExpectType boolean
// ensure that instanceof works
verify instanceof crypto.Verify;
sign instanceof crypto.Signer;
}
{
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
});
const sign: crypto.Signer = crypto.createSign('SHA256');
sign.update('some data to sign');
sign.end();
const signature: Buffer = sign.sign(privateKey);
const verify: crypto.Verify = crypto.createVerify('SHA256');
verify.update('some data to sign');
verify.end();
verify.verify(publicKey, signature); // $ExpectType boolean
}
{
// crypto_constants_test
let num: number;