From 157226ac40b78e48836163ff5bdad0d850049ad0 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 1 Aug 2016 22:24:22 +0200 Subject: Add Python interfacing example Signed-off-by: Szczepan Zalega --- python_bindings_example.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 python_bindings_example.py diff --git a/python_bindings_example.py b/python_bindings_example.py new file mode 100755 index 0000000..a690874 --- /dev/null +++ b/python_bindings_example.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +import cffi + +ffi = cffi.FFI() +get_string = ffi.string + + +def get_library(): + fp = 'NK_C_API.h' # path to C API header + + declarations = [] + with open(fp, 'r') as f: + declarations = f.readlines() + + for declaration in declarations: + if 'extern' in declaration and not '"C"' in declaration: + declaration = declaration.replace('extern', '').strip() + # print(declaration) + ffi.cdef(declaration) + + C = ffi.dlopen("build/libnitrokey.so") # path to built library + return C + + +def get_hotp_code(lib, i): + return lib.NK_get_hotp_code(i) + + +libnitrokey = get_library() +libnitrokey.NK_set_debug(False) # do not show debug messages + +libnitrokey.NK_login('P') # connect to Nitrokey Pro device +hotp_slot_1_code = get_hotp_code(libnitrokey, 1) +print('Getting HOTP code from Nitrokey Pro: ') +print(hotp_slot_1_code) +libnitrokey.NK_logout() # disconnect device + +libnitrokey.NK_login('S') # connect to Nitrokey Storage device +hotp_slot_1_code_nitrokey_storage = get_hotp_code(libnitrokey, 1) +print('Getting HOTP code from Nitrokey Storage: ') +print(hotp_slot_1_code_nitrokey_storage) +libnitrokey.NK_logout() # disconnect device -- cgit v1.2.3