summaryrefslogtreecommitdiffstats
path: root/openpgp/src/serialize.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-10-06 13:24:04 +0200
committerJustus Winter <justus@sequoia-pgp.org>2020-10-06 14:56:16 +0200
commit48d18fdff5f24526c3e83789f1ebfad763fa94af (patch)
treed0aa424d9589f28a2ead700a7f5b59efe600bd4d /openpgp/src/serialize.rs
parent93d84877304486bfcb2be664a1565f835bd60cf3 (diff)
openpgp: Implement two-octet checksums over secret key material.
- Also, rename methods to be more explicit.
Diffstat (limited to 'openpgp/src/serialize.rs')
-rw-r--r--openpgp/src/serialize.rs27
1 files changed, 20 insertions, 7 deletions
diff --git a/openpgp/src/serialize.rs b/openpgp/src/serialize.rs
index 800dceb7..a7caab66 100644
--- a/openpgp/src/serialize.rs
+++ b/openpgp/src/serialize.rs
@@ -1094,16 +1094,29 @@ impl MarshalInto for crypto::mpi::SecretKeyMaterial {
impl crypto::mpi::SecretKeyMaterial {
/// Writes this secret key with a checksum to `w`.
- pub fn serialize_chksumd<W: io::Write>(&self, w: &mut W) -> Result<()> {
+ pub fn serialize_with_checksum<W: io::Write>(
+ &self, w: &mut W,
+ checksum: crypto::mpi::SecretKeyChecksum)
+ -> Result<()>
+ {
// First, the MPIs.
self.serialize(w)?;
- // The checksum is SHA1 over the serialized MPIs.
- let mut hash = HashAlgorithm::SHA1.context().unwrap();
- self.serialize(&mut hash)?;
- let mut digest = [0u8; 20];
- hash.digest(&mut digest);
- w.write_all(&digest)?;
+ match checksum {
+ crypto::mpi::SecretKeyChecksum::SHA1 => {
+ // The checksum is SHA1 over the serialized MPIs.
+ let mut hash = HashAlgorithm::SHA1.context().unwrap();
+ self.serialize(&mut hash)?;
+ let mut digest = [0u8; 20];
+ hash.digest(&mut digest);
+ w.write_all(&digest)?;
+ },
+ crypto::mpi::SecretKeyChecksum::Sum16 => {
+ w.write_all(&self.to_vec()?.iter()
+ .fold(0u16, |acc, v| acc.wrapping_add(*v as u16))
+ .to_be_bytes())?;
+ },
+ }
Ok(())
}