aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2018-01-05 10:07:42 +0100
committerSzczepan Zalega <szczepan@nitrokey.com>2018-01-05 10:07:42 +0100
commitcd310d836391b1895f6eb10ebef60d797a2efe15 (patch)
treec090d723c005649de01045e51a076242e590c1ef
parentd917909036585eabeaa2e2727fca1d3d8cf9d086 (diff)
parent8cfabd53a7e784ef8c04bbfe20219398bda28ab1 (diff)
downloadlibnitrokey-cd310d836391b1895f6eb10ebef60d797a2efe15.tar.gz
libnitrokey-cd310d836391b1895f6eb10ebef60d797a2efe15.tar.bz2
Merge branch '82-python_example_fix'
Handle getting HOTP code properly - code is now string, not an int Additional improvements Fixes #82
-rwxr-xr-xpython_bindings_example.py34
1 files changed, 25 insertions, 9 deletions
diff --git a/python_bindings_example.py b/python_bindings_example.py
index 1740a76..6ddcbc4 100755
--- a/python_bindings_example.py
+++ b/python_bindings_example.py
@@ -41,16 +41,23 @@ def get_library():
C = None
import os, sys
path_build = os.path.join(".", "build")
- paths = [ os.path.join(path_build,"libnitrokey-log.so"),
- os.path.join(path_build,"libnitrokey.so")]
+ paths = [
+ os.environ.get('LIBNK_PATH', None),
+ os.path.join(path_build,"libnitrokey.so"),
+ os.path.join(path_build,"libnitrokey.dylib"),
+ os.path.join(path_build,"libnitrokey.dll"),
+ os.path.join(path_build,"nitrokey.dll"),
+ ]
for p in paths:
- print p
+ if not p: continue
+ print("Trying " +p)
+ p = os.path.abspath(p)
if os.path.exists(p):
+ print("Found: "+p)
C = ffi.dlopen(p)
break
else:
print("File does not exist: " + p)
- print("Trying another")
if not C:
print("No library file found")
sys.exit(1)
@@ -59,7 +66,7 @@ def get_library():
def get_hotp_code(lib, i):
- return lib.NK_get_hotp_code(i)
+ return get_string(lib.NK_get_hotp_code(i))
def to_hex(ss):
return ''.join([ format(ord(s),'02x') for s in ss ])
@@ -75,9 +82,17 @@ if not a == 'continue':
ADMIN = raw_input('Please enter your admin PIN (empty string uses 12345678) ')
ADMIN = ADMIN or '12345678' # use default if empty string
-show_log = raw_input('Should log messages be shown (please write "yes" to enable)? ') == 'yes'
+show_log = raw_input('Should log messages be shown (please write "yes" to enable; this will make harder reading script output) ') == 'yes'
libnitrokey = get_library()
-libnitrokey.NK_set_debug(show_log) # do not show debug messages
+
+if show_log:
+ log_level = raw_input('Please select verbosity level (0-5, 2 is library default, 3 will be selected on empty input) ')
+ log_level = log_level or '3'
+ log_level = int(log_level)
+ libnitrokey.NK_set_debug_level(log_level)
+else:
+ libnitrokey.NK_set_debug_level(2)
+
ADMIN_TEMP = '123123123'
RFC_SECRET = to_hex('12345678901234567890')
@@ -110,8 +125,9 @@ test_data = [
1284755224, 1094287082, 137359152, 1726969429, 1640338314, 868254676, 1918287922, 82162583, 673399871,
645520489,
]
-print('Getting HOTP code from Nitrokey Pro (RFC test, 8 digits): ')
+print('Getting HOTP code from Nitrokey Stick (RFC test, 8 digits): ')
for i in range(10):
hotp_slot_1_code = get_hotp_code(libnitrokey, 1)
- print('%d: %d, should be %s' % (i, hotp_slot_1_code, str(test_data[i])[-8:] ))
+ correct_str = "correct!" if hotp_slot_1_code == str(test_data[i])[-8:] else "not correct"
+ print('%d: %s, should be %s -> %s' % (i, hotp_slot_1_code, str(test_data[i])[-8:], correct_str))
libnitrokey.NK_logout() # disconnect device