@@ -78,6 +78,7 @@ OAEP is Optimal Asymmetric Encryption Padding.
78
78
Use if you need KEM (encrypt/decrypt).
79
79
80
80
``` ts
81
+ import * as rsa from ' micro-rsa-dsa-dh/rsa.js' ;
81
82
const alice = rsa .keygen (2048 );
82
83
const oaep = rsa .OAEP (sha256 , rsa .mgf1 (sha256 ));
83
84
const msg = new Uint8Array ([1 , 2 , 3 ]);
@@ -90,6 +91,7 @@ deepStrictEqual(oaep.decrypt(alice.privateKey, encrypted), msg);
90
91
Use if you need signatures (sign/verify).
91
92
92
93
``` ts
94
+ import * as rsa from ' micro-rsa-dsa-dh/rsa.js' ;
93
95
const alice = rsa .keygen (2048 );
94
96
const pss = rsa .PSS (sha256 , rsa .mgf1 (sha256 ));
95
97
const msg = new Uint8Array ([1 , 2 , 3 ]);
@@ -104,6 +106,7 @@ This is old standard, OAEP/PSS is better.
104
106
Signatures:
105
107
106
108
``` ts
109
+ import * as rsa from ' micro-rsa-dsa-dh/rsa.js' ;
107
110
const alice = rsa .keygen (2048 );
108
111
const pkcs = rsa .PKCS1_SHA256 ;
109
112
const msg = new Uint8Array ([1 , 2 , 3 ]);
@@ -116,6 +119,7 @@ KEM (vulnerable [[1]](https://crypto.stackexchange.com/questions/12688/can-you-e
116
119
):
117
120
118
121
``` ts
122
+ import * as rsa from ' micro-rsa-dsa-dh/rsa.js' ;
119
123
const alice = rsa .keygen (2048 );
120
124
const pkcs = rsa .PKCS1_KEM ;
121
125
const msg = new Uint8Array ([1 , 2 , 3 ]);
@@ -132,16 +136,17 @@ Same as ECDH, seems safe if pre-defined groups are used. Cons:
132
136
- Using custom non-standard groups can make algorithm weak
133
137
134
138
``` ts
135
- const nobleDH = rsa .DH (' modp18' );
136
- const alicePriv = nobleDH .randomPrivateKey ();
137
- const alicePub = nobleDH .getPublicKey (alicePriv );
139
+ import { DH , DHGroups } from ' micro-rsa-dsa-dh/dh.js' ;
140
+ const dh = DH (' modp18' );
141
+ const alicePriv = dh .randomPrivateKey ();
142
+ const alicePub = dh .getPublicKey (alicePriv );
138
143
139
- const bobPriv = nobleDH .randomPrivateKey ();
140
- const bobPub = nobleDH .getPublicKey (bobPriv );
144
+ const bobPriv = dh .randomPrivateKey ();
145
+ const bobPub = dh .getPublicKey (bobPriv );
141
146
142
147
deepStrictEqual (
143
- nobleDH .getSharedSecret (alicePriv , bobPub ),
144
- nobleDH .getSharedSecret (bobPriv , alicePub )
148
+ dh .getSharedSecret (alicePriv , bobPub ),
149
+ dh .getSharedSecret (bobPriv , alicePub )
145
150
);
146
151
```
147
152
@@ -158,28 +163,29 @@ Same as ECDSA, but with big numbers. Cons:
158
163
- Harder to protect from timing attacks
159
164
160
165
``` ts
166
+ import * as dsa from ' micro-rsa-dsa-dh/dsa.js' ;
161
167
// 1. Params
162
168
// Carol generates random params
163
- const carolParams = rsa .genDSAParams (2048 , 256 , sha256 , 1 );
169
+ const carolParams = dsa .genDSAParams (2048 , 256 , sha256 , 1 );
164
170
// Instead of sending primes to Alice and Bob (which can be insecure), she sends seed
165
171
// This ensures that params are not constructed primes, but generated randomly:
166
172
// Alice and Bob can use these params without trusting Carol.
167
173
const seed = carolParams .domainParameterSeed ;
168
174
169
- const aliceParams = rsa .genDSAParams (2048 , 256 , sha256 , 1 , seed );
175
+ const aliceParams = dsa .genDSAParams (2048 , 256 , sha256 , 1 , seed );
170
176
deepStrictEqual (aliceParams , carolParams ); // Same params as Carol!
171
177
172
- const bobParams = rsa .genDSAParams (2048 , 256 , sha256 , 1 , seed );
178
+ const bobParams = dsa .genDSAParams (2048 , 256 , sha256 , 1 , seed );
173
179
deepStrictEqual (aliceParams , bobParams ); // Now Bob has same params too!
174
180
175
181
// 2. Keys
176
- const aliceDSA = rsa .DSA (aliceParams );
182
+ const aliceDSA = dsa .DSA (aliceParams );
177
183
const alicePrivKey = aliceDSA .randomPrivateKey ();
178
184
const alicePubKey = aliceDSA .getPublicKey (alicePrivKey ); // Alice generates public key and sends to Bob
179
185
const msg = new Uint8Array ([1 , 2 , 3 , 4 , 5 ]);
180
186
const sig = aliceDSA .sign (alicePrivKey , msg ); // Alice signs message
181
187
182
- const bobDSA = rsa .DSA (bobParams );
188
+ const bobDSA = dsa .DSA (bobParams );
183
189
// Now Bob can verify that message was sent by Alice (and not Carol for example).
184
190
deepStrictEqual (bobDSA .verify (alicePubKey , msg , sig ), true );
185
191
```
@@ -189,9 +195,10 @@ deepStrictEqual(bobDSA.verify(alicePubKey, msg, sig), true);
189
195
Mostly for educational purpose: almost nobody uses it.
190
196
191
197
``` ts
198
+ import { ElGamal , genElGamalParams } from ' micro-rsa-dsa-dh/elgamal.js' ;
192
199
// NOTE: this is super slow! 512: 1s, 1024: 20s, 2048: 1046s
193
- const params = rsa . genElGamalParams (512 );
194
- const elgamal = rsa . ElGamal (params );
200
+ const params = genElGamalParams (512 );
201
+ const elgamal = ElGamal (params );
195
202
196
203
const alicePriv = elgamal .randomPrivateKey ();
197
204
const alicePub = elgamal .getPublicKey (alicePriv );
0 commit comments