Forráskód Böngészése

hashing with no file limit

AdamHorvath12 4 éve
szülő
commit
05f13ecc0b
2 módosított fájl, 32 hozzáadás és 11 törlés
  1. 14 1
      create_keys.py
  2. 18 10
      lib/transaction.py

+ 14 - 1
create_keys.py

@@ -14,6 +14,7 @@ import os
 from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.primitives.asymmetric import rsa
 from cryptography.hazmat.primitives import serialization
+from cryptography.fernet import Fernet
 
 def make_keys(contract_name):
     #(pubkey,privkey)=rsa.newkeys(2048)
@@ -43,7 +44,13 @@ def make_keys(contract_name):
     )
 
     with open(path + 'privatekey.key','wb') as f:
-        f.write(pem)    
+        f.write(pem)
+    
+    
+    symmetric_key = Fernet.generate_key()
+
+    with open(path + 'symmetric.key','wb') as f:
+        f.write(symmetric_key)
     
 def get_keys(contract_name):
     path = 'contract_keys\\' + contract_name  + '\\publickey.key'
@@ -55,6 +62,12 @@ def get_keys(contract_name):
             )
         return public_key
 
+def get_symmetric_key(contract_name):
+    path = 'contract_keys\\' + contract_name  + '\\symmetric.key'
+
+    with open(path, 'rb') as f:
+        return f.read()
+
 def get_private_key(contract_name):
     path = 'contract_keys\\' + contract_name  + '\\privatekey.key'
 

+ 18 - 10
lib/transaction.py

@@ -9,6 +9,8 @@ from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.primitives import serialization
 from cryptography.hazmat.primitives import hashes
 from cryptography.hazmat.primitives.asymmetric import padding
+from cryptography.fernet import Fernet
+
 
 class Transaction:
     def __init__(self):
@@ -73,16 +75,22 @@ class Transaction:
         block_string = string_object.encode()
 
         # open the public key file
-        pubkey = get_keys(contract) #Sender public key - input field neccessary
-
-        encrypted_data = pubkey.encrypt(
-            block_string,
-            padding.OAEP(
-                mgf=padding.MGF1(algorithm=hashes.SHA256()),
-                algorithm=hashes.SHA256(),
-                label=None
-            )
-        )
+        symmetric_key = get_symmetric_key(contract)
+        pubkey = get_keys(contract)
+
+        chiper = Fernet(symmetric_key)
+
+        encrypted_data = chiper.encrypt(block_string)
+
+        encrypted_key = rsa.encrypt(symmetric_key,pubkey) # we need an extra field in the transaction
+        # encrypted_data = pubkey.encrypt(
+        #     block_string,
+        #     padding.OAEP(
+        #         mgf=padding.MGF1(algorithm=hashes.SHA256()),
+        #         algorithm=hashes.SHA256(),
+        #         label=None
+        #     )
+        # )
 
         self.hash_value = encrypted_data
         self.log(f'Hashed: {self.hash_value}')