@@ -28,6 +28,9 @@ pub const PUBLIC_KEY_SIZE: usize = 264;
2828/// Size of an RSA-2048 signature.
2929pub const SIGNATURE_SIZE : usize = 256 ;
3030
31+ /// Size of an RSA key fingerprint (SHA256 hash).
32+ pub const FINGERPRINT_SIZE : usize = 32 ;
33+
3134/// SecretKey contains a 2048-bit RSA private key usable for signing, with SHA256
3235/// as the underlying hash algorithm. Whilst RSA could also be used for encryption,
3336/// that is not exposed on the API as it's not required by the project.
@@ -155,7 +158,7 @@ impl SecretKey {
155158
156159 /// fingerprint returns a 256bit unique identified for this key. For RSA, that
157160 /// is the SHA256 hash of the raw (le modulus || le exponent) public key.
158- pub fn fingerprint ( & self ) -> [ u8 ; 32 ] {
161+ pub fn fingerprint ( & self ) -> Fingerprint {
159162 self . public_key ( ) . fingerprint ( )
160163 }
161164
@@ -259,7 +262,7 @@ impl PublicKey {
259262
260263 /// fingerprint returns a 256bit unique identified for this key. For RSA, that
261264 /// is the SHA256 hash of the raw (le modulus || le exponent) public key.
262- pub fn fingerprint ( & self ) -> [ u8 ; 32 ] {
265+ pub fn fingerprint ( & self ) -> Fingerprint {
263266 let pubkey: RsaPublicKey = self . inner . as_ref ( ) . clone ( ) ;
264267
265268 let mut mod_le = pubkey. n ( ) . to_bytes_le ( ) ;
@@ -270,7 +273,7 @@ impl PublicKey {
270273 let mut hasher = Sha256 :: new ( ) ;
271274 hasher. update ( & mod_le) ;
272275 hasher. update ( & exp_le) ;
273- hasher. finalize ( ) . into ( )
276+ Fingerprint ( hasher. finalize ( ) . into ( ) )
274277 }
275278
276279 /// verify verifies a digital signature.
@@ -310,6 +313,22 @@ impl Signature {
310313 }
311314}
312315
316+ /// Fingerprint contains an RSA key fingerprint (SHA256 hash).
317+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
318+ pub struct Fingerprint ( [ u8 ; FINGERPRINT_SIZE ] ) ;
319+
320+ impl Fingerprint {
321+ /// from_bytes converts a 32-byte array into a fingerprint.
322+ pub fn from_bytes ( bytes : & [ u8 ; FINGERPRINT_SIZE ] ) -> Self {
323+ Self ( * bytes)
324+ }
325+
326+ /// to_bytes converts a fingerprint into a 32-byte array.
327+ pub fn to_bytes ( & self ) -> [ u8 ; FINGERPRINT_SIZE ] {
328+ self . 0
329+ }
330+ }
331+
313332#[ cfg( test) ]
314333mod tests {
315334 use super :: * ;
@@ -557,7 +576,7 @@ fQIDAQAB
557576-----END PUBLIC KEY-----" ,
558577 )
559578 . unwrap ( ) ;
560- assert_eq ! ( hex:: encode( key. fingerprint( ) ) , input) ;
579+ assert_eq ! ( hex:: encode( key. fingerprint( ) . to_bytes ( ) ) , input) ;
561580 }
562581
563582 // Tests signing and verifying messages. Note, this test is not meant to test
0 commit comments