Moved Destination Fernet to internal implementation

This commit is contained in:
Mark Qvist 2022-06-08 12:37:24 +02:00
parent 4bd5f05e0e
commit 76f86f782a

View File

@ -20,13 +20,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
import base64
import math import math
import time import time
import RNS import RNS
from cryptography.fernet import Fernet from RNS.Cryptography import Fernet
from cryptography.hazmat.backends import default_backend
class Callbacks: class Callbacks:
def __init__(self): def __init__(self):
@ -311,8 +309,8 @@ class Destination:
raise TypeError("A single destination holds keys through an Identity instance") raise TypeError("A single destination holds keys through an Identity instance")
if self.type == Destination.GROUP: if self.type == Destination.GROUP:
self.prv_bytes = base64.urlsafe_b64decode(Fernet.generate_key()) self.prv_bytes = Fernet.generate_key()
self.prv = Fernet(base64.urlsafe_b64encode(self.prv_bytes)) self.prv = Fernet(self.prv_bytes)
def get_private_key(self): def get_private_key(self):
@ -344,7 +342,7 @@ class Destination:
if self.type == Destination.GROUP: if self.type == Destination.GROUP:
self.prv_bytes = key self.prv_bytes = key
self.prv = Fernet(base64.urlsafe_b64encode(self.prv_bytes)) self.prv = Fernet(self.prv_bytes)
def load_public_key(self, key): def load_public_key(self, key):
if self.type != Destination.SINGLE: if self.type != Destination.SINGLE:
@ -369,7 +367,7 @@ class Destination:
if self.type == Destination.GROUP: if self.type == Destination.GROUP:
if hasattr(self, "prv") and self.prv != None: if hasattr(self, "prv") and self.prv != None:
try: try:
return base64.urlsafe_b64decode(self.prv.encrypt(plaintext)) return self.prv.encrypt(plaintext)
except Exception as e: except Exception as e:
RNS.log("The GROUP destination could not encrypt data", RNS.LOG_ERROR) RNS.log("The GROUP destination could not encrypt data", RNS.LOG_ERROR)
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
@ -394,7 +392,7 @@ class Destination:
if self.type == Destination.GROUP: if self.type == Destination.GROUP:
if hasattr(self, "prv") and self.prv != None: if hasattr(self, "prv") and self.prv != None:
try: try:
return self.prv.decrypt(base64.urlsafe_b64encode(ciphertext)) return self.prv.decrypt(ciphertext)
except Exception as e: except Exception as e:
RNS.log("The GROUP destination could not decrypt data", RNS.LOG_ERROR) RNS.log("The GROUP destination could not decrypt data", RNS.LOG_ERROR)
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)