Skip to content

Commit

Permalink
All cryptographic functions are now working.
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Batzel <[email protected]>
  • Loading branch information
Romulus10 committed Sep 26, 2018
1 parent 8c487e5 commit 4ad80ae
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
11 changes: 4 additions & 7 deletions blockchain_message/src/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,23 @@ def encrypt(message: str, recipient: Contact) -> bytes:
:param recipient: The individual whose public key to encrypt with.
:return: A bytestring representing the encrypted message.
"""
return rsa.encrypt(message.encode('latin-1'),
rsa.PublicKey.load_pkcs1(recipient.key))
return rsa.encrypt(message.encode('utf8'), rsa.PublicKey.load_pkcs1(recipient.key))

def decrypt(self, message: bytes) -> str:
"""
Carries out decryption on a given bytestring using the current private key.
:param message: The message to decrypt.
:return: A plaintext message decrypted from 'message'.
"""
return rsa.decrypt(message, self.key).decode('latin-1')
return rsa.decrypt(message, self.key).decode('utf8')

def sign(self, message: str) -> str:
"""
Applies a cryptographic signature to the given message.
:param message: The message to sign.
:return: The cryptographic signature for the given payload.
"""
return rsa.sign(message.encode('latin-1'), self.key, 'SHA-1')
return rsa.sign(message.encode('utf8'), self.key, 'SHA-512').hex()

@staticmethod
def verify(message: str, signature: str, sender: Contact):
Expand All @@ -73,9 +72,7 @@ def verify(message: str, signature: str, sender: Contact):
:param signature: The signature to verify.
:param sender: The contact who the message claims to be sent by.
"""
m = bytes(bytes(message, 'latin-1').decode('latin-1'), 'latin-1')
s = bytes(signature, 'latin-1')
rsa.verify(m, s, rsa.PublicKey.load_pkcs1(sender.key))
rsa.verify(message.encode('utf8'), bytes.fromhex(signature), rsa.PublicKey.load_pkcs1(sender.key))

@staticmethod
def generate_key(uname: str):
Expand Down
1 change: 1 addition & 0 deletions test/test_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class TestCrypt(unittest.TestCase):
def test_encrypt(self):
c = Crypt("test")
con = Contact("00000", "test", "[email protected]")
cipher = c.encrypt("This is a test.", con)

def test_decrypt(self):
c = Crypt("test")
Expand Down

0 comments on commit 4ad80ae

Please sign in to comment.