@@ -203,8 +203,10 @@ impl PublicKey {
203
203
204
204
#[ cfg( test) ]
205
205
mod tests {
206
+ use std:: io:: Read ;
207
+
206
208
use super :: * ;
207
- use rand:: rngs :: adapter :: ReadRng ;
209
+ use rand:: RngCore ;
208
210
209
211
const SEED : & [ u8 ; 64 ] = b"\x03 \x2e \x45 \x32 \x6f \xa8 \x59 \xa7 \x2e \xc2 \x35 \xac \xff \x92 \x9b \x15 \xd1 \
210
212
\x37 \x2e \x30 \xb2 \x07 \x25 \x5f \x06 \x11 \xb8 \xf7 \x85 \xd7 \x64 \x37 \x41 \x52 \xe0 \xac \x00 \x9e \x50 \x9e \
@@ -218,6 +220,39 @@ mod tests {
218
220
\x7a \x25 \x19 \x29 \x85 \xa8 \x42 \xdb \xff \x8e \x13 \xef \xee \x5b \x7e \x7e \x55 \xbb \xe4 \xd3 \x89 \x64 \x7c \
219
221
\x68 \x6a \x9a \x9a \xb3 \xfb \x88 \x9b \x2d \x77 \x67 \xd3 \x83 \x7e \xea \x4e \x0a \x2f \x04 ";
220
222
223
+ /// Replacement for the deprecated `rand::ReadRng`.
224
+ struct Seed < ' a > ( & ' a [ u8 ] ) ;
225
+
226
+ impl < ' a > RngCore for Seed < ' a > {
227
+ fn next_u32 ( & mut self ) -> u32 {
228
+ let mut buf = [ 0 ; 4 ] ;
229
+ self . fill_bytes ( & mut buf) ;
230
+ u32:: from_le_bytes ( buf)
231
+ }
232
+
233
+ fn next_u64 ( & mut self ) -> u64 {
234
+ let mut buf = [ 0 ; 8 ] ;
235
+ self . fill_bytes ( & mut buf) ;
236
+ u64:: from_le_bytes ( buf)
237
+ }
238
+
239
+ fn fill_bytes ( & mut self , dest : & mut [ u8 ] ) {
240
+ self . try_fill_bytes ( dest) . unwrap_or_else ( |err| {
241
+ panic ! (
242
+ "reading random bytes from Read implementation failed; error: {}" ,
243
+ err
244
+ )
245
+ } ) ;
246
+ }
247
+
248
+ fn try_fill_bytes ( & mut self , dest : & mut [ u8 ] ) -> Result < ( ) , rand:: Error > {
249
+ if dest. is_empty ( ) {
250
+ return Ok ( ( ) ) ;
251
+ }
252
+ self . 0 . read_exact ( dest) . map_err ( |e| rand:: Error :: new ( e) )
253
+ }
254
+ }
255
+
221
256
#[ test]
222
257
fn mgf1 ( ) {
223
258
let mask = Pkcs1OaepPadding :: < ( ) > :: mgf1 ( & SEED [ ..] , 128 ) ;
@@ -292,7 +327,7 @@ mod tests {
292
327
BigUint :: from_bytes_be ( & exponent) ,
293
328
) ;
294
329
295
- let rng = ReadRng :: new ( & * seed1) ;
330
+ let rng = Seed ( & * seed1) ;
296
331
let pad = Pkcs1Padding :: new ( rng) ;
297
332
298
333
let cipher_text = public_key. encrypt_block ( msg1, pad) ;
@@ -344,7 +379,7 @@ mod tests {
344
379
BigUint :: from_bytes_be ( & exponent) ,
345
380
) ;
346
381
347
- let rng = ReadRng :: new ( & * seed) ;
382
+ let rng = Seed ( & * seed) ;
348
383
let pad = Pkcs1OaepPadding :: new ( rng) ;
349
384
350
385
let cipher_text = public_key. encrypt_block ( msg, pad) ;
0 commit comments