def createPayload(self, publicKeyNuki, privateKeyAuth, publicKeyAuth, nonceNuki, appID, idType, name): self.appID = ("%x" % appID).rjust(8,'0') self.idType = idType self.name = name.encode().hex().ljust(64, '0') self.nonce = nacl.utils.random(32).hex() sharedKey = crypto_box_beforenm(bytes(bytearray.fromhex(publicKeyNuki)),bytes(bytearray.fromhex(privateKeyAuth))).hex() valueR = self.idType + self.appID + self.name + self.nonce + nonceNuki self.authenticator = hmac.new(bytearray.fromhex(sharedKey), msg=bytearray.fromhex(valueR), digestmod=hashlib.sha256).hexdigest() self.payload = self.authenticator + self.idType + self.appID + self.name + self.nonce
def createPayload(self, publicKeyNuki, privateKeyAuth, nonceNuki, authID): self.authID = ("%x" % authID).rjust(8, '0') sharedKey = crypto_box_beforenm( bytes(bytearray.fromhex(publicKeyNuki)), bytes(bytearray.fromhex(privateKeyAuth))).hex() valueR = self.authID + nonceNuki self.authenticator = hmac.new(bytearray.fromhex(sharedKey), msg=bytearray.fromhex(valueR), digestmod=hashlib.sha256).hexdigest() self.payload = self.authenticator + self.authID
def createPayload(self, nonceNuki, privateKeyAuth, publicKeyAuth, publicKeyNuki): sharedKey = crypto_box_beforenm( bytes(bytearray.fromhex(publicKeyNuki)), bytes(bytearray.fromhex(privateKeyAuth))).hex() valueR = publicKeyAuth.hex() + publicKeyNuki + nonceNuki self.authenticator = hmac.new(bytearray.fromhex(sharedKey), msg=bytearray.fromhex(valueR), digestmod=hashlib.sha256).hexdigest() self.payload = self.authenticator
def generate(self, format='BYTE_ARRAY'): unencrypted = self.authID + self.command.generate(format='HEX')[:-4] crc = self.byteSwapper.swap(self.crcCalculator.crc_ccitt(unencrypted)) unencrypted = unencrypted + crc sharedKey = crypto_box_beforenm(bytes(bytearray.fromhex(self.publicKey)),bytes(bytearray.fromhex(self.privateKey))).hex() box = nacl.secret.SecretBox(bytes(bytearray.fromhex(sharedKey))) encrypted = box.encrypt(bytes(bytearray.fromhex(unencrypted)), bytes(bytearray.fromhex(self.nonce))).hex()[48:] length = self.byteSwapper.swap("%04X" % int((len(encrypted)/2))) msg = self.nonce + self.authID + length + encrypted if format == 'BYTE_ARRAY': return bytearray.fromhex(msg) else: return msg
def createPayload(self, nonceNuki, privateKeyAuth, publicKeyAuth, publicKeyNuki): sharedKey = binascii.hexlify( crypto_box_beforenm( bytes(bytearray.fromhex(publicKeyNuki)), bytes(bytearray.fromhex( privateKeyAuth.decode('utf-8'))))).decode('utf-8') valueR = to_str(publicKeyAuth) + to_str(publicKeyNuki) + to_str( nonceNuki) self.authenticator = hmac.new(bytearray.fromhex(sharedKey), msg=bytearray.fromhex(valueR), digestmod=hashlib.sha256).hexdigest() self.payload = self.authenticator
def decrypt(self, msg, publicKey, privateKey): print("msg: %s" % msg) nonce = msg[:48] #print "nonce: %s" % nonce authID = msg[48:56] #print "authID: %s" % authID length = int(self.byteSwapper.swap(msg[56:60]), 16) #print "length: %d" % length encrypted = nonce + msg[60:60+(length*2)] #print "encrypted: %s" % encrypted sharedKey = crypto_box_beforenm(bytes(bytearray.fromhex(publicKey)),bytes(bytearray.fromhex(privateKey))).hex() box = nacl.secret.SecretBox(bytes(bytearray.fromhex(sharedKey))) decrypted = box.decrypt(bytes(bytearray.fromhex(encrypted))).hex() #print "decrypted: %s" % decrypted return decrypted
def decrypt(self, msg, publicKey, privateKey): # print("msg: %s" % msg) nonce = msg[:48] # print("nonce: %s" % nonce) authID = msg[48:56] # print("authID: %s" % authID) length = int(self.byteSwapper.swap(msg[56:60]), 16) # print("length: %d" % length) encrypted = nonce + msg[60:60 + (length * 2)] # print("encrypted: %s" % encrypted) sharedKey = binascii.hexlify( crypto_box_beforenm(binascii.unhexlify(publicKey), binascii.unhexlify(privateKey))) box = nacl.secret.SecretBox(binascii.unhexlify(sharedKey)) decrypted = binascii.hexlify(box.decrypt( binascii.unhexlify(encrypted))) print("decrypted: %s" % decrypted) return decrypted
def createPayload(self, publicKeyNuki, privateKeyAuth, publicKeyAuth, nonceNuki, appID, idType, name): self.appID = ("%x" % appID).rjust(8, '0') self.idType = idType self.name = binascii.hexlify(name.encode()).ljust(64, b'0') self.nonce = binascii.hexlify(nacl.utils.random(32)) sharedKey = binascii.hexlify( crypto_box_beforenm( bytes(bytearray.fromhex(to_str(publicKeyNuki))), bytes(bytearray.fromhex(to_str(privateKeyAuth))))) valueR = to_str(self.idType) + to_str(self.appID) + to_str( self.name) + to_str(self.nonce) + to_str(nonceNuki) self.authenticator = hmac.new(bytearray.fromhex(to_str(sharedKey)), msg=bytearray.fromhex(valueR), digestmod=hashlib.sha256).hexdigest() self.payload = to_str(self.authenticator) + to_str( self.idType) + to_str(self.appID) + to_str(self.name) + to_str( self.nonce)
def generate(self, format='BYTE_ARRAY'): unencrypted = self.authID + self.command.generate(format='HEX')[:-4] crc = self.byteSwapper.swap(self.crcCalculator.crc_ccitt(unencrypted)) unencrypted = unencrypted + crc sharedKey = binascii.hexlify( crypto_box_beforenm(binascii.unhexlify(self.publicKey), binascii.unhexlify(self.privateKey))) box = nacl.secret.SecretBox(binascii.unhexlify(sharedKey)) encrypted = binascii.hexlify( box.encrypt(binascii.unhexlify(unencrypted), binascii.unhexlify(self.nonce)))[48:] length = self.byteSwapper.swap("%04X" % (len(encrypted) / 2)) msg = to_str(self.nonce) + to_str( self.authID) + to_str(length) + to_str(encrypted) if format == 'BYTE_ARRAY': return array.array('B', binascii.unhexlify(msg)) else: return msg