From: Michael Tremer Date: Thu, 1 Jun 2023 18:11:26 +0000 (+0000) Subject: keys: Convert the key ID to integer in Python X-Git-Tag: 0.9.29~129 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5d30390be100677e80d93602f517e444da6903fe;p=people%2Fms%2Fpakfire.git keys: Convert the key ID to integer in Python Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/key.c b/src/_pakfire/key.c index 499cff29f..8c23381c3 100644 --- a/src/_pakfire/key.c +++ b/src/_pakfire/key.c @@ -52,10 +52,21 @@ static void Key_dealloc(KeyObject* self) { Py_TYPE(self)->tp_free((PyObject *)self); } +static uint64_t Key_id_to_int(const pakfire_key_id* id) { + uint64_t i = 0; + + for (unsigned int j = 0; j < sizeof(*id); j++) { + // Shift i by one byte & append the next byte + i = (i << 8) | (*id)[j]; + } + + return i; +} + static PyObject* Key_repr(KeyObject* self) { const pakfire_key_id* id = pakfire_key_get_id(self->key); - return PyUnicode_FromFormat("<_pakfire.Key (%lu)>", *id); + return PyUnicode_FromFormat("<_pakfire.Key (%lu)>", Key_id_to_int(id)); } static PyObject* Key_str(KeyObject* self) { @@ -70,7 +81,9 @@ static PyObject* Key_str(KeyObject* self) { static PyObject* Key_get_id(KeyObject* self) { const pakfire_key_id* id = pakfire_key_get_id(self->key); - return PyLong_FromUnsignedLong(*id); + uint64_t i = Key_id_to_int(id); + + return PyLong_FromUnsignedLong(i); } static PyObject* Key_get_algorithm(KeyObject* self) { diff --git a/tests/python/keys.py b/tests/python/keys.py index 271bffd04..1b2abeed1 100755 --- a/tests/python/keys.py +++ b/tests/python/keys.py @@ -57,13 +57,20 @@ class KeysTests(unittest.TestCase): with open(path, "rb") as f: return self.pakfire.import_key(f) - def test_import(self): + def test_import_public_key(self): # Import a public key key = self._import("keys/key1.pub") + # Check for the correct key ID + self.assertEqual(key.id, 13863674484496905947) + + def test_import_secret_key(self): # Import a secret key key = self._import("keys/key1.sec") + # Check for the correct key ID + self.assertEqual(key.id, 13863674484496905947) + if __name__ == "__main__": unittest.main()