summaryrefslogtreecommitdiffstats
path: root/openpgp
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2020-06-19 11:34:04 +0200
committerNeal H. Walfield <neal@pep.foundation>2020-06-19 15:08:56 +0200
commite0ce9ef05dacd25bcc824d1d016f8765548b693b (patch)
tree4d92867b66a5370496186f6a60b512daa3cb2907 /openpgp
parent1be399cf5fc87b0cab6958d489bf854e48fbf737 (diff)
openpgp: Refactor.
- Reduce the amount of redundant code.
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/src/packet/signature/mod.rs59
1 files changed, 31 insertions, 28 deletions
diff --git a/openpgp/src/packet/signature/mod.rs b/openpgp/src/packet/signature/mod.rs
index 076463a4..9454a0d8 100644
--- a/openpgp/src/packet/signature/mod.rs
+++ b/openpgp/src/packet/signature/mod.rs
@@ -228,9 +228,10 @@ impl SignatureBuilder {
pub fn sign_standalone(mut self, signer: &mut dyn Signer)
-> Result<Signature>
{
- self.sort();
- self.pk_algo = signer.public().pk_algo();
+ self.pre_sign(signer);
+
let digest = Signature::hash_standalone(&self)?;
+
self.sign(signer, digest)
}
@@ -243,9 +244,10 @@ impl SignatureBuilder {
pub fn sign_timestamp(mut self, signer: &mut dyn Signer)
-> Result<Signature>
{
- self.sort();
- self.pk_algo = signer.public().pk_algo();
+ self.pre_sign(signer);
+
let digest = Signature::hash_timestamp(&self)?;
+
self.sign(signer, digest)
}
@@ -258,12 +260,10 @@ impl SignatureBuilder {
pub fn sign_direct_key(mut self, signer: &mut dyn Signer)
-> Result<Signature>
{
- self.sort();
- self.pk_algo = signer.public().pk_algo();
- let digest =
- Signature::hash_direct_key(&self,
- signer.public()
- .role_as_primary())?;
+ self.pre_sign(signer);
+
+ let digest = Signature::hash_direct_key(
+ &self, signer.public().role_as_primary())?;
self.sign(signer, digest)
}
@@ -280,8 +280,8 @@ impl SignatureBuilder {
-> Result<Signature>
where P: key::KeyParts,
{
- self.sort();
- self.pk_algo = signer.public().pk_algo();
+ self.pre_sign(signer);
+
let digest = Signature::hash_userid_binding(&self, key, userid)?;
self.sign(signer, digest)
@@ -300,8 +300,8 @@ impl SignatureBuilder {
where P: key:: KeyParts,
Q: key:: KeyParts,
{
- self.sort();
- self.pk_algo = signer.public().pk_algo();
+ self.pre_sign(signer);
+
let digest = Signature::hash_subkey_binding(&self, primary, subkey)?;
self.sign(signer, digest)
@@ -322,10 +322,11 @@ impl SignatureBuilder {
where P: key:: KeyParts,
Q: key:: KeyParts,
{
- self.sort();
- self.pk_algo = subkey_signer.public().pk_algo();
+ self.pre_sign(subkey_signer);
+
let digest =
Signature::hash_primary_key_binding(&self, primary, subkey)?;
+
self.sign(subkey_signer, digest)
}
@@ -342,8 +343,8 @@ impl SignatureBuilder {
-> Result<Signature>
where P: key::KeyParts,
{
- self.sort();
- self.pk_algo = signer.public().pk_algo();
+ self.pre_sign(signer);
+
let digest =
Signature::hash_user_attribute_binding(&self, key, ua)?;
@@ -352,6 +353,8 @@ impl SignatureBuilder {
/// Signs `hash` using `signer`.
///
+ /// This sets the hash algorithm to whatever 'hash's algorithm is.
+ ///
/// The Signature's public-key algorithm field is set to the
/// algorithm used by `signer`.
/// If not set before, Issuer and Issuer Fingerprint subpackets are added
@@ -360,13 +363,11 @@ impl SignatureBuilder {
mut hash: hash::Context)
-> Result<Signature>
{
- self.sort();
- // Fill out some fields, then hash the packet.
- self.pk_algo = signer.public().pk_algo();
self.hash_algo = hash.algo();
- self.hash(&mut hash);
- // Compute the digest.
+ self.pre_sign(signer);
+
+ self.hash(&mut hash);
let mut digest = vec![0u8; hash.digest_size()];
hash.digest(&mut digest);
@@ -383,22 +384,24 @@ impl SignatureBuilder {
-> Result<Signature>
where M: AsRef<[u8]>
{
- self.sort();
// Hash the message
let mut hash = self.hash_algo.context()?;
hash.update(msg.as_ref());
- // Fill out some fields, then hash the packet.
- self.pk_algo = signer.public().pk_algo();
- self.hash(&mut hash);
+ self.pre_sign(signer);
- // Compute the digest.
+ self.hash(&mut hash);
let mut digest = vec![0u8; hash.digest_size()];
hash.digest(&mut digest);
self.sign(signer, digest)
}
+ fn pre_sign(&mut self, signer: &dyn Signer) {
+ self.pk_algo = signer.public().pk_algo();
+ self.sort();
+ }
+
fn sign(self, signer: &mut dyn Signer, digest: Vec<u8>)
-> Result<Signature>
{