Beispiel #1
0
    def sign(message: object, private_key: str) -> str:
        """
        signs a message

        @param message: (object) message to sign
        @param private_key: (str) private key to sign with
        """
        private_key_bytes = serialization.load_pem_private_key(
            private_key.encode('utf-8'),
            password=None,
            backend=default_backend()
        )
        message_bytes = Bytes.object_to_bytes(message)
        signature_bytes = private_key_bytes.sign(
            message_bytes,
            ec.ECDSA(hashes.SHA256())
        )
        return Bytes.encode_bytes(signature_bytes).decode('utf-8')
Beispiel #2
0
    def verify(message: object, signature: str, public_key: str) -> bool:
        """
        verfies message with digital signature

        @param message: (object) message to verify
        @param signature: (str) signature to verify message with
        @param public_key: (str) public key to verify message with
        """
        try:
            public_key_bytes = serialization.load_pem_public_key(
                public_key.encode('utf-8'),
                backend=default_backend()
            )
            signature_bytes = Bytes.decode_bytes(signature.encode('utf-8'))
            message_bytes = Bytes.object_to_bytes(message)
            public_key_bytes.verify(
                signature_bytes,
                message_bytes,
                ec.ECDSA(hashes.SHA256())
            )
            return True
        except InvalidSignature:
            return False