]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
keys: Convert the key ID to integer in Python
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 1 Jun 2023 18:11:26 +0000 (18:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 1 Jun 2023 18:11:26 +0000 (18:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/key.c
tests/python/keys.py

index 499cff29f40131e8373145d1c452e5bf857db222..8c23381c38de810c139aeeb7682ae074bfbc3a47 100644 (file)
@@ -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) {
index 271bffd04e281fd170e852116a82b9528f2a6d36..1b2abeed1ff0fe042657a4a1b934630d9bf23898 100755 (executable)
@@ -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()