@@ -23,6 +23,9 @@ use ed25519::signature::Verifier;
2323
2424pub use sha2:: Sha512 ;
2525
26+ #[ cfg( feature = "pkcs8" ) ]
27+ use ed25519:: pkcs8:: { self , DecodePublicKey } ;
28+
2629#[ cfg( feature = "serde" ) ]
2730use serde:: de:: Error as SerdeError ;
2831#[ cfg( feature = "serde" ) ]
@@ -354,6 +357,72 @@ impl Verifier<ed25519::Signature> for PublicKey {
354357 }
355358}
356359
360+ impl TryFrom < & [ u8 ] > for PublicKey {
361+ type Error = SignatureError ;
362+
363+ fn try_from ( bytes : & [ u8 ] ) -> Result < PublicKey , SignatureError > {
364+ PublicKey :: from_bytes ( bytes)
365+ }
366+ }
367+
368+ #[ cfg( feature = "pkcs8" ) ]
369+ #[ cfg_attr( docsrs, doc( cfg( feature = "pkcs8" ) ) ) ]
370+ impl DecodePublicKey for PublicKey { }
371+
372+ #[ cfg( all( feature = "alloc" , feature = "pkcs8" ) ) ]
373+ #[ cfg_attr( docsrs, doc( cfg( feature = "alloc" , feaure = "pkcs8" ) ) ) ]
374+ impl pkcs8:: EncodePublicKey for PublicKey {
375+ fn to_public_key_der ( & self ) -> pkcs8:: spki:: Result < pkcs8:: Document > {
376+ pkcs8:: PublicKeyBytes :: from ( self ) . to_public_key_der ( )
377+ }
378+ }
379+
380+ #[ cfg( feature = "pkcs8" ) ]
381+ #[ cfg_attr( docsrs, doc( cfg( feature = "pkcs8" ) ) ) ]
382+ impl TryFrom < pkcs8:: PublicKeyBytes > for PublicKey {
383+ type Error = pkcs8:: spki:: Error ;
384+
385+ fn try_from ( pkcs8_key : pkcs8:: PublicKeyBytes ) -> pkcs8:: spki:: Result < Self > {
386+ PublicKey :: try_from ( & pkcs8_key)
387+ }
388+ }
389+
390+ #[ cfg( feature = "pkcs8" ) ]
391+ #[ cfg_attr( docsrs, doc( cfg( feature = "pkcs8" ) ) ) ]
392+ impl TryFrom < & pkcs8:: PublicKeyBytes > for PublicKey {
393+ type Error = pkcs8:: spki:: Error ;
394+
395+ fn try_from ( pkcs8_key : & pkcs8:: PublicKeyBytes ) -> pkcs8:: spki:: Result < Self > {
396+ PublicKey :: from_bytes ( pkcs8_key. as_ref ( ) ) . map_err ( |_| pkcs8:: spki:: Error :: KeyMalformed )
397+ }
398+ }
399+
400+ #[ cfg( feature = "pkcs8" ) ]
401+ #[ cfg_attr( docsrs, doc( cfg( feature = "pkcs8" ) ) ) ]
402+ impl From < PublicKey > for pkcs8:: PublicKeyBytes {
403+ fn from ( public_key : PublicKey ) -> pkcs8:: PublicKeyBytes {
404+ pkcs8:: PublicKeyBytes :: from ( & public_key)
405+ }
406+ }
407+
408+ #[ cfg( feature = "pkcs8" ) ]
409+ #[ cfg_attr( docsrs, doc( cfg( feature = "pkcs8" ) ) ) ]
410+ impl From < & PublicKey > for pkcs8:: PublicKeyBytes {
411+ fn from ( public_key : & PublicKey ) -> pkcs8:: PublicKeyBytes {
412+ pkcs8:: PublicKeyBytes ( public_key. to_bytes ( ) )
413+ }
414+ }
415+
416+ #[ cfg( feature = "pkcs8" ) ]
417+ #[ cfg_attr( docsrs, doc( cfg( feature = "pkcs8" ) ) ) ]
418+ impl TryFrom < pkcs8:: spki:: SubjectPublicKeyInfo < ' _ > > for PublicKey {
419+ type Error = pkcs8:: spki:: Error ;
420+
421+ fn try_from ( public_key : pkcs8:: spki:: SubjectPublicKeyInfo < ' _ > ) -> pkcs8:: spki:: Result < Self > {
422+ pkcs8:: PublicKeyBytes :: try_from ( public_key) ?. try_into ( )
423+ }
424+ }
425+
357426#[ cfg( feature = "serde" ) ]
358427impl Serialize for PublicKey {
359428 fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
0 commit comments