From adb086785942232ebc48b521ded7cee4f6232194 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 5 Jan 2018 10:05:00 +0100 Subject: Handle getting HOTP code properly Signed-off-by: Szczepan Zalega --- python_bindings_example.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python_bindings_example.py b/python_bindings_example.py index 1740a76..7bb806c 100755 --- a/python_bindings_example.py +++ b/python_bindings_example.py @@ -59,7 +59,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 ]) @@ -113,5 +113,6 @@ test_data = [ print('Getting HOTP code from Nitrokey Pro (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 -- cgit v1.2.3 From d3d35a3b60b4b41d905821ea4f38b6fcf14ca086 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 5 Jan 2018 10:06:03 +0100 Subject: Handle library names for all OSes Signed-off-by: Szczepan Zalega --- python_bindings_example.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/python_bindings_example.py b/python_bindings_example.py index 7bb806c..7911846 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) -- cgit v1.2.3 From 5dc71535eaf5810712692d08a5948e74db297ec8 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 5 Jan 2018 10:06:29 +0100 Subject: Allow to choose log verbosity level Signed-off-by: Szczepan Zalega --- python_bindings_example.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/python_bindings_example.py b/python_bindings_example.py index 7911846..6f1e56d 100755 --- a/python_bindings_example.py +++ b/python_bindings_example.py @@ -82,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') -- cgit v1.2.3 From 8cfabd53a7e784ef8c04bbfe20219398bda28ab1 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 5 Jan 2018 10:06:46 +0100 Subject: Fix typo Signed-off-by: Szczepan Zalega --- python_bindings_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_bindings_example.py b/python_bindings_example.py index 6f1e56d..6ddcbc4 100755 --- a/python_bindings_example.py +++ b/python_bindings_example.py @@ -125,7 +125,7 @@ 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) correct_str = "correct!" if hotp_slot_1_code == str(test_data[i])[-8:] else "not correct" -- cgit v1.2.3