-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add chacha20 and salsa20 ciphers (#46)
* Add salsa20 and chacha20 ciphers * Fix the hex escapes in documentation
- Loading branch information
Showing
7 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
from .aes import aes | ||
from .blowfish import blowfish | ||
from .chacha20 import chacha20 | ||
from .des3 import des3 | ||
from .rabbit import rabbit | ||
from .rc import rc4 | ||
from .rsa import rsa | ||
from .salsa20 import salsa20 | ||
from .serpent import serpent | ||
from .xor import xor | ||
|
||
__all__ = ["aes", "blowfish", "des3", "rabbit", "rc4", "rsa", "serpent", "xor"] | ||
__all__ = [ | ||
"aes", | ||
"blowfish", | ||
"chacha20", | ||
"des3", | ||
"rabbit", | ||
"rc4", | ||
"rsa", | ||
"salsa20", | ||
"serpent", | ||
"xor", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Optional | ||
from Cryptodome.Cipher import ChaCha20 as ChaCha20Cipher | ||
|
||
|
||
__all__ = ["chacha20"] | ||
|
||
|
||
class ChaCha20: | ||
def encrypt(self, key: bytes, data: bytes, nonce: Optional[bytes] = None) -> bytes: | ||
""" | ||
Encrypts buffer using ChaCha20 algorithm. | ||
:param key: Cryptographic key (32 bytes) | ||
:type key: bytes | ||
:param data: Buffer to be encrypted | ||
:type data: bytes | ||
:param nonce: Nonce value (8/12 bytes, defaults to `b"\\\\x00"*8` ) | ||
:type nonce: bytes, optional | ||
:return: Encrypted data | ||
:rtype: bytes | ||
""" | ||
if nonce is None: | ||
nonce = b"\x00" * 8 | ||
return ChaCha20Cipher.new(key=key, nonce=nonce).encrypt(data) | ||
|
||
def decrypt(self, key: bytes, data: bytes, nonce: Optional[bytes] = None) -> bytes: | ||
""" | ||
Decrypts buffer using ChaCha20 algorithm. | ||
:param key: Cryptographic key (32 bytes) | ||
:type key: bytes | ||
:param data: Buffer to be decrypted | ||
:type data: bytes | ||
:param nonce: Nonce value (8/12 bytes, defaults to `b"\\\\x00"*8` ) | ||
:type nonce: bytes, optional | ||
:return: Decrypted data | ||
:rtype: bytes | ||
""" | ||
if nonce is None: | ||
nonce = b"\x00" * 8 | ||
return ChaCha20Cipher.new(key=key, nonce=nonce).decrypt(data) | ||
|
||
|
||
chacha20 = ChaCha20() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Optional | ||
from Cryptodome.Cipher import Salsa20 as Salsa20Cipher | ||
|
||
|
||
__all__ = ["salsa20"] | ||
|
||
|
||
class Salsa20: | ||
def encrypt(self, key: bytes, data: bytes, nonce: Optional[bytes] = None) -> bytes: | ||
""" | ||
Encrypts buffer using Salsa20 algorithm. | ||
:param key: Cryptographic key (16/32 bytes) | ||
:type key: bytes | ||
:param data: Buffer to be encrypted | ||
:type data: bytes | ||
:param nonce: Nonce value (8 bytes, defaults to `b"\\\\x00"*8` ) | ||
:type nonce: bytes, optional | ||
:return: Encrypted data | ||
:rtype: bytes | ||
""" | ||
if nonce is None: | ||
nonce = b"\x00" * 8 | ||
return Salsa20Cipher.new(key=key, nonce=nonce).encrypt(data) | ||
|
||
def decrypt(self, key: bytes, data: bytes, nonce: Optional[bytes] = None) -> bytes: | ||
""" | ||
Decrypts buffer using Salsa20 algorithm. | ||
:param key: Cryptographic key (16/32 bytes) | ||
:type key: bytes | ||
:param data: Buffer to be decrypted | ||
:type data: bytes | ||
:param nonce: Nonce value (8 bytes, defaults to `b"\\\\x00"*8` ) | ||
:type nonce: bytes, optional | ||
:return: Decrypted data | ||
:rtype: bytes | ||
""" | ||
if nonce is None: | ||
nonce = b"\x00" * 8 | ||
return Salsa20Cipher.new(key=key, nonce=nonce).decrypt(data) | ||
|
||
|
||
salsa20 = Salsa20() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters