def getSymmetricallyDecryptedVal(val, secretKey: Union[str, bytes]) -> str: if isHex(val): val = bytes(bytearray.fromhex(val)) elif isinstance(val, str): val = val.encode("utf-8") if isHex(secretKey): secretKey = bytes(bytearray.fromhex(secretKey)) elif isinstance(secretKey, str): secretKey = secretKey.encode() box = libnacl.secret.SecretBox(secretKey) return box.decrypt(val).decode()
def verifySig(identifier, signature, msg) -> bool: key = cryptonymToHex(identifier) if not isHex(identifier) else identifier ser = serializeForSig(msg) b64sig = signature.encode('utf-8') sig = b64decode(b64sig) vr = Verifier(key) return vr.verify(sig, ser)
def ed25519SkToCurve25519(sk, toHex=False): if isHex(sk): sk = unhexlify(sk) secretKey = ctypes.create_string_buffer(crypto_box_SECRETKEYBYTES) ret = nacl.crypto_sign_ed25519_sk_to_curve25519(secretKey, sk) if ret: raise Exception("error in converting ed22519 key to curve25519") return hexlify(secretKey.raw) if toHex else secretKey.raw
def ed25519PkToCurve25519(pk, toHex=False): if isHex(pk): pk = unhexlify(pk) publicKey = ctypes.create_string_buffer(crypto_box_PUBLICKEYBYTES) ret = nacl.crypto_sign_ed25519_pk_to_curve25519(publicKey, pk) if ret: raise Exception("error in converting ed22519 key to curve25519") return hexlify(publicKey.raw) if toHex else publicKey.raw
def _isVerified(self, msg: Dict[str, str]): signature = msg.get(f.SIG.nm) identifier = msg.get(IDENTIFIER) msgWithoutSig = {} for k, v in msg.items(): if k != f.SIG.nm: msgWithoutSig[k] = v key = cryptonymToHex( identifier) if not isHex(identifier) else identifier isVerified = verifySig(key, signature, msgWithoutSig) if not isVerified: self.notifyObservers("Signature rejected") return isVerified
def getSymmetricallyEncryptedVal(val, secretKey: Union[str, bytes]=None) -> \ Tuple[str, str]: """ Encrypt the provided value with symmetric encryption :param val: the value to encrypt :param secretKey: Optional key, if provided should be either in hex or bytes :return: Tuple of the encrypted value and secret key encoded in hex """ if isinstance(val, str): val = val.encode("utf-8") if secretKey: if isHex(secretKey): secretKey = bytes(bytearray.fromhex(secretKey)) elif not isinstance(secretKey, bytes): error("Secret key must be either in hex or bytes") box = libnacl.secret.SecretBox(secretKey) else: box = libnacl.secret.SecretBox() return box.encrypt(val).hex(), box.sk.hex()