biometry_hash_1.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. def enroll():
  2. ## Enrolls new finger
  3. ##
  4. ## Tries to initialize the sensor
  5. try:
  6. f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000)
  7. if ( f.verifyPassword() == False ):
  8. raise ValueError('The given fingerprint sensor password is wrong!')
  9. except Exception as e:
  10. print('The fingerprint sensor could not be initialized!')
  11. print('Exception message: ' + str(e))
  12. exit(1)
  13. ## Gets some sensor information
  14. print('Currently stored templates: ' + str(f.getTemplateCount()))
  15. ## Tries to enroll new finger
  16. try:
  17. print('Waiting for finger...')
  18. ## Wait that finger is read
  19. while ( f.readImage() == False ):
  20. pass
  21. ## Converts read image to characteristics and stores it in charbuffer 1
  22. f.convertImage(0x01)
  23. ## Checks if finger is already enrolled
  24. result = f.searchTemplate()
  25. positionNumber = result[0]
  26. if ( positionNumber >= 0 ):
  27. f.loadTemplate(positionNumber, 0x01)
  28. characterics = str(f.downloadCharacteristics(0x01))
  29. passhashes = hashlib.sha256(characterics).hexdigest()
  30. passhash = passhashes[0:32]
  31. print('Template already exists at position #' + str(positionNumber))
  32. return passhash
  33. time.sleep(2)
  34. ## Wait that finger is read again
  35. while ( f.readImage() == False ):
  36. pass
  37. ## Converts read image to characteristics and stores it in charbuffer 2
  38. f.convertImage(0x02)
  39. ## Compares the charbuffers and creates a template
  40. f.createTemplate()
  41. ## Gets new position number (the counting starts at 0, so we do not need to increment)
  42. positionNumber = f.getTemplateCount()
  43. ## Saves template at new position number
  44. if ( f.storeTemplate(positionNumber) == True ):
  45. ## Hashes characteristics of template
  46. characterics = str(f.downloadCharacteristics(0x01))
  47. passhashes = hashlib.sha256(characterics).hexdigest()
  48. passhash = passhashes[0:32]
  49. ## Hashes characteristics of template
  50. print('SHA-2 hash of template: ' + passhash)
  51. return passhash
  52. except Exception as e:
  53. exit(1)
  54. #print('SHA-2 hash of template: ' + hashlib.sha256(characterics).hexdigest())