1 |
|
2 |
|
3 | declare class NodeRSA {
|
4 | |
5 |
|
6 |
|
7 | constructor(key?: NodeRSA.KeyBits);
|
8 |
|
9 | /**
|
10 | * Load key from string/buffer/components.
|
11 | */
|
12 | constructor(key: NodeRSA.Key, format?: NodeRSA.Format, options?: NodeRSA.Options);
|
13 |
|
14 | /**
|
15 | * Set and validate options for key instance.
|
16 | */
|
17 | setOptions(options: NodeRSA.Options): void;
|
18 |
|
19 | /**
|
20 | * @param bits Key size in bits. 2048 by default.
|
21 | * @param exponent public exponent. 65537 by default.
|
22 | */
|
23 | generateKeyPair(bits?: number, exponent?: number): NodeRSA;
|
24 |
|
25 | /**
|
26 | * Import key from PEM string, PEM/DER Buffer or components.
|
27 | */
|
28 | importKey(key: NodeRSA.Key, format?: NodeRSA.Format): NodeRSA;
|
29 |
|
30 | /**
|
31 | * Export key to PEM string, PEM/DER Buffer or components.
|
32 | */
|
33 | exportKey(format?: NodeRSA.FormatPem): string;
|
34 | exportKey(format: NodeRSA.FormatDer): Buffer;
|
35 | exportKey(format: NodeRSA.FormatComponentsPrivate): NodeRSA.KeyComponentsPrivate;
|
36 | exportKey(format: NodeRSA.FormatComponentsPublic): NodeRSA.KeyComponentsPublic;
|
37 |
|
38 | isPrivate(): boolean;
|
39 |
|
40 | /**
|
41 | * @param strict if true method will return false if key pair have private exponent. Default false.
|
42 | */
|
43 | isPublic(strict?: boolean): boolean;
|
44 |
|
45 | /**
|
46 | * Return true if key pair doesn't have any data.
|
47 | */
|
48 | isEmpty(): boolean;
|
49 |
|
50 | /**
|
51 | * Return key size in bits.
|
52 | */
|
53 | getKeySize(): number;
|
54 |
|
55 | /**
|
56 | * Return max data size for encrypt in bytes.
|
57 | */
|
58 | getMaxMessageSize(): number;
|
59 |
|
60 | encrypt(data: NodeRSA.Data, encoding?: "buffer"): Buffer;
|
61 | encrypt(data: NodeRSA.Data, encoding: NodeRSA.Encoding): string;
|
62 | encrypt(data: Buffer, encoding: "buffer", sourceEncoding?: NodeRSA.Encoding): Buffer;
|
63 | encrypt(data: Buffer, encoding: NodeRSA.Encoding, sourceEncoding?: NodeRSA.Encoding): string;
|
64 |
|
65 | encryptPrivate(data: NodeRSA.Data, encoding?: "buffer"): Buffer;
|
66 | encryptPrivate(data: NodeRSA.Data, encoding: NodeRSA.Encoding): string;
|
67 | encryptPrivate(data: Buffer, encoding: "buffer", sourceEncoding?: NodeRSA.Encoding): Buffer;
|
68 | encryptPrivate(data: Buffer, encoding: NodeRSA.Encoding, sourceEncoding?: NodeRSA.Encoding): string;
|
69 |
|
70 | decrypt(data: Buffer | string, encoding?: "buffer"): Buffer;
|
71 | decrypt(data: Buffer | string, encoding: NodeRSA.Encoding): string;
|
72 | decrypt<T extends object>(data: Buffer | string, encoding: "json"): T;
|
73 |
|
74 | decryptPublic(data: Buffer | string, encoding?: "buffer"): Buffer;
|
75 | decryptPublic(data: Buffer | string, encoding: NodeRSA.Encoding): string;
|
76 | decryptPublic<T extends object>(data: Buffer | string, encoding: "json"): T;
|
77 |
|
78 | sign(data: NodeRSA.Data, encoding?: "buffer"): Buffer;
|
79 | sign(data: NodeRSA.Data, encoding: NodeRSA.Encoding): string;
|
80 | sign(data: Buffer, encoding: "buffer", sourceEncoding?: NodeRSA.Encoding): Buffer;
|
81 | sign(data: Buffer, encoding: NodeRSA.Encoding, sourceEncoding?: NodeRSA.Encoding): string;
|
82 |
|
83 | verify(data: NodeRSA.Data, signature: Buffer): boolean;
|
84 | verify(data: Buffer, signature: Buffer, sourceEncoding?: NodeRSA.Encoding): boolean;
|
85 | verify(
|
86 | data: Buffer,
|
87 | signature: string,
|
88 | sourceEncoding: NodeRSA.Encoding,
|
89 | signatureEncoding: NodeRSA.Encoding,
|
90 | ): boolean;
|
91 | verify(
|
92 | data: NodeRSA.Data,
|
93 | signature: string,
|
94 | sourceEncoding: undefined,
|
95 | signatureEncoding: NodeRSA.Encoding,
|
96 | ): boolean;
|
97 | }
|
98 |
|
99 | declare namespace NodeRSA {
|
100 | type Key = string | Buffer | KeyComponentsPrivate | KeyComponentsPublic;
|
101 | type Data = string | object | any[];
|
102 |
|
103 | type FormatPem =
|
104 | | "private"
|
105 | | "public"
|
106 | | "pkcs1"
|
107 | | "pkcs1-pem"
|
108 | | "pkcs1-private"
|
109 | | "pkcs1-private-pem"
|
110 | | "pkcs1-public"
|
111 | | "pkcs1-public-pem"
|
112 | | "pkcs8"
|
113 | | "pkcs8-pem"
|
114 | | "pkcs8-private"
|
115 | | "pkcs8-private-pem"
|
116 | | "pkcs8-public"
|
117 | | "pkcs8-public-pem"
|
118 | | "openssh-public"
|
119 | | "openssh-private";
|
120 | type FormatDer =
|
121 | | "pkcs1-der"
|
122 | | "pkcs1-private-der"
|
123 | | "pkcs1-public-der"
|
124 | | "pkcs8-der"
|
125 | | "pkcs8-private-der"
|
126 | | "pkcs8-public-der";
|
127 | type FormatComponentsPrivate =
|
128 | | "components"
|
129 | | "components-pem"
|
130 | | "components-der"
|
131 | | "components-private"
|
132 | | "components-private-pem"
|
133 | | "components-private-der";
|
134 | type FormatComponentsPublic = "components-public" | "components-public-pem" | "components-public-der";
|
135 | type Format = FormatPem | FormatDer | FormatComponentsPrivate | FormatComponentsPublic;
|
136 |
|
137 | type EncryptionScheme = "pkcs1_oaep" | "pkcs1";
|
138 |
|
139 | type HashingAlgorithm = "ripemd160" | "md4" | "md5" | "sha1" | "sha224" | "sha256" | "sha384" | "sha512";
|
140 |
|
141 | type SigningScheme = "pkcs1" | "pss";
|
142 |
|
143 | type SigningSchemeHash =
|
144 | | "pkcs1-ripemd160"
|
145 | | "pkcs1-md4"
|
146 | | "pkcs1-md5"
|
147 | | "pkcs1-sha"
|
148 | | "pkcs1-sha1"
|
149 | | "pkcs1-sha224"
|
150 | | "pkcs1-sha256"
|
151 | | "pkcs1-sha384"
|
152 | | "pkcs1-sha512"
|
153 | | "pss-ripemd160"
|
154 | | "pss-md4"
|
155 | | "pss-md5"
|
156 | | "pss-sha"
|
157 | | "pss-sha1"
|
158 | | "pss-sha224"
|
159 | | "pss-sha256"
|
160 | | "pss-sha384"
|
161 | | "pss-sha512";
|
162 |
|
163 | type Encoding = "ascii" | "utf8" | "utf16le" | "ucs2" | "latin1" | "base64" | "hex" | "binary" | "buffer";
|
164 |
|
165 | interface KeyComponentsPrivate {
|
166 | n: Buffer;
|
167 | e: Buffer | number;
|
168 | d: Buffer;
|
169 | p: Buffer;
|
170 | q: Buffer;
|
171 | dmp1: Buffer;
|
172 | dmq1: Buffer;
|
173 | coeff: Buffer;
|
174 | }
|
175 |
|
176 | interface KeyComponentsPublic {
|
177 | n: Buffer;
|
178 | e: Buffer | number;
|
179 | }
|
180 |
|
181 | interface KeyBits {
|
182 | |
183 |
|
184 |
|
185 | b: number;
|
186 | }
|
187 |
|
188 | interface AdvancedEncryptionSchemePKCS1 {
|
189 | scheme: "pkcs1";
|
190 | padding: number;
|
191 | }
|
192 |
|
193 | interface AdvancedEncryptionSchemePKCS1OAEP {
|
194 | scheme: "pkcs1_oaep";
|
195 | hash: HashingAlgorithm;
|
196 |
|
197 | |
198 |
|
199 |
|
200 | mgf?(data: Buffer, length: number, hash: HashingAlgorithm): Buffer;
|
201 | }
|
202 |
|
203 | type AdvancedEncryptionScheme = AdvancedEncryptionSchemePKCS1 | AdvancedEncryptionSchemePKCS1OAEP;
|
204 |
|
205 | interface AdvancedSigningSchemePSS {
|
206 | scheme: "pss";
|
207 | hash: HashingAlgorithm;
|
208 | saltLength: number;
|
209 | }
|
210 |
|
211 | interface AdvancedSigningSchemePKCS1 {
|
212 | scheme: "pkcs1";
|
213 | hash: HashingAlgorithm;
|
214 | }
|
215 |
|
216 | type AdvancedSigningScheme = AdvancedSigningSchemePSS | AdvancedSigningSchemePKCS1;
|
217 |
|
218 | interface Options {
|
219 | |
220 |
|
221 |
|
222 | environment?: "browser" | "node" | undefined;
|
223 |
|
224 | |
225 |
|
226 |
|
227 | encryptionScheme?: EncryptionScheme | AdvancedEncryptionScheme | undefined;
|
228 |
|
229 | |
230 |
|
231 |
|
232 | signingScheme?: SigningScheme | SigningSchemeHash | AdvancedSigningScheme | undefined;
|
233 | }
|
234 | }
|
235 |
|
236 | export = NodeRSA;
|