Skip to content

Commit a2da458

Browse files
committed
🗓 Nov 4, 2025 8:57:45 PM
🐛 fix with with write_bytes ✨ salsa20 encrypt/decrypt
1 parent 7d8396c commit a2da458

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

chepy/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,10 +1096,11 @@ def write_binary(self, path: str) -> None: # pragma: no cover
10961096
Examples:
10971097
>>> c = Chepy("some data").write_binary('/some/path/file')
10981098
"""
1099+
data = self._convert_to_bytes()
10991100
if isinstance(path, bytes): # pragma: no cover
11001101
path = path.decode()
11011102
with open(str(self._abs_path(path)), "wb+") as f:
1102-
f.write(self.state)
1103+
f.write(data)
11031104
self._info_logger("File written to {}".format(self._abs_path(path)))
11041105
return None
11051106

chepy/modules/encryptionencoding.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
AES = lazy_import.lazy_module("Crypto.Cipher.AES")
25+
Salsa20 = lazy_import.lazy_module("Crypto.Cipher.Salsa20")
2526
ARC4 = lazy_import.lazy_module("Crypto.Cipher.ARC4")
2627
DES = lazy_import.lazy_module("Crypto.Cipher.DES")
2728
ChaCha20 = lazy_import.lazy_module("Crypto.Cipher.ChaCha20")
@@ -705,6 +706,58 @@ def des_decrypt(
705706
self.state = cipher.decrypt(self._convert_to_bytes())
706707
return self
707708

709+
@ChepyDecorators.call_stack
710+
def salsa20_encrypt(
711+
self,
712+
key: str,
713+
nonce: str = "0000000000000000",
714+
key_format: str = "hex",
715+
nonce_format: str = "hex",
716+
) -> EncryptionEncodingT:
717+
"""Encrypt raw state with Salsa 20 rounds
718+
719+
Args:
720+
key (str): Required. The secret key
721+
nonce (str, optional): Nonce. Defaults to '0000000000000000'.
722+
key_format (str, optional): Format of key. Defaults to 'hex'.
723+
nonce_format (str, optional): Format of nonce. Defaults to 'hex'.
724+
725+
Returns:
726+
Chepy: The Chepy object.
727+
"""
728+
729+
key, nonce = self._convert_key(key, nonce, key_format, nonce_format)
730+
731+
cipher = Salsa20.new(key=key, nonce=nonce)
732+
self.state = cipher.encrypt(self._convert_to_bytes())
733+
return self
734+
735+
@ChepyDecorators.call_stack
736+
def salsa20_decrypt(
737+
self,
738+
key: str,
739+
nonce: str = "0000000000000000",
740+
key_format: str = "hex",
741+
nonce_format: str = "hex",
742+
) -> EncryptionEncodingT:
743+
"""Decrypt raw state encrypted with ChaCha 20 rounds.
744+
745+
Args:
746+
key (str): Required. The secret key
747+
nonce (str, optional): nonce for certain modes only. Defaults to '0000000000000000'.
748+
key_format (str, optional): Format of key. Defaults to 'hex'.
749+
nonce_format (str, optional): Format of nonce. Defaults to 'hex'.
750+
751+
Returns:
752+
Chepy: The Chepy object.
753+
"""
754+
755+
key, nonce = self._convert_key(key, nonce, key_format, nonce_format)
756+
757+
cipher = Salsa20.new(key=key, nonce=nonce)
758+
self.state = cipher.decrypt(self._convert_to_bytes())
759+
return self
760+
708761
@ChepyDecorators.call_stack
709762
def chacha_encrypt(
710763
self,

chepy/modules/encryptionencoding.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class EncryptionEncoding(ChepyCore):
3131
def rc4_decrypt(self: EncryptionEncodingT, key: str, key_format: RC4_FORMAT=...) -> EncryptionEncodingT: ...
3232
def des_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]="CBC", key_format: FORMAT="hex", iv_format: FORMAT="hex") -> EncryptionEncodingT: ...
3333
def des_decrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]="CBC", key_format: FORMAT="hex", iv_format: FORMAT="hex") -> EncryptionEncodingT: ...
34+
def salsa20_encrypt(self: EncryptionEncodingT, key: str, nonce: str=..., key_format: FORMAT="hex", nonce_format: FORMAT="hex") -> EncryptionEncodingT: ...
35+
def salsa20_decrypt(self: EncryptionEncodingT, key: str, nonce: str=..., key_format: FORMAT="hex", nonce_format: FORMAT="hex") -> EncryptionEncodingT: ...
3436
def chacha_encrypt(self: EncryptionEncodingT, key: str, nonce: str=..., key_format: FORMAT=..., nonce_format: FORMAT=...) -> EncryptionEncodingT: ...
3537
def chacha_decrypt(self: EncryptionEncodingT, key: str, nonce: str=..., key_format: FORMAT=..., nonce_format: FORMAT=...) -> EncryptionEncodingT: ...
3638
def triple_des_encrypt(self: EncryptionEncodingT, key: str, iv: str=..., mode: Literal["CBC", "OFB", "CTR", "ECB"]="CBC", key_format: FORMAT="hex", iv_format: FORMAT="hex") -> EncryptionEncodingT: ...

tests/test_encryptionencoding.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,29 @@ def test_monoalphabetic_substitution():
766766
assert Chepy("lol").monoalphabetic_substitution({"l": "t", "o": "s"}).o == b"tst"
767767

768768

769+
def test_salsa20():
770+
data = "securisec"
771+
enc_data = "07 89 19 8b d9 85 59 c7 45".replace(" ", "")
772+
assert (
773+
Chepy(data)
774+
.salsa20_encrypt(
775+
key="ff000000000000000000000000000000", nonce="0000000000000000"
776+
)
777+
.to_hex()
778+
.o
779+
== enc_data.encode()
780+
)
781+
assert (
782+
Chepy(enc_data)
783+
.from_hex()
784+
.salsa20_decrypt(
785+
key="ff000000000000000000000000000000", nonce="0000000000000000"
786+
)
787+
.o
788+
== data.encode()
789+
)
790+
791+
769792
def test_chacha_decrypt():
770793
assert (
771794
Chepy("0d118d5f5807747b085473553146a3c76cf1ef61b976519240")

0 commit comments

Comments
 (0)