mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-10 06:48:19 +02:00
Encrypt, decrypt and padding in model methods
This commit is contained in:
parent
72582abb2d
commit
11a05a8e09
1 changed files with 28 additions and 0 deletions
|
|
@ -17,6 +17,7 @@ import os.path
|
|||
import sys
|
||||
import uuid
|
||||
from tempfile import NamedTemporaryFile
|
||||
from Crypto.Cipher import AES
|
||||
|
||||
import six
|
||||
from six.moves.urllib.parse import parse_qs, unquote, urlparse
|
||||
|
|
@ -113,6 +114,33 @@ class Mailbox(models.Model):
|
|||
objects = models.Manager()
|
||||
active_mailboxes = ActiveMailboxManager()
|
||||
|
||||
def pad(self, key):
|
||||
length = 32 - (len(key) % 32)
|
||||
return key + chr(length).encode('utf-8') * length
|
||||
|
||||
def unpad(self, key):
|
||||
return key[0:-ord(key[-1])]
|
||||
|
||||
def encrypt_uri(self):
|
||||
secret_key = self.pad(django_settings.SECRET_KEY)
|
||||
self.uri = unicode(self.pad(self.uri)).encode('utf-8')
|
||||
|
||||
cipher = AES.new(secret_key)
|
||||
self.uri = cipher.encrypt(self.uri)
|
||||
self.uri = base64.b64encode(self.uri)
|
||||
|
||||
return self.uri
|
||||
|
||||
def decrypt_uri(self):
|
||||
secret_key = self.pad(django_settings.SECRET_KEY)
|
||||
self.uri = self.pad(self.uri)
|
||||
|
||||
cipher = AES.new(secret_key)
|
||||
self.uri = base64.b64decode(self.uri)
|
||||
self.uri = cipher.decrypt(self.uri)
|
||||
|
||||
return self.unpad(self.uri)
|
||||
|
||||
@property
|
||||
def _protocol_info(self):
|
||||
return urlparse(self.uri)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue