aboutsummaryrefslogtreecommitdiff
path: root/python_bindings_example.py
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2016-08-16 19:37:00 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2016-08-16 19:37:00 +0200
commit7dbbe7fb4c50c62b0e1818c5356aafb97a00ac0f (patch)
treeedc299a04db9ce49e2629369dfce95fb596f9d9a /python_bindings_example.py
parent709b3e4c3a7aac2565687579bff62601b141362f (diff)
downloadlibnitrokey-7dbbe7fb4c50c62b0e1818c5356aafb97a00ac0f.tar.gz
libnitrokey-7dbbe7fb4c50c62b0e1818c5356aafb97a00ac0f.tar.bz2
Python example update #5
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
Diffstat (limited to 'python_bindings_example.py')
-rwxr-xr-xpython_bindings_example.py66
1 files changed, 57 insertions, 9 deletions
diff --git a/python_bindings_example.py b/python_bindings_example.py
index 31cf285..6bad8f4 100755
--- a/python_bindings_example.py
+++ b/python_bindings_example.py
@@ -1,9 +1,22 @@
#!/usr/bin/env python
import cffi
+from enum import Enum
+
+"""
+This example will print 10 HOTP codes from just written HOTP#2 slot.
+For more examples of use please refer to unittest/test_bindings.py file.
+"""
ffi = cffi.FFI()
get_string = ffi.string
+class DeviceErrorCode(Enum):
+ STATUS_OK = 0
+ NOT_PROGRAMMED = 3
+ WRONG_PASSWORD = 4
+ STATUS_NOT_AUTHORIZED = 5
+ STATUS_AES_DEC_FAILED = 0xa
+
def get_library():
fp = 'NK_C_API.h' # path to C API header
@@ -28,18 +41,53 @@ def get_library():
def get_hotp_code(lib, i):
return lib.NK_get_hotp_code(i)
+print('Warning!')
+print('This example will change your configuration on inserted stick and overwrite your HOTP#2 slot.')
+print('Please write "continue" to continue or any other string to quit')
+a = raw_input()
+
+if not a == 'continue':
+ exit()
+
+ADMIN = raw_input('Please enter your admin PIN (empty string uses 12345678)')
+ADMIN = ADMIN or '12345678' # use default if empty string
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
+ADMIN_TEMP = '123123123'
+RFC_SECRET = '12345678901234567890'
+
+# libnitrokey.NK_login('S') # connect only to Nitrokey Storage device
+# libnitrokey.NK_login('P') # connect only to Nitrokey Pro device
+device_connected = libnitrokey.NK_login_auto() # connect to any Nitrokey Stick
+if device_connected:
+ print('Connected to Nitrokey device!')
+else:
+ print('Could not connect to Nitrokey device!')
+ exit()
+use_8_digits = True
+pin_correct = libnitrokey.NK_first_authenticate(ADMIN, ADMIN_TEMP) == DeviceErrorCode.STATUS_OK
+if pin_correct:
+ print('Your PIN is correct!')
+else:
+ print('Your PIN is not correct! Please try again. Please be careful to not lock your stick!')
+ retry_count_left = libnitrokey.NK_get_admin_retry_count()
+ print('Retry count left: %d' % retry_count_left )
+ exit()
-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)
+# For function parameters documentation please check NK_C_API.h
+assert libnitrokey.NK_write_config(255, 255, 255, False, True, ADMIN_TEMP) == DeviceErrorCode.STATUS_OK
+libnitrokey.NK_first_authenticate(ADMIN, ADMIN_TEMP)
+libnitrokey.NK_write_hotp_slot(1, 'python_test', RFC_SECRET, 0, use_8_digits, False, False, "",
+ ADMIN_TEMP)
+# RFC test according to: https://tools.ietf.org/html/rfc4226#page-32
+test_data = [
+ 1284755224, 1094287082, 137359152, 1726969429, 1640338314, 868254676, 1918287922, 82162583, 673399871,
+ 645520489,
+]
+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:] ))
libnitrokey.NK_logout() # disconnect device