summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-01-22 16:36:24 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-01-22 17:47:45 +0100
commit35567167e9a40dff7c40d46650ba120e4c72f7ae (patch)
treef4ccec7fd29cb53c909d66a34c0ed932c07bbefb
parent865096185b5d52fc98f799b2d2b8cb5ef62bdc02 (diff)
openpgp-ffi: Wrap KeyID.
-rw-r--r--ffi/lang/python/sequoia/openpgp.py1
-rw-r--r--openpgp-ffi/include/sequoia/openpgp.h6
-rw-r--r--openpgp-ffi/src/keyid.rs73
3 files changed, 28 insertions, 52 deletions
diff --git a/ffi/lang/python/sequoia/openpgp.py b/ffi/lang/python/sequoia/openpgp.py
index 4432ec13..404d9797 100644
--- a/ffi/lang/python/sequoia/openpgp.py
+++ b/ffi/lang/python/sequoia/openpgp.py
@@ -9,6 +9,7 @@ class KeyID(SQObject):
_del = lib.pgp_keyid_free
_clone = lib.pgp_keyid_clone
_str = lib.pgp_keyid_to_string
+ _debug = lib.pgp_keyid_debug
_eq = lib.pgp_keyid_equal
_hash = lib.pgp_keyid_hash
diff --git a/openpgp-ffi/include/sequoia/openpgp.h b/openpgp-ffi/include/sequoia/openpgp.h
index 4c1e9997..29613aab 100644
--- a/openpgp-ffi/include/sequoia/openpgp.h
+++ b/openpgp-ffi/include/sequoia/openpgp.h
@@ -80,6 +80,12 @@ uint64_t pgp_keyid_hash (pgp_keyid_t keyid);
char *pgp_keyid_to_string (const pgp_keyid_t fp);
/*/
+/// Returns a human readable description of this object suitable for
+/// debugging.
+/*/
+char *pgp_keyid_debug (const pgp_keyid_t fp);
+
+/*/
/// Converts the KeyID to a hexadecimal number.
/*/
char *pgp_keyid_to_hex (const pgp_keyid_t keyid);
diff --git a/openpgp-ffi/src/keyid.rs b/openpgp-ffi/src/keyid.rs
index 2e428ea9..202335f1 100644
--- a/openpgp-ffi/src/keyid.rs
+++ b/openpgp-ffi/src/keyid.rs
@@ -10,14 +10,26 @@
//!
//! [`sequoia-openpgp::KeyID`]: ../../sequoia_openpgp/enum.KeyID.html
-use std::hash::{Hash, Hasher};
use std::ptr;
use std::slice;
-use libc::{uint8_t, uint64_t, c_char};
+use libc::{uint8_t, c_char};
extern crate sequoia_openpgp as openpgp;
-use build_hasher;
+/// Holds a KeyID.
+///
+/// A KeyID is a fingerprint fragment. It identifies a public key,
+/// but is easy to forge. For more details about how a KeyID is
+/// generated, see [Section 12.2 of RFC 4880].
+///
+/// [Section 12.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-12.2
+///
+/// Wraps [`sequoia-openpgp::KeyID`].
+///
+/// [`sequoia-openpgp::KeyID`]: ../../sequoia_openpgp/enum.KeyID.html
+#[::ffi_wrapper_type(prefix = "pgp_", name = "keyid",
+ derive = "Clone, Debug, Display, Hash, PartialEq")]
+pub struct KeyID(openpgp::KeyID);
/// Reads a binary key ID.
///
@@ -37,68 +49,25 @@ use build_hasher;
/// pgp_keyid_free (mr_b);
/// free (mr_b_as_string);
/// ```
-#[::ffi_catch_abort] #[no_mangle]
-pub extern "system" fn pgp_keyid_from_bytes(id: *const uint8_t) -> *mut openpgp::KeyID {
+#[::ffi_catch_abort] #[no_mangle] pub extern "system"
+fn pgp_keyid_from_bytes(id: *const uint8_t) -> *mut openpgp::KeyID {
assert!(!id.is_null());
let id = unsafe { slice::from_raw_parts(id, 8) };
Box::into_raw(Box::new(openpgp::KeyID::from_bytes(id)))
}
/// Reads a hex-encoded Key ID.
-#[::ffi_catch_abort] #[no_mangle]
-pub extern "system" fn pgp_keyid_from_hex(id: *const c_char) -> *mut openpgp::KeyID {
+#[::ffi_catch_abort] #[no_mangle] pub extern "system"
+fn pgp_keyid_from_hex(id: *const c_char) -> *mut openpgp::KeyID {
let id = ffi_param_cstr!(id).to_string_lossy();
openpgp::KeyID::from_hex(&id)
.map(|id| Box::into_raw(Box::new(id)))
.unwrap_or(ptr::null_mut())
}
-/// Frees an `KeyID` object.
-#[::ffi_catch_abort] #[no_mangle]
-pub extern "system" fn pgp_keyid_free(keyid: Option<&mut openpgp::KeyID>) {
- ffi_free!(keyid)
-}
-
-/// Clones the KeyID.
-#[::ffi_catch_abort] #[no_mangle]
-pub extern "system" fn pgp_keyid_clone(id: *const openpgp::KeyID)
- -> *mut openpgp::KeyID {
- let id = ffi_param_ref!(id);
- box_raw!(id.clone())
-}
-
-/// Hashes the KeyID.
-#[::ffi_catch_abort] #[no_mangle]
-pub extern "system" fn pgp_keyid_hash(id: *const openpgp::KeyID)
- -> uint64_t {
- let id = ffi_param_ref!(id);
- let mut hasher = build_hasher();
- id.hash(&mut hasher);
- hasher.finish()
-}
-
-/// Converts the KeyID to its standard representation.
-#[::ffi_catch_abort] #[no_mangle]
-pub extern "system" fn pgp_keyid_to_string(id: *const openpgp::KeyID)
- -> *mut c_char {
- let id = ffi_param_ref!(id);
- ffi_return_string!(id.to_string())
-}
-
/// Converts the KeyID to a hexadecimal number.
-#[::ffi_catch_abort] #[no_mangle]
-pub extern "system" fn pgp_keyid_to_hex(id: *const openpgp::KeyID)
- -> *mut c_char {
+#[::ffi_catch_abort] #[no_mangle] pub extern "system"
+fn pgp_keyid_to_hex(id: *const openpgp::KeyID) -> *mut c_char {
let id = ffi_param_ref!(id);
ffi_return_string!(id.to_hex())
}
-
-/// Compares KeyIDs.
-#[::ffi_catch_abort] #[no_mangle]
-pub extern "system" fn pgp_keyid_equal(a: *const openpgp::KeyID,
- b: *const openpgp::KeyID)
- -> bool {
- let a = ffi_param_ref!(a);
- let b = ffi_param_ref!(b);
- a == b
-}