Skip to content

Commit

Permalink
Python example update #5
Browse files Browse the repository at this point in the history
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
  • Loading branch information
szszszsz committed Aug 16, 2016
1 parent 709b3e4 commit 7dbbe7f
Showing 1 changed file with 57 additions and 9 deletions.
66 changes: 57 additions & 9 deletions python_bindings_example.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit 7dbbe7f

Please sign in to comment.