From 92b5aa4e6067d1566b2759385dcfec966b6700c1 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 20 Dec 2018 17:17:36 +0100 Subject: openpgp: Implement serialize_chksumd for SecretKey. --- openpgp/src/serialize/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'openpgp') diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index 482df71d..75a1c940 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -357,6 +357,26 @@ impl Serialize for crypto::mpis::SecretKey { } } +impl crypto::mpis::SecretKey { + /// Writes this secret key with a checksum to `w`. + pub fn serialize_chksumd(&self, w: &mut W) -> Result<()> { + use nettle::Hash; + use nettle::hash::insecure_do_not_use::Sha1; + + // First, the MPIs. + self.serialize(w)?; + + // The checksum is SHA1 over the serialized MPIs. + let mut hash = Sha1::default(); + self.serialize(&mut hash)?; + let mut digest = [0u8; 20]; + hash.digest(&mut digest); + w.write_all(&digest)?; + + Ok(()) + } +} + impl Serialize for crypto::mpis::Ciphertext { fn serialize(&self, w: &mut W) -> Result<()> { use crypto::mpis::Ciphertext::*; -- cgit v1.2.3