def initCard(): log("Request to init new card") card = None while (card == None or card == 0): card = pn532.read() if (pn532.format() != 1): log("Failed to format card") return None newhash = hashlib.sha256("%s %s %s" % (card[0],config.hashSecret,str(time.time()))).digest() status = pn532.write(newhash) if (status != 0): log("Write failed: %s",(status)) return None retries = 0 while(True): card2 = pn532.read() if ((card2[0] == card[0]) & (card2[1]==newhash)): break retries = retries + 1 if (retries > 5): break if ((card2[0] != card[0]) | (card2[1]!=newhash)): log("Verify failed") return None log ("New card written serial=%s : hash=%s" % (card[0],binascii.hexlify(newhash))) return(card[0],binascii.hexlify(newhash))
def writeHash(hash): status = pn532.write(hash) if (status==0): log(" -> Write successful") else: log(" -> Write failed: err no "+status) return False card = pn532.read() if (card == None or card == 0): log(" -> Failed to read after write") return False if (card[1] != hash): log(" -> Failed to verify hash") return False # If all that didn't fail, we must have succeeded! return True
#!/usr/bin/python import pn532 import hashlib import binascii pn532.init() #print "Starting Value" #print pn532.read() print "Waiting for card..." card = None while (card == None or card == 0): card = pn532.read() print "Found Serial #", card[0] print " hash -> ", binascii.hexlify(card[1]) newhash = hashlib.sha256(card[1]).digest() print "New hash = ", binascii.hexlify(newhash) print "Writing card" #newhash = "12345678901234567890123456789012" status = pn532.write(newhash) if (status == 0): print "Write successful" else: print "Write failed: err no", status print "Final Hash Value" print binascii.hexlify(pn532.read()[1]) pn532.close()
import binascii import time pn532.init() card = None while (card == None or card == 0): card = pn532.read() if (pn532.format() != 1): print "Failed to format" exit() newhash = hashlib.sha256(card[0]+"newSecretValue"+time.time()).digest() #print "New hash = ",binascii.hexlify(newhash) #print "Writing card" #newhash = "12345678901234567890123456789012" status = pn532.write(newhash) if (status != 0): print "Write failed:\n",status card2 = pn532.read() if (card2 == card): #Store in database print "Success" #print "Starting Value" #print pn532.read() #print "Waiting for card..." #card = None #while (card == None or card == 0): # card = pn532.read() #print "Found Serial #",card[0] #print " hash -> ",binascii.hexlify(card[1])