diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-11-19 15:50:23 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-11-19 15:50:23 +0100 |
commit | 1ddc1dd61b45b41801c1d1c364cd6789314cb8f3 (patch) | |
tree | b34baf591fce5f778cd2f132ead705d39abbff47 /openpgp-ffi/src/serialize.rs | |
parent | cf9b92f5f9238b8322d09833b92c886dda219924 (diff) |
openpgp: Use the builder pattern for stream::Signer.
- See #375.
Diffstat (limited to 'openpgp-ffi/src/serialize.rs')
-rw-r--r-- | openpgp-ffi/src/serialize.rs | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/openpgp-ffi/src/serialize.rs b/openpgp-ffi/src/serialize.rs index e799888c..2b123673 100644 --- a/openpgp-ffi/src/serialize.rs +++ b/openpgp-ffi/src/serialize.rs @@ -15,7 +15,6 @@ extern crate time; use self::openpgp::constants::{ AEADAlgorithm, - HashAlgorithm, SymmetricAlgorithm, }; @@ -143,6 +142,8 @@ pub extern "C" fn pgp_arbitrary_writer_new /// packet, then hashes and emits the data stream, then for every key /// writes a signature packet. /// +/// The signers are consumed. +/// /// The hash is performed using the algorithm specified in /// `hash_algo`. Pass 0 for the default (which is what you usually /// want). @@ -162,18 +163,23 @@ pub extern "C" fn pgp_signer_new let signers = unsafe { slice::from_raw_parts(signers, signers_len) }; - let signers = signers.into_iter().map( - |s| -> &mut dyn self::openpgp::crypto::Signer<_> { - let signer = *s; - ffi_param_ref_mut!(signer).as_mut() - } - ).collect(); - let hash_algo : Option<HashAlgorithm> = if hash_algo == 0 { - None - } else { - Some(hash_algo.into()) - }; - ffi_try_box!(Signer::new(*inner, signers, hash_algo)) + let mut signers = signers.iter().map(|s| { + *ffi_param_move!(*s) + }).collect::<Vec<_>>(); + + let mut signer = + Signer::new(*inner, ffi_try!(signers.pop().ok_or_else(|| { + failure::format_err!("signers is empty") + }))); + for s in signers { + signer = signer.add_signer(s); + } + + if hash_algo != 0 { + signer = ffi_try!(signer.hash_algo(hash_algo.into())); + } + + ffi_try_box!(signer.build()) } /// Creates a signer for a detached signature. @@ -195,18 +201,23 @@ pub extern "C" fn pgp_signer_new_detached let signers = unsafe { slice::from_raw_parts(signers, signers_len) }; - let signers = signers.into_iter().map( - |s| -> &mut dyn self::openpgp::crypto::Signer<_> { - let signer = *s; - ffi_param_ref_mut!(signer).as_mut() - } - ).collect(); - let hash_algo : Option<HashAlgorithm> = if hash_algo == 0 { - None - } else { - Some(hash_algo.into()) - }; - ffi_try_box!(Signer::detached(*inner, signers, hash_algo)) + let mut signers = signers.iter().map(|s| { + *ffi_param_move!(*s) + }).collect::<Vec<_>>(); + + let mut signer = + Signer::new(*inner, ffi_try!(signers.pop().ok_or_else(|| { + failure::format_err!("signers is empty") + }))); + for s in signers { + signer = signer.add_signer(s); + } + + if hash_algo != 0 { + signer = ffi_try!(signer.hash_algo(hash_algo.into())); + } + + ffi_try_box!(signer.detached().build()) } /// Writes a literal data packet. |