Skip to content

Commit

Permalink
add test for crypto.py
Browse files Browse the repository at this point in the history
  • Loading branch information
justin1121 committed Sep 22, 2023
1 parent 697b6b4 commit 8da3458
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pycape/llms/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
NONCE_SIZE = 12


def aes_decrypt(ctxt: bytes, data_key: bytes) -> bytes:
def aes_decrypt(ctxt: bytes, key: bytes) -> bytes:
nonce, ctxt = ctxt[:NONCE_SIZE], ctxt[NONCE_SIZE:]
encryptor = aead.AESGCM(data_key)
encryptor = aead.AESGCM(key)
ptxt = encryptor.decrypt(nonce, ctxt, None)
return ptxt


def aes_encrypt(aes_key: bytes, ptxt: bytes):
encryptor = aead.AESGCM(aes_key)
nonce = os.urandom(NONCE_SIZE) # AESGCM nonce size is 12
def aes_encrypt(ptxt: bytes, key: bytes):
encryptor = aead.AESGCM(key)
nonce = os.urandom(NONCE_SIZE)
ctxt = encryptor.encrypt(nonce, ptxt, None)
return nonce + ctxt

Expand All @@ -29,7 +29,7 @@ def envelope_encrypt(public_key: bytes, data: Dict[str, Any]):
aes_key = os.urandom(32)
s = json.dumps(data)

enc_data = aes_encrypt(aes_key, s.encode())
enc_data = aes_encrypt(s.encode(), aes_key)

pub = serialization.load_pem_public_key(public_key)

Expand Down
17 changes: 17 additions & 0 deletions pycape/llms/crypto_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

from pycape.llms.crypto import aes_decrypt
from pycape.llms.crypto import aes_encrypt


def test_encrypt_decrypt():
expected = b"hi there"

key = os.urandom(32)
ciphertext = aes_encrypt(expected, key)

assert expected == aes_decrypt(ciphertext, key)


def test_envelope_encrypt():
pass

0 comments on commit 8da3458

Please sign in to comment.