-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for Caesar cipher (#4)
* added support for Caesar cipher
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
""" | ||
|
||
from .base64 import * | ||
from .dumb import * | ||
from .hex import * | ||
from .url import * |
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,69 @@ | ||
"""Implement support for 'dumb' ciphers such as Caesar cipher.""" | ||
|
||
import string | ||
from typing import Union | ||
|
||
from binapy import binapy_decoder, binapy_encoder | ||
|
||
|
||
@binapy_encoder("caesar") | ||
def encode_caesar( | ||
bp: bytes, | ||
shift: int, | ||
alphabet: Union[None, str, bytes] = None, | ||
) -> bytes: | ||
"""Encode data with Caesar cipher. | ||
This shifts each character from `bp` by `shift` positions in the given `alphabet`. | ||
Characters from `bp` that are not in the alphabet are left as-is. | ||
Alphabet is usually `string.ascii_lowercase`, `string.ascii_uppercase`, but you may pass any | ||
alphabet, either as a `str` (which will be encoded using 'utf-8'), or as `bytes` directly. | ||
By default, alphabet will be auto-detected: | ||
- `string.ascii_uppercase` if all character from the input are uppercase letters ASCII codes | ||
- `string.ascii_lowercase` if all character from the input are lowercase letters ASCII codes | ||
- `string.ascii_letters` if all character from the input are letters (both upper and lower case) ASCII codes | ||
- the full ASCII range (0-127) if all characters are valid ASCII | ||
- the full octect range (0-255) otherwise | ||
Args: | ||
bp: input data. | ||
shift: number of places to shift each character in the alphabet. | ||
alphabet: alphabet to use. Leave `None` to try to auto-detect alphabet | ||
Returns: | ||
the result of applying Caesar-cipher with `shift` positions to `bp`. | ||
""" | ||
if not alphabet: | ||
if all(65 <= c <= 90 for c in bp): | ||
alphabet = string.ascii_uppercase | ||
elif all(97 <= c <= 122 for c in bp): | ||
alphabet = string.ascii_lowercase | ||
elif all(65 <= c <= 90 or 97 <= c <= 122 for c in bp): | ||
alphabet = string.ascii_letters | ||
elif all(0 <= c <= 127 for c in bp): | ||
alphabet = bytes(range(128)) | ||
else: | ||
alphabet = bytes(range(256)) | ||
|
||
if isinstance(alphabet, str): | ||
alphabet = alphabet.encode() | ||
|
||
return bytes( | ||
alphabet[(alphabet.index(c) + shift) % len(alphabet)] if c in alphabet else c | ||
for c in bp | ||
) | ||
|
||
|
||
@binapy_decoder("caesar") | ||
def decode_caesar( | ||
bp: bytes, | ||
shift: int, | ||
alphabet: Union[None, str, bytes] = None, | ||
) -> bytes: | ||
"""Decode data with Caesar cipher. | ||
Since encoding and decoding are symmetric, this is just an alias to `encode_caesar()` with an | ||
opposite shift. | ||
""" | ||
return encode_caesar(bp, -shift, alphabet) |
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