Switch to using pycryptodome instead of pycrypto
authorMagnus Hagander <magnus@hagander.net>
Fri, 1 Nov 2019 13:52:35 +0000 (14:52 +0100)
committerMagnus Hagander <magnus@hagander.net>
Fri, 1 Nov 2019 13:52:35 +0000 (14:52 +0100)
pycrypto is not being maintained, and pycryptodome is theoretically
a drop-in replacement (in practice, it seems it was close)

pgweb/account/views.py
pgweb/util/misc.py
requirements.txt
tools/communityauth/generate_cryptkey.py
tools/communityauth/sample/django/auth.py
tools/communityauth/test_auth.py

index a5a4c4ef0e6c5f4b73845996f3760bbb15506932..f90e532912797e680e2c4a845ecd2d7af02b820f 100644 (file)
@@ -14,8 +14,8 @@ from django.db.models import Q
 
 import base64
 import urllib.parse
-from Crypto.Cipher import AES
-from Crypto import Random
+from Cryptodome.Cipher import AES
+from Cryptodome import Random
 import time
 import json
 from datetime import datetime, timedelta
@@ -547,7 +547,7 @@ def communityauth(request, siteid):
     r = Random.new()
     iv = r.read(16)  # Always 16 bytes for AES
     encryptor = AES.new(base64.b64decode(site.cryptkey), AES.MODE_CBC, iv)
-    cipher = encryptor.encrypt(s + ' ' * (16 - (len(s) % 16)))  # Pad to even 16 bytes
+    cipher = encryptor.encrypt(s.encode('ascii') + b' ' * (16 - (len(s) % 16)))  # Pad to even 16 bytes
 
     # Generate redirect
     return HttpResponseRedirect("%s?i=%s&d=%s" % (
@@ -594,7 +594,7 @@ def _encrypt_site_response(site, s):
     r = Random.new()
     iv = r.read(16)  # Always 16 bytes for AES
     encryptor = AES.new(base64.b64decode(site.cryptkey), AES.MODE_CBC, iv)
-    cipher = encryptor.encrypt(s + ' ' * (16 - (len(s) % 16)))  # Pad to even 16 bytes
+    cipher = encryptor.encrypt(s.encode('ascii') + b' ' * (16 - (len(s) % 16)))  # Pad to even 16 bytes
 
     # Base64-encode the response, just to be consistent
     return "%s&%s" % (
index 353548b84cf4fde6036ba74e0822b6181c8b4a75..331ac90f526c308bb60310f1ce75a80c500dc2f4 100644 (file)
@@ -1,8 +1,8 @@
 from django.db import connection
 from django.conf import settings
 
-from Crypto.Hash import SHA256
-from Crypto import Random
+from Cryptodome.Hash import SHA256
+from Cryptodome import Random
 
 from pgweb.mailqueue.util import send_simple_mail
 from pgweb.util.helpers import template_to_string
index aae1330c43d9b0f3f62671f753bbcb5d3946cc7c..e244d0774e7c4efe544fdb7b10a43ff7f667f57e 100644 (file)
@@ -1,7 +1,7 @@
 Django>=1.11,<1.12
 django-markdown==0.2.1
 psycopg2==2.7.6
-pycrypto==2.6
+pycryptodomex>=3.4.7,<3.5
 django_markwhat==1.4
 requests-oauthlib==0.4.0
 cvss==1.9
index 0e62f4c3aa972397e98b63722a63647f43243354..da13736cd6ebcd8b539e94da5d68915bc8f40c01 100755 (executable)
@@ -5,7 +5,7 @@
 # community authentication integration.
 #
 
-from Crypto import Random
+from Cryptodome import Random
 import base64
 
 if __name__ == "__main__":
index 13e5b28e7fe052c29efd95c2b13f3054d3f69b4e..79dac96cbe9d22513ee69d5bf4e2d8508ba42631 100644 (file)
@@ -30,9 +30,9 @@ import json
 import socket
 from urllib.parse import urlparse, urlencode, parse_qs
 import requests
-from Crypto.Cipher import AES
-from Crypto.Hash import SHA
-from Crypto import Random
+from Cryptodome.Cipher import AES
+from Cryptodome.Hash import SHA
+from Cryptodome import Random
 import time
 
 
@@ -58,7 +58,7 @@ def login(request):
         r = Random.new()
         iv = r.read(16)
         encryptor = AES.new(SHA.new(settings.SECRET_KEY.encode('ascii')).digest()[:16], AES.MODE_CBC, iv)
-        cipher = encryptor.encrypt(s + ' ' * (16 - (len(s) % 16)))  # pad to 16 bytes
+        cipher = encryptor.encrypt(s.encode('ascii') + b' ' * (16 - (len(s) % 16)))  # pad to 16 bytes
 
         return HttpResponseRedirect("%s?d=%s$%s" % (
             settings.PGAUTH_REDIRECT,
index 19106fd5762d0501e79059a2480d0e132b4b0f36..746a8ef58369eb5bc6e0e05f7b767495efe8ff93 100755 (executable)
@@ -6,8 +6,8 @@
 #
 
 import sys
-from Crypto import Random
-from Crypto.Cipher import AES
+from Cryptodome import Random
+from Cryptodome.Cipher import AES
 import base64
 import time
 import urllib.parse
@@ -59,7 +59,7 @@ if __name__ == "__main__":
     r = Random.new()
     iv = r.read(16)
     encryptor = AES.new(base64.b64decode(options.key), AES.MODE_CBC, iv)
-    cipher = encryptor.encrypt(s + ' ' * (16 - (len(s) % 16)))
+    cipher = encryptor.encrypt(s.encode('ascii') + b' ' * (16 - (len(s) % 16)))
 
     print("Paste the following after the receiving url:")
     print("?i=%s&d=%s" % (