You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto.d.ts 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. declare module "crypto" {
  2. import * as stream from "stream";
  3. interface Certificate {
  4. exportChallenge(spkac: BinaryLike): Buffer;
  5. exportPublicKey(spkac: BinaryLike): Buffer;
  6. verifySpkac(spkac: Binary): boolean;
  7. }
  8. const Certificate: {
  9. new(): Certificate;
  10. (): Certificate;
  11. };
  12. namespace constants { // https://nodejs.org/dist/latest-v10.x/docs/api/crypto.html#crypto_crypto_constants
  13. const OPENSSL_VERSION_NUMBER: number;
  14. /** Applies multiple bug workarounds within OpenSSL. See https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html for detail. */
  15. const SSL_OP_ALL: number;
  16. /** Allows legacy insecure renegotiation between OpenSSL and unpatched clients or servers. See https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html. */
  17. const SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION: number;
  18. /** Attempts to use the server's preferences instead of the client's when selecting a cipher. See https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html. */
  19. const SSL_OP_CIPHER_SERVER_PREFERENCE: number;
  20. /** Instructs OpenSSL to use Cisco's "speshul" version of DTLS_BAD_VER. */
  21. const SSL_OP_CISCO_ANYCONNECT: number;
  22. /** Instructs OpenSSL to turn on cookie exchange. */
  23. const SSL_OP_COOKIE_EXCHANGE: number;
  24. /** Instructs OpenSSL to add server-hello extension from an early version of the cryptopro draft. */
  25. const SSL_OP_CRYPTOPRO_TLSEXT_BUG: number;
  26. /** Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability workaround added in OpenSSL 0.9.6d. */
  27. const SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: number;
  28. /** Instructs OpenSSL to always use the tmp_rsa key when performing RSA operations. */
  29. const SSL_OP_EPHEMERAL_RSA: number;
  30. /** Allows initial connection to servers that do not support RI. */
  31. const SSL_OP_LEGACY_SERVER_CONNECT: number;
  32. const SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER: number;
  33. const SSL_OP_MICROSOFT_SESS_ID_BUG: number;
  34. /** Instructs OpenSSL to disable the workaround for a man-in-the-middle protocol-version vulnerability in the SSL 2.0 server implementation. */
  35. const SSL_OP_MSIE_SSLV2_RSA_PADDING: number;
  36. const SSL_OP_NETSCAPE_CA_DN_BUG: number;
  37. const SSL_OP_NETSCAPE_CHALLENGE_BUG: number;
  38. const SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG: number;
  39. const SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG: number;
  40. /** Instructs OpenSSL to disable support for SSL/TLS compression. */
  41. const SSL_OP_NO_COMPRESSION: number;
  42. const SSL_OP_NO_QUERY_MTU: number;
  43. /** Instructs OpenSSL to always start a new session when performing renegotiation. */
  44. const SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION: number;
  45. const SSL_OP_NO_SSLv2: number;
  46. const SSL_OP_NO_SSLv3: number;
  47. const SSL_OP_NO_TICKET: number;
  48. const SSL_OP_NO_TLSv1: number;
  49. const SSL_OP_NO_TLSv1_1: number;
  50. const SSL_OP_NO_TLSv1_2: number;
  51. const SSL_OP_PKCS1_CHECK_1: number;
  52. const SSL_OP_PKCS1_CHECK_2: number;
  53. /** Instructs OpenSSL to always create a new key when using temporary/ephemeral DH parameters. */
  54. const SSL_OP_SINGLE_DH_USE: number;
  55. /** Instructs OpenSSL to always create a new key when using temporary/ephemeral ECDH parameters. */
  56. const SSL_OP_SINGLE_ECDH_USE: number;
  57. const SSL_OP_SSLEAY_080_CLIENT_DH_BUG: number;
  58. const SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG: number;
  59. const SSL_OP_TLS_BLOCK_PADDING_BUG: number;
  60. const SSL_OP_TLS_D5_BUG: number;
  61. /** Instructs OpenSSL to disable version rollback attack detection. */
  62. const SSL_OP_TLS_ROLLBACK_BUG: number;
  63. const ENGINE_METHOD_RSA: number;
  64. const ENGINE_METHOD_DSA: number;
  65. const ENGINE_METHOD_DH: number;
  66. const ENGINE_METHOD_RAND: number;
  67. const ENGINE_METHOD_EC: number;
  68. const ENGINE_METHOD_CIPHERS: number;
  69. const ENGINE_METHOD_DIGESTS: number;
  70. const ENGINE_METHOD_PKEY_METHS: number;
  71. const ENGINE_METHOD_PKEY_ASN1_METHS: number;
  72. const ENGINE_METHOD_ALL: number;
  73. const ENGINE_METHOD_NONE: number;
  74. const DH_CHECK_P_NOT_SAFE_PRIME: number;
  75. const DH_CHECK_P_NOT_PRIME: number;
  76. const DH_UNABLE_TO_CHECK_GENERATOR: number;
  77. const DH_NOT_SUITABLE_GENERATOR: number;
  78. const ALPN_ENABLED: number;
  79. const RSA_PKCS1_PADDING: number;
  80. const RSA_SSLV23_PADDING: number;
  81. const RSA_NO_PADDING: number;
  82. const RSA_PKCS1_OAEP_PADDING: number;
  83. const RSA_X931_PADDING: number;
  84. const RSA_PKCS1_PSS_PADDING: number;
  85. /** Sets the salt length for RSA_PKCS1_PSS_PADDING to the digest size when signing or verifying. */
  86. const RSA_PSS_SALTLEN_DIGEST: number;
  87. /** Sets the salt length for RSA_PKCS1_PSS_PADDING to the maximum permissible value when signing data. */
  88. const RSA_PSS_SALTLEN_MAX_SIGN: number;
  89. /** Causes the salt length for RSA_PKCS1_PSS_PADDING to be determined automatically when verifying a signature. */
  90. const RSA_PSS_SALTLEN_AUTO: number;
  91. const POINT_CONVERSION_COMPRESSED: number;
  92. const POINT_CONVERSION_UNCOMPRESSED: number;
  93. const POINT_CONVERSION_HYBRID: number;
  94. /** Specifies the built-in default cipher list used by Node.js (colon-separated values). */
  95. const defaultCoreCipherList: string;
  96. /** Specifies the active default cipher list used by the current Node.js process (colon-separated values). */
  97. const defaultCipherList: string;
  98. }
  99. /** @deprecated since v10.0.0 */
  100. const fips: boolean;
  101. function createHash(algorithm: string, options?: stream.TransformOptions): Hash;
  102. function createHmac(algorithm: string, key: BinaryLike, options?: stream.TransformOptions): Hmac;
  103. type Utf8AsciiLatin1Encoding = "utf8" | "ascii" | "latin1";
  104. type HexBase64Latin1Encoding = "latin1" | "hex" | "base64";
  105. type Utf8AsciiBinaryEncoding = "utf8" | "ascii" | "binary";
  106. type HexBase64BinaryEncoding = "binary" | "base64" | "hex";
  107. type ECDHKeyFormat = "compressed" | "uncompressed" | "hybrid";
  108. class Hash extends stream.Transform {
  109. private constructor();
  110. update(data: BinaryLike): Hash;
  111. update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hash;
  112. digest(): Buffer;
  113. digest(encoding: HexBase64Latin1Encoding): string;
  114. }
  115. class Hmac extends stream.Transform {
  116. private constructor();
  117. update(data: BinaryLike): Hmac;
  118. update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hmac;
  119. digest(): Buffer;
  120. digest(encoding: HexBase64Latin1Encoding): string;
  121. }
  122. export type KeyObjectType = 'secret' | 'public' | 'private';
  123. interface KeyExportOptions<T extends KeyFormat> {
  124. type: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1';
  125. format: T;
  126. cipher?: string;
  127. passphrase?: string | Buffer;
  128. }
  129. class KeyObject {
  130. private constructor();
  131. asymmetricKeyType?: KeyType;
  132. export(options: KeyExportOptions<'pem'>): string | Buffer;
  133. export(options?: KeyExportOptions<'der'>): Buffer;
  134. symmetricSize?: number;
  135. type: KeyObjectType;
  136. }
  137. type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm';
  138. type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
  139. type Binary = Buffer | NodeJS.TypedArray | DataView;
  140. type BinaryLike = string | Binary;
  141. type CipherKey = BinaryLike | KeyObject;
  142. interface CipherCCMOptions extends stream.TransformOptions {
  143. authTagLength: number;
  144. }
  145. interface CipherGCMOptions extends stream.TransformOptions {
  146. authTagLength?: number;
  147. }
  148. /** @deprecated since v10.0.0 use createCipheriv() */
  149. function createCipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): CipherCCM;
  150. /** @deprecated since v10.0.0 use createCipheriv() */
  151. function createCipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): CipherGCM;
  152. /** @deprecated since v10.0.0 use createCipheriv() */
  153. function createCipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Cipher;
  154. function createCipheriv(
  155. algorithm: CipherCCMTypes,
  156. key: CipherKey,
  157. iv: BinaryLike | null,
  158. options: CipherCCMOptions
  159. ): CipherCCM;
  160. function createCipheriv(
  161. algorithm: CipherGCMTypes,
  162. key: CipherKey,
  163. iv: BinaryLike | null,
  164. options?: CipherGCMOptions
  165. ): CipherGCM;
  166. function createCipheriv(
  167. algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions
  168. ): Cipher;
  169. class Cipher extends stream.Transform {
  170. private constructor();
  171. update(data: BinaryLike): Buffer;
  172. update(data: string, input_encoding: Utf8AsciiBinaryEncoding): Buffer;
  173. update(data: Binary, input_encoding: undefined, output_encoding: HexBase64BinaryEncoding): string;
  174. update(data: string, input_encoding: Utf8AsciiBinaryEncoding | undefined, output_encoding: HexBase64BinaryEncoding): string;
  175. final(): Buffer;
  176. final(output_encoding: string): string;
  177. setAutoPadding(auto_padding?: boolean): this;
  178. // getAuthTag(): Buffer;
  179. // setAAD(buffer: Buffer): this; // docs only say buffer
  180. }
  181. interface CipherCCM extends Cipher {
  182. setAAD(buffer: Buffer, options: { plaintextLength: number }): this;
  183. getAuthTag(): Buffer;
  184. }
  185. interface CipherGCM extends Cipher {
  186. setAAD(buffer: Buffer, options?: { plaintextLength: number }): this;
  187. getAuthTag(): Buffer;
  188. }
  189. /** @deprecated since v10.0.0 use createCipheriv() */
  190. function createDecipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): DecipherCCM;
  191. /** @deprecated since v10.0.0 use createCipheriv() */
  192. function createDecipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): DecipherGCM;
  193. /** @deprecated since v10.0.0 use createCipheriv() */
  194. function createDecipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Decipher;
  195. function createDecipheriv(
  196. algorithm: CipherCCMTypes,
  197. key: BinaryLike,
  198. iv: BinaryLike | null,
  199. options: CipherCCMOptions,
  200. ): DecipherCCM;
  201. function createDecipheriv(
  202. algorithm: CipherGCMTypes,
  203. key: BinaryLike,
  204. iv: BinaryLike | null,
  205. options?: CipherGCMOptions,
  206. ): DecipherGCM;
  207. function createDecipheriv(algorithm: string, key: BinaryLike, iv: BinaryLike | null, options?: stream.TransformOptions): Decipher;
  208. class Decipher extends stream.Transform {
  209. private constructor();
  210. update(data: Binary): Buffer;
  211. update(data: string, input_encoding: HexBase64BinaryEncoding): Buffer;
  212. update(data: Binary, input_encoding: undefined, output_encoding: Utf8AsciiBinaryEncoding): string;
  213. update(data: string, input_encoding: HexBase64BinaryEncoding | undefined, output_encoding: Utf8AsciiBinaryEncoding): string;
  214. final(): Buffer;
  215. final(output_encoding: string): string;
  216. setAutoPadding(auto_padding?: boolean): this;
  217. // setAuthTag(tag: Binary): this;
  218. // setAAD(buffer: Binary): this;
  219. }
  220. interface DecipherCCM extends Decipher {
  221. setAuthTag(buffer: Binary): this;
  222. setAAD(buffer: Binary, options: { plaintextLength: number }): this;
  223. }
  224. interface DecipherGCM extends Decipher {
  225. setAuthTag(buffer: Binary): this;
  226. setAAD(buffer: Binary, options?: { plaintextLength: number }): this;
  227. }
  228. interface PrivateKeyInput {
  229. key: string | Buffer;
  230. format?: KeyFormat;
  231. type?: 'pkcs1' | 'pkcs8' | 'sec1';
  232. passphrase?: string | Buffer;
  233. }
  234. interface PublicKeyInput {
  235. key: string | Buffer;
  236. format?: KeyFormat;
  237. type?: 'pkcs1' | 'spki';
  238. }
  239. function createPrivateKey(key: PrivateKeyInput | string | Buffer): KeyObject;
  240. function createPublicKey(key: PublicKeyInput | string | Buffer | KeyObject): KeyObject;
  241. function createSecretKey(key: Buffer): KeyObject;
  242. function createSign(algorithm: string, options?: stream.WritableOptions): Signer;
  243. interface SignPrivateKeyInput extends PrivateKeyInput {
  244. padding?: number;
  245. saltLength?: number;
  246. }
  247. type KeyLike = string | Buffer | KeyObject;
  248. class Signer extends stream.Writable {
  249. private constructor();
  250. update(data: BinaryLike): Signer;
  251. update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Signer;
  252. sign(private_key: SignPrivateKeyInput | KeyLike): Buffer;
  253. sign(private_key: SignPrivateKeyInput | KeyLike, output_format: HexBase64Latin1Encoding): string;
  254. }
  255. function createVerify(algorith: string, options?: stream.WritableOptions): Verify;
  256. class Verify extends stream.Writable {
  257. private constructor();
  258. update(data: BinaryLike): Verify;
  259. update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Verify;
  260. verify(object: Object | KeyLike, signature: Binary): boolean;
  261. verify(object: Object | KeyLike, signature: string, signature_format?: HexBase64Latin1Encoding): boolean;
  262. // https://nodejs.org/api/crypto.html#crypto_verifier_verify_object_signature_signature_format
  263. // The signature field accepts a TypedArray type, but it is only available starting ES2017
  264. }
  265. function createDiffieHellman(prime_length: number, generator?: number | Binary): DiffieHellman;
  266. function createDiffieHellman(prime: Binary): DiffieHellman;
  267. function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding): DiffieHellman;
  268. function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: number | Binary): DiffieHellman;
  269. function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: string, generator_encoding: HexBase64Latin1Encoding): DiffieHellman;
  270. class DiffieHellman {
  271. private constructor();
  272. generateKeys(): Buffer;
  273. generateKeys(encoding: HexBase64Latin1Encoding): string;
  274. computeSecret(other_public_key: Binary): Buffer;
  275. computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer;
  276. computeSecret(other_public_key: Binary, output_encoding: HexBase64Latin1Encoding): string;
  277. computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string;
  278. getPrime(): Buffer;
  279. getPrime(encoding: HexBase64Latin1Encoding): string;
  280. getGenerator(): Buffer;
  281. getGenerator(encoding: HexBase64Latin1Encoding): string;
  282. getPublicKey(): Buffer;
  283. getPublicKey(encoding: HexBase64Latin1Encoding): string;
  284. getPrivateKey(): Buffer;
  285. getPrivateKey(encoding: HexBase64Latin1Encoding): string;
  286. setPublicKey(public_key: Binary): void;
  287. setPublicKey(public_key: string, encoding: string): void;
  288. setPrivateKey(private_key: Binary): void;
  289. setPrivateKey(private_key: string, encoding: string): void;
  290. verifyError: number;
  291. }
  292. function getDiffieHellman(group_name: string): DiffieHellman;
  293. function pbkdf2(
  294. password: BinaryLike,
  295. salt: BinaryLike,
  296. iterations: number,
  297. keylen: number,
  298. digest: string,
  299. callback: (err: Error | null, derivedKey: Buffer) => any,
  300. ): void;
  301. function pbkdf2Sync(password: BinaryLike, salt: BinaryLike, iterations: number, keylen: number, digest: string): Buffer;
  302. function randomBytes(size: number): Buffer;
  303. function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
  304. function pseudoRandomBytes(size: number): Buffer;
  305. function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
  306. function randomFillSync<T extends Binary>(buffer: T, offset?: number, size?: number): T;
  307. function randomFill<T extends Binary>(buffer: T, callback: (err: Error | null, buf: T) => void): void;
  308. function randomFill<T extends Binary>(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void;
  309. function randomFill<T extends Binary>(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void;
  310. interface ScryptOptions {
  311. N?: number;
  312. r?: number;
  313. p?: number;
  314. maxmem?: number;
  315. }
  316. function scrypt(
  317. password: BinaryLike,
  318. salt: BinaryLike,
  319. keylen: number, callback: (err: Error | null, derivedKey: Buffer) => void,
  320. ): void;
  321. function scrypt(
  322. password: BinaryLike,
  323. salt: BinaryLike,
  324. keylen: number,
  325. options: ScryptOptions,
  326. callback: (err: Error | null, derivedKey: Buffer) => void,
  327. ): void;
  328. function scryptSync(password: BinaryLike, salt: BinaryLike, keylen: number, options?: ScryptOptions): Buffer;
  329. interface RsaPublicKey {
  330. key: KeyLike;
  331. padding?: number;
  332. }
  333. interface RsaPrivateKey {
  334. key: KeyLike;
  335. passphrase?: string;
  336. padding?: number;
  337. }
  338. function publicEncrypt(public_key: RsaPublicKey | KeyLike, buffer: Binary): Buffer;
  339. function privateDecrypt(private_key: RsaPrivateKey | KeyLike, buffer: Binary): Buffer;
  340. function privateEncrypt(private_key: RsaPrivateKey | KeyLike, buffer: Binary): Buffer;
  341. function publicDecrypt(public_key: RsaPublicKey | KeyLike, buffer: Binary): Buffer;
  342. function getCiphers(): string[];
  343. function getCurves(): string[];
  344. function getHashes(): string[];
  345. class ECDH {
  346. private constructor();
  347. static convertKey(
  348. key: BinaryLike,
  349. curve: string,
  350. inputEncoding?: HexBase64Latin1Encoding,
  351. outputEncoding?: "latin1" | "hex" | "base64",
  352. format?: "uncompressed" | "compressed" | "hybrid",
  353. ): Buffer | string;
  354. generateKeys(): Buffer;
  355. generateKeys(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string;
  356. computeSecret(other_public_key: Binary): Buffer;
  357. computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer;
  358. computeSecret(other_public_key: Binary, output_encoding: HexBase64Latin1Encoding): string;
  359. computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string;
  360. getPrivateKey(): Buffer;
  361. getPrivateKey(encoding: HexBase64Latin1Encoding): string;
  362. getPublicKey(): Buffer;
  363. getPublicKey(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string;
  364. setPrivateKey(private_key: Binary): void;
  365. setPrivateKey(private_key: string, encoding: HexBase64Latin1Encoding): void;
  366. }
  367. function createECDH(curve_name: string): ECDH;
  368. function timingSafeEqual(a: Binary, b: Binary): boolean;
  369. /** @deprecated since v10.0.0 */
  370. const DEFAULT_ENCODING: string;
  371. export type KeyType = 'rsa' | 'dsa' | 'ec';
  372. export type KeyFormat = 'pem' | 'der';
  373. interface BasePrivateKeyEncodingOptions<T extends KeyFormat> {
  374. format: T;
  375. cipher: string;
  376. passphrase: string;
  377. }
  378. interface KeyPairKeyObjectResult {
  379. publicKey: KeyObject;
  380. privateKey: KeyObject;
  381. }
  382. interface ECKeyPairKeyObjectOptions {
  383. /**
  384. * Name of the curve to use.
  385. */
  386. namedCurve: string;
  387. }
  388. interface RSAKeyPairKeyObjectOptions {
  389. /**
  390. * Key size in bits
  391. */
  392. modulusLength: number;
  393. /**
  394. * @default 0x10001
  395. */
  396. publicExponent?: number;
  397. }
  398. interface DSAKeyPairKeyObjectOptions {
  399. /**
  400. * Key size in bits
  401. */
  402. modulusLength: number;
  403. /**
  404. * Size of q in bits
  405. */
  406. divisorLength: number;
  407. }
  408. interface RSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
  409. /**
  410. * Key size in bits
  411. */
  412. modulusLength: number;
  413. /**
  414. * @default 0x10001
  415. */
  416. publicExponent?: number;
  417. publicKeyEncoding: {
  418. type: 'pkcs1' | 'spki';
  419. format: PubF;
  420. };
  421. privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
  422. type: 'pkcs1' | 'pkcs8';
  423. };
  424. }
  425. interface DSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
  426. /**
  427. * Key size in bits
  428. */
  429. modulusLength: number;
  430. /**
  431. * Size of q in bits
  432. */
  433. divisorLength: number;
  434. publicKeyEncoding: {
  435. type: 'spki';
  436. format: PubF;
  437. };
  438. privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
  439. type: 'pkcs8';
  440. };
  441. }
  442. interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
  443. /**
  444. * Name of the curve to use.
  445. */
  446. namedCurve: string;
  447. publicKeyEncoding: {
  448. type: 'pkcs1' | 'spki';
  449. format: PubF;
  450. };
  451. privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
  452. type: 'sec1' | 'pkcs8';
  453. };
  454. }
  455. interface KeyPairSyncResult<T1 extends string | Buffer, T2 extends string | Buffer> {
  456. publicKey: T1;
  457. privateKey: T2;
  458. }
  459. function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
  460. function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
  461. function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
  462. function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
  463. function generateKeyPairSync(type: 'rsa', options: RSAKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
  464. function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
  465. function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
  466. function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
  467. function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
  468. function generateKeyPairSync(type: 'dsa', options: DSAKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
  469. function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
  470. function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
  471. function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
  472. function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
  473. function generateKeyPairSync(type: 'ec', options: ECKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
  474. function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
  475. function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
  476. function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
  477. function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
  478. function generateKeyPair(type: 'rsa', options: RSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
  479. function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
  480. function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
  481. function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
  482. function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
  483. function generateKeyPair(type: 'dsa', options: DSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
  484. function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
  485. function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
  486. function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
  487. function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
  488. function generateKeyPair(type: 'ec', options: ECKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
  489. namespace generateKeyPair {
  490. function __promisify__(type: "rsa", options: RSAKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
  491. function __promisify__(type: "rsa", options: RSAKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
  492. function __promisify__(type: "rsa", options: RSAKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
  493. function __promisify__(type: "rsa", options: RSAKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
  494. function __promisify__(type: "rsa", options: RSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
  495. function __promisify__(type: "dsa", options: DSAKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
  496. function __promisify__(type: "dsa", options: DSAKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
  497. function __promisify__(type: "dsa", options: DSAKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
  498. function __promisify__(type: "dsa", options: DSAKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
  499. function __promisify__(type: "dsa", options: DSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
  500. function __promisify__(type: "ec", options: ECKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
  501. function __promisify__(type: "ec", options: ECKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
  502. function __promisify__(type: "ec", options: ECKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
  503. function __promisify__(type: "ec", options: ECKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
  504. function __promisify__(type: "ec", options: ECKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
  505. }
  506. }