Skip to content

Commit

Permalink
add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
DimmaDont committed Jan 11, 2025
1 parent 02da1fc commit 79e73ae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/crypt_sindresorhus_conf/crypt_sindresorhus_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class CryptSindresorhusConf:
def __init__(self, key, iv):
def __init__(self, key: bytes, iv: bytes):
self.iv = iv
logging.debug("Key: %d %s", len(key), key.hex())
logging.debug("IV: %d %s", len(iv), iv.hex())
Expand All @@ -24,14 +24,14 @@ def __init__(self, key, iv):

self.cipher = Cipher(AES(self.password), CBC(iv))

def encrypt(self, data):
def encrypt(self, data: bytes) -> bytes:
padder = PKCS7(128).padder()
padded_data = padder.update(data) + padder.finalize()
encryptor = self.cipher.encryptor()
encrypted = encryptor.update(padded_data) + encryptor.finalize()
return self.iv + b":" + encrypted

def decrypt(self, data):
def decrypt(self, data: bytes) -> bytes:
decryptor = self.cipher.decryptor()
# iv and data are separated by a ":"
decrypted = decryptor.update(data[17:]) + decryptor.finalize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class CryptSindresorhusConf:
def __init__(self, key, iv):
def __init__(self, key: bytes, iv: bytes):
self.iv = iv
logging.debug("Key: %d %s", len(key), key.hex())
logging.debug("IV: %d %s", len(iv), iv.hex())
Expand All @@ -16,16 +16,18 @@ def __init__(self, key, iv):
salt = iv.decode(encoding="utf-8", errors="replace").encode()
logging.debug("Salt: %d %s", len(salt), salt.hex())

self.password = PBKDF2(key, salt, 32, count=10_000, hmac_hash_module=SHA512)
self.password = PBKDF2(
key.decode(), salt, 32, count=10_000, hmac_hash_module=SHA512
)
logging.debug("Password: %d %s", len(self.password), self.password.hex())

def encrypt(self, data):
def encrypt(self, data: bytes) -> bytes:
cipher = AES.new(self.password, AES.MODE_CBC, self.iv)
encrypted = cipher.encrypt(pad(data, AES.block_size))
encrypted = cipher.encrypt(pad(data, 16))
return self.iv + b":" + encrypted

def decrypt(self, payload):
def decrypt(self, data: bytes) -> bytes:
cipher = AES.new(self.password, AES.MODE_CBC, self.iv)
# iv and data are separated by a ":"
decrypted = cipher.decrypt(payload[AES.block_size + 1 :])
return unpad(decrypted, AES.block_size)
decrypted = cipher.decrypt(data[16 + 1 :])
return unpad(decrypted, 16)
6 changes: 3 additions & 3 deletions tests/test_crypt_sindresorhus_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ def test_encrypt(self):

def test_json(self):
data = json.loads(self.plaintext)
self.assertEqual(data['c'], 1)
self.assertEqual(data['b'], 2)
self.assertEqual(data['a'], 3)
self.assertEqual(data["c"], 1)
self.assertEqual(data["b"], 2)
self.assertEqual(data["a"], 3)

0 comments on commit 79e73ae

Please sign in to comment.