Skip to content

Commit bfd4900

Browse files
robhoganfacebook-github-bot
authored andcommitted
Align Flow lib defs for Node.js crypto with v24
Summary: This is an AI-assisted change to align the Flow definitions for the `crypto` module with the Node.js docs as at v24. **New v24 APIs:** 1. **`hash(algorithm, data, [outputEncoding])`** - One-shot hashing convenience function - Added in Node.js v21.7.0, v20.12.0 - https://nodejs.org/api/crypto.html#cryptohashalgorithm-data-outputencoding 2. **X509Certificate new properties** (added in v22.10.0): - `validFromDate` - Certificate valid-from as Date object - `validToDate` - Certificate valid-to as Date object - https://nodejs.org/api/crypto.html#x509certificatevalidfromdate - https://nodejs.org/api/crypto.html#x509certificatevalidtodate **New Classes:** 3. **KeyObject** - Represents cryptographic keys (symmetric/asymmetric) - Properties: `type`, `asymmetricKeyType`, `asymmetricKeySize`, `symmetricKeySize` - Methods: `export()`, `equals()` - https://nodejs.org/api/crypto.html#class-keyobject 4. **X509Certificate** - X.509 certificate handling - Properties: `ca`, `fingerprint*`, `issuer`, `subject`, `publicKey`, `raw`, etc. - Methods: `checkEmail()`, `checkHost()`, `checkIP()`, `verify()`, etc. - https://nodejs.org/api/crypto.html#class-x509certificate 5. **Certificate** - Legacy SPKAC (Signed Public Key and Challenge) support - Static methods: `exportChallenge()`, `exportPublicKey()`, `verifySpkac()` - https://nodejs.org/api/crypto.html#class-certificate **Key Management Functions:** 6. **Key Creation:** - `createSecretKey(key, [encoding])` - Create symmetric KeyObject - `createPublicKey(key)` - Create public KeyObject - `createPrivateKey(key)` - Create private KeyObject - https://nodejs.org/api/crypto.html#cryptocreatesecretkeykey-encoding - https://nodejs.org/api/crypto.html#cryptocreatepublickeykey - https://nodejs.org/api/crypto.html#cryptocreateprivatekeykey 7. **Key Generation:** - `generateKeyPair()` / `generateKeyPairSync()` - Generate asymmetric key pairs - `generateKey()` / `generateKeySync()` - Generate symmetric keys - Supports: RSA, RSA-PSS, DSA, EC, Ed25519, Ed448, X25519, X448 - https://nodejs.org/api/crypto.html#cryptogeneratekeypairtype-options-callback - https://nodejs.org/api/crypto.html#cryptogeneratekeytype-options-callback 8. **Primality Testing:** - `checkPrime()` / `checkPrimeSync()` - Test if candidate is prime - https://nodejs.org/api/crypto.html#cryptocheckprimecandidate-options-callback **Other Improvements:** 9. **Hash.copy()** - Create deep copy of Hash object - https://nodejs.org/api/crypto.html#hashcopyoptions 10. **webcrypto property** - References web Crypto type - Web Crypto API types are NOT redefined (use existing definitions where available) - https://nodejs.org/api/webcrypto.html **Type Safety:** - All options objects use modern `Readonly<{...}>` syntax for inputs - Exact-by-default types throughout - Proper overloading for functions with optional parameters **References:** - Node.js crypto module docs: https://nodejs.org/api/crypto.html - Node.js Web Crypto API: https://nodejs.org/api/webcrypto.html Changelog: [Internal] --- > Generated by [Confucius Code Assist (CCA)](https://www.internalfb.com/wiki/Confucius/Analect/Shared_Analects/Confucius_Code_Assist_(CCA)/) [Confucius Session](https://www.internalfb.com/confucius?host=devvm45708.cln0.facebook.com&port=8086&tab=Chat&session_id=1a3aa26e-e5a9-11f0-8d47-71a4a90f0494&entry_name=Code+Assist), [Trace](https://www.internalfb.com/confucius?session_id=1a3aa26e-e5a9-11f0-8d47-71a4a90f0494&tab=Trace) Reviewed By: vzaidman Differential Revision: D89934145
1 parent 07d2255 commit bfd4900

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

flow-typed/environment/node.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ declare class crypto$Hash extends stream$Duplex {
603603
data: string | Buffer,
604604
input_encoding?: 'utf8' | 'ascii' | 'latin1' | 'binary',
605605
): crypto$Hash;
606+
copy(options?: mixed): crypto$Hash;
606607
}
607608

608609
declare class crypto$Hmac extends stream$Duplex {
@@ -661,6 +662,83 @@ type crypto$key =
661662
...
662663
};
663664

665+
declare class crypto$KeyObject {
666+
+asymmetricKeyType?:
667+
| 'rsa'
668+
| 'rsa-pss'
669+
| 'dsa'
670+
| 'ec'
671+
| 'ed25519'
672+
| 'ed448'
673+
| 'x25519'
674+
| 'x448';
675+
+asymmetricKeySize?: number;
676+
+symmetricKeySize?: number;
677+
+type: 'secret' | 'public' | 'private';
678+
679+
export(
680+
options: Readonly<{
681+
type: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1',
682+
format: 'pem',
683+
}>,
684+
): string;
685+
export(
686+
options: Readonly<{
687+
type: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1',
688+
format: 'der',
689+
}>,
690+
): Buffer;
691+
export(options: Readonly<{format: 'jwk'}>): mixed;
692+
equals(otherKeyObject: crypto$KeyObject): boolean;
693+
}
694+
695+
declare class crypto$X509Certificate {
696+
constructor(buffer: string | Buffer | $TypedArray | DataView): void;
697+
698+
+ca: boolean;
699+
+fingerprint: string;
700+
+fingerprint256: string;
701+
+fingerprint512: string;
702+
+issuer: string;
703+
+issuerCertificate?: crypto$X509Certificate;
704+
+keyUsage: Array<string>;
705+
+publicKey: crypto$KeyObject;
706+
+raw: Buffer;
707+
+serialNumber: string;
708+
+subject: string;
709+
+subjectAltName: string;
710+
+validFrom: string;
711+
+validTo: string;
712+
+validFromDate: Date;
713+
+validToDate: Date;
714+
715+
checkEmail(
716+
email: string,
717+
options?: Readonly<{subject?: 'always' | 'default' | 'never'}>,
718+
): string | void;
719+
checkHost(
720+
name: string,
721+
options?: Readonly<{subject?: 'always' | 'default' | 'never'}>,
722+
): string | void;
723+
checkIP(ip: string): string | void;
724+
checkIssued(otherCert: crypto$X509Certificate): boolean;
725+
checkPrivateKey(privateKey: crypto$KeyObject): boolean;
726+
toJSON(): string;
727+
toLegacyObject(): mixed;
728+
toString(): string;
729+
verify(publicKey: crypto$KeyObject): boolean;
730+
}
731+
732+
declare class crypto$Certificate {
733+
static exportChallenge(
734+
spkac: string | Buffer | $TypedArray | DataView,
735+
): Buffer;
736+
static exportPublicKey(
737+
spkac: string | Buffer | $TypedArray | DataView,
738+
): Buffer;
739+
static verifySpkac(spkac: Buffer | $TypedArray | DataView): boolean;
740+
}
741+
664742
declare module 'crypto' {
665743
declare var DEFAULT_ENCODING: string;
666744

@@ -820,6 +898,79 @@ declare module 'crypto' {
820898
a: Buffer | $TypedArray | DataView,
821899
b: Buffer | $TypedArray | DataView,
822900
): boolean;
901+
declare function hash(
902+
algorithm: string,
903+
data: string | Buffer | $TypedArray | DataView,
904+
): Buffer;
905+
declare function hash(
906+
algorithm: string,
907+
data: string | Buffer | $TypedArray | DataView,
908+
outputEncoding: buffer$Encoding,
909+
): string;
910+
declare function createSecretKey(
911+
key: Buffer | $TypedArray | DataView,
912+
): crypto$KeyObject;
913+
declare function createSecretKey(
914+
key: string,
915+
encoding: buffer$Encoding,
916+
): crypto$KeyObject;
917+
declare function createPublicKey(
918+
key: string | Buffer | crypto$KeyObject | mixed,
919+
): crypto$KeyObject;
920+
declare function createPrivateKey(
921+
key: string | Buffer | mixed,
922+
): crypto$KeyObject;
923+
declare function generateKeyPair(
924+
type:
925+
| 'rsa'
926+
| 'rsa-pss'
927+
| 'dsa'
928+
| 'ec'
929+
| 'ed25519'
930+
| 'ed448'
931+
| 'x25519'
932+
| 'x448',
933+
options: mixed,
934+
callback: (
935+
err: ?Error,
936+
publicKey: crypto$KeyObject,
937+
privateKey: crypto$KeyObject,
938+
) => void,
939+
): void;
940+
declare function generateKeyPairSync(
941+
type:
942+
| 'rsa'
943+
| 'rsa-pss'
944+
| 'dsa'
945+
| 'ec'
946+
| 'ed25519'
947+
| 'ed448'
948+
| 'x25519'
949+
| 'x448',
950+
options: mixed,
951+
): {publicKey: crypto$KeyObject, privateKey: crypto$KeyObject, ...};
952+
declare function generateKey(
953+
type: 'hmac' | 'aes',
954+
options: Readonly<{length: number}>,
955+
callback: (err: ?Error, key: crypto$KeyObject) => void,
956+
): void;
957+
declare function generateKeySync(
958+
type: 'hmac' | 'aes',
959+
options: Readonly<{length: number}>,
960+
): crypto$KeyObject;
961+
declare function checkPrime(
962+
candidate: Buffer | $TypedArray | DataView | bigint,
963+
options?: Readonly<{checks?: number}>,
964+
callback: (err: ?Error, result: boolean) => void,
965+
): void;
966+
declare function checkPrimeSync(
967+
candidate: Buffer | $TypedArray | DataView | bigint,
968+
options?: Readonly<{checks?: number}>,
969+
): boolean;
970+
declare class Certificate extends crypto$Certificate {}
971+
declare class X509Certificate extends crypto$X509Certificate {}
972+
declare class KeyObject extends crypto$KeyObject {}
973+
declare var webcrypto: unknown;
823974
}
824975

825976
type net$Socket$address = {

0 commit comments

Comments
 (0)