summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/openai/crypt.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-03-25 21:21:09 +0100
committerGitHub <noreply@github.com>2024-03-25 21:21:09 +0100
commitcf3f8cc6bd7cf7eb0dca1afca4937066c1c1f291 (patch)
tree217e6fd1d3440d7e5fe1ce6907c2273d16567827 /g4f/Provider/openai/crypt.py
parent~ (diff)
parentUpdate min requirements, Add pycryptodome (diff)
downloadgpt4free-cf3f8cc6bd7cf7eb0dca1afca4937066c1c1f291.tar
gpt4free-cf3f8cc6bd7cf7eb0dca1afca4937066c1c1f291.tar.gz
gpt4free-cf3f8cc6bd7cf7eb0dca1afca4937066c1c1f291.tar.bz2
gpt4free-cf3f8cc6bd7cf7eb0dca1afca4937066c1c1f291.tar.lz
gpt4free-cf3f8cc6bd7cf7eb0dca1afca4937066c1c1f291.tar.xz
gpt4free-cf3f8cc6bd7cf7eb0dca1afca4937066c1c1f291.tar.zst
gpt4free-cf3f8cc6bd7cf7eb0dca1afca4937066c1c1f291.zip
Diffstat (limited to 'g4f/Provider/openai/crypt.py')
-rw-r--r--g4f/Provider/openai/crypt.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/g4f/Provider/openai/crypt.py b/g4f/Provider/openai/crypt.py
new file mode 100644
index 00000000..e7f35190
--- /dev/null
+++ b/g4f/Provider/openai/crypt.py
@@ -0,0 +1,66 @@
+import json
+import base64
+import hashlib
+import random
+from Crypto.Cipher import AES
+
+def pad(data: str) -> bytes:
+ # Convert the string to bytes and calculate the number of bytes to pad
+ data_bytes = data.encode()
+ padding = 16 - (len(data_bytes) % 16)
+ # Append the padding bytes with their value
+ return data_bytes + bytes([padding] * padding)
+
+def encrypt(data, key):
+ salt = ""
+ salted = ""
+ dx = bytes()
+
+ # Generate salt, as 8 random lowercase letters
+ salt = "".join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(8))
+
+ # Our final key and IV come from the key and salt being repeatedly hashed
+ for x in range(3):
+ dx = hashlib.md5(dx + key.encode() + salt.encode()).digest()
+ salted += dx.hex()
+
+ # Pad the data before encryption
+ data = pad(data)
+
+ aes = AES.new(
+ bytes.fromhex(salted[:64]), AES.MODE_CBC, bytes.fromhex(salted[64:96])
+ )
+
+ return json.dumps(
+ {
+ "ct": base64.b64encode(aes.encrypt(data)).decode(),
+ "iv": salted[64:96],
+ "s": salt.encode().hex(),
+ }
+ )
+
+def unpad(data: bytes) -> bytes:
+ # Extract the padding value from the last byte and remove padding
+ padding_value = data[-1]
+ return data[:-padding_value]
+
+def decrypt(data: str, key: str):
+ # Parse JSON data
+ parsed_data = json.loads(base64.b64decode(data))
+ ct = base64.b64decode(parsed_data["ct"])
+ iv = bytes.fromhex(parsed_data["iv"])
+ salt = bytes.fromhex(parsed_data["s"])
+
+ salted = ''
+ dx = b''
+ for x in range(3):
+ dx = hashlib.md5(dx + key.encode() + salt).digest()
+ salted += dx.hex()
+
+ aes = AES.new(
+ bytes.fromhex(salted[:64]), AES.MODE_CBC, iv
+ )
+
+ data = aes.decrypt(ct)
+ if data.startswith(b'[{"key":'):
+ return unpad(data).decode() \ No newline at end of file