aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2016-08-01 22:24:22 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2016-08-01 22:28:54 +0200
commit157226ac40b78e48836163ff5bdad0d850049ad0 (patch)
treea94d7c6489df13d2cc4477e5b303c55de8dbb986
parent010d25fceb91360ac3d8bd0bd61604649f7994ac (diff)
downloadlibnitrokey-157226ac40b78e48836163ff5bdad0d850049ad0.tar.gz
libnitrokey-157226ac40b78e48836163ff5bdad0d850049ad0.tar.bz2
Add Python interfacing example
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rwxr-xr-xpython_bindings_example.py42
1 files changed, 42 insertions, 0 deletions
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