@@ -9,9 +9,9 @@ use super::error;
99use super :: Signature ;
1010
1111use p256:: ecdsa:: { signature:: Signer , signature:: Verifier , SigningKey , VerifyingKey } ;
12- use p256:: elliptic_curve:: rand_core:: { CryptoRng , OsRng , RngCore } ;
12+ use p256:: elliptic_curve:: rand_core:: { CryptoRng , RngCore } ;
1313use p256:: NistP256 ;
14- use std:: hash:: Hash ;
14+ use std:: { convert :: TryInto , hash:: Hash } ;
1515
1616/// pair of cryptographic keys used to sign a token's block
1717#[ derive( Debug , PartialEq ) ]
@@ -21,10 +21,10 @@ pub struct KeyPair {
2121
2222impl KeyPair {
2323 pub fn new ( ) -> Self {
24- Self :: new_with_rng ( & mut OsRng )
24+ Self :: new_with_rng ( & mut rand :: rng ( ) )
2525 }
2626
27- pub fn new_with_rng < T : RngCore + CryptoRng > ( rng : & mut T ) -> Self {
27+ pub fn new_with_rng < T : RngCore + CryptoRng + ? Sized > ( rng : & mut T ) -> Self {
2828 let kp = SigningKey :: random ( rng) ;
2929
3030 KeyPair { kp }
@@ -41,9 +41,13 @@ impl KeyPair {
4141 if bytes. len ( ) != 32 {
4242 return Err ( Format :: InvalidKeySize ( bytes. len ( ) ) ) ;
4343 }
44- let kp = SigningKey :: from_bytes ( bytes. into ( ) )
45- . map_err ( |s| s. to_string ( ) )
46- . map_err ( Format :: InvalidKey ) ?;
44+ let kp = SigningKey :: from_bytes (
45+ bytes
46+ . try_into ( )
47+ . map_err ( |_| Format :: InvalidKeySize ( bytes. len ( ) ) ) ?,
48+ )
49+ . map_err ( |s| s. to_string ( ) )
50+ . map_err ( Format :: InvalidKey ) ?;
4751
4852 Ok ( KeyPair { kp } )
4953 }
@@ -134,15 +138,14 @@ impl PrivateKey {
134138
135139 /// deserializes from a big endian byte array
136140 pub fn from_bytes ( bytes : & [ u8 ] ) -> Result < Self , error:: Format > {
137- // the version of generic-array used by p256 panics if the input length
138- // is incorrect (including when using `.try_into()`)
139- if bytes. len ( ) != 32 {
140- return Err ( Format :: InvalidKeySize ( bytes. len ( ) ) ) ;
141- }
142- SigningKey :: from_bytes ( bytes. into ( ) )
143- . map ( PrivateKey )
144- . map_err ( |s| s. to_string ( ) )
145- . map_err ( Format :: InvalidKey )
141+ SigningKey :: from_bytes (
142+ bytes
143+ . try_into ( )
144+ . map_err ( |_| Format :: InvalidKeySize ( bytes. len ( ) ) ) ?,
145+ )
146+ . map ( PrivateKey )
147+ . map_err ( |s| s. to_string ( ) )
148+ . map_err ( Format :: InvalidKey )
146149 }
147150
148151 /// deserializes from an hex-encoded string
0 commit comments