biometry_hash_1.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. ## Gets new position number (the counting starts at 0, so we do not need to increment)
  27. #positionNumber = f.getTemplateCount()
  28. if ( positionNumber >= 0 ):
  29. f.loadTemplate(positionNumber, 0x01)
  30. characterics = str(f.downloadCharacteristics(0x01))
  31. passhashes = hashlib.sha256(characterics).hexdigest()
  32. passhash = passhashes[0:32]
  33. print('Template already exists at position #' + str(positionNumber))
  34. return passhash
  35. print('Remove finger...')
  36. time.sleep(2)
  37. print('Waiting for same finger again...')
  38. ## Wait that finger is read again
  39. while ( f.readImage() == False ):
  40. pass
  41. ## Converts read image to characteristics and stores it in charbuffer 2
  42. f.convertImage(0x02)
  43. ## Compares the charbuffers and creates a template
  44. f.createTemplate()
  45. ## Gets new position number (the counting starts at 0, so we do not need to increment)
  46. positionNumber = f.getTemplateCount()
  47. ## Saves template at new position number
  48. if ( f.storeTemplate(positionNumber) == True ):
  49. print('Finger enrolled successfully!')
  50. print('New template position #' + str(positionNumber))
  51. ## Hashes characteristics of template
  52. characterics = str(f.downloadCharacteristics(0x01))
  53. passhashes = hashlib.sha256(characterics).hexdigest()
  54. passhash = passhashes[0:32]
  55. ## Hashes characteristics of template
  56. print('SHA-2 hash of template: ' + passhash)
  57. return passhash
  58. except Exception as e:
  59. print('Operation failed!')
  60. print('Exception message: ' + str(e))
  61. exit(1)
  62. #print('SHA-2 hash of template: ' + hashlib.sha256(characterics).hexdigest())