Skip to content

Commit 578f57d

Browse files
committed
Clarified on listing codecs from the CLI (#41)
1 parent b5414ec commit 578f57d

File tree

4 files changed

+87
-38
lines changed

4 files changed

+87
-38
lines changed

README.md

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $ echo -en "test" | codext encode base100
4343
👫👜👪👫
4444
```
4545

46-
### Chaining codecs
46+
### :chains: Chaining codecs
4747

4848
```sh
4949
$ echo -en "Test string" | codext encode reverse
@@ -62,7 +62,7 @@ $ echo -en "AGTCAGTCAGTGAGAAAGTCAGTGAGAAAGTGAGTGAGAAAGTGAGTCAGTGAGAAAGTCAGAAAGTG
6262
test string
6363
```
6464

65-
### Using macros
65+
### :twisted_rightwards_arrows: Using macros
6666

6767
```sh
6868
$ codext add-macro my-encoding-chain gzip base63 lzma base64
@@ -79,7 +79,9 @@ $ codext list macros
7979
example-macro
8080
```
8181

82-
## :computer: Usage (base CLI tool) <a href="https://twitter.com/intent/tweet?text=UnBase%20-%20Decode%20any%20multi-layer%20base-encoded%20string.%0D%0APython%20tool%20for%20decoding%20any%20base-encoded%20string,%20even%20when%20encoded%20with%20multiple%20layers.%0D%0Ahttps%3a%2f%2fgithub%2ecom%2fdhondta%2fpython-codext%0D%0A&hashtags=python,base,encodings,codecs,cryptography,stegano,steganography,ctftools"><img src="https://img.shields.io/badge/Tweet%20(unbase)--lightgrey?logo=twitter&style=social" alt="Tweet on unbase" height="20"/></a>
82+
## :desktop_computer: Usage (`baseXX` CLI tools) <a href="https://twitter.com/intent/tweet?text=UnBase%20-%20Decode%20any%20multi-layer%20base-encoded%20string.%0D%0APython%20tool%20for%20decoding%20any%20base-encoded%20string,%20even%20when%20encoded%20with%20multiple%20layers.%0D%0Ahttps%3a%2f%2fgithub%2ecom%2fdhondta%2fpython-codext%0D%0A&hashtags=python,base,encodings,codecs,cryptography,stegano,steganography,ctftools"><img src="https://img.shields.io/badge/Tweet%20(unbase)--lightgrey?logo=twitter&style=social" alt="Tweet on unbase" height="20"/></a>
83+
84+
Playing with base encodings.
8385

8486
```session
8587
$ echo "Test string !" | base122
@@ -106,16 +108,73 @@ $ echo "Test string !" | base91 | base85 | base36 | base58-flickr | unbase -f Te
106108
Test string !
107109
```
108110

109-
## :computer: Usage (Python)
111+
## :computer: Usage (CLI)
112+
113+
Listing codecs.
114+
115+
```session
116+
$ codext list encodings
117+
a1z26 adler32 affine alternative-rot ascii
118+
atbash autoclave bacon barbie base
119+
base1 base2 base3 base4 base8
120+
<<snipped>>
121+
```
122+
123+
Finding a codec based on a name.
124+
125+
```session
126+
$ codext search bitcoin
127+
base58
128+
```
129+
130+
Encoding a string.
110131

111-
Getting the list of available codecs:
132+
```sesssion
133+
$ echo -en "This is a test" | codext encode polybius
134+
44232443 2443 11 44154344
135+
```
136+
137+
Encoding a file.
138+
139+
```session
140+
$ echo -en "this is a test" > to_be_encoded.txt
141+
$ codext encode base64 < to_be_encoded.txt > text.b64
142+
$ cat text.b64
143+
dGhpcyBpcyBhIHRlc3Q=
144+
```
145+
146+
Chaining codecs.
147+
148+
```session
149+
$ echo -en "mrdvm6teie6t2cq=" | codext encode upper | codext decode base32 | codext decode base64
150+
test
151+
```
152+
153+
Iteratively guessing decodings.
154+
155+
```session
156+
$ echo -en "test" | codext encode base64 gzip | codext guess
157+
Codecs: gzip
158+
dGVzdA==
159+
$ echo -en "test" | codext encode base64 gzip | codext guess gzip -i base
160+
Codecs: gzip, base64
161+
test
162+
```
163+
164+
165+
## :snake: Usage (Python)
166+
167+
Getting the list of available codecs.
112168

113169
```python
114170
>>> import codext
115171

116172
>>> codext.list()
117173
['ascii85', 'base85', 'base100', 'base122', ..., 'tomtom', 'dna', 'html', 'markdown', 'url', 'resistor', 'sms', 'whitespace', 'whitespace-after-before']
118174

175+
Playing with some base encodings.
176+
177+
```python
119178
>>> codext.encode("this is a test", "base58-bitcoin")
120179
'jo91waLQA1NNeBmZKUF'
121180

@@ -130,7 +189,21 @@ Getting the list of available codecs:
130189

131190
>>> codecs.decode("👫👟👠👪🐗👠👪🐗👘🐗👫👜👪👫", "base100")
132191
'this is a test'
192+
```
193+
194+
Playing with some cryptography-based codecs.
133195

196+
```python
197+
>>> codext.encode("This is a test !", "vigenere-MYSECRETKET")
198+
'Ffaw kj e mowm !'
199+
200+
>>> codext.encode("This is a test !", "autoclave-SECRET")
201+
'Llkj ml t amkb !'
202+
```
203+
204+
Encoding/decoding with various other codecs.
205+
206+
```python
134207
>>> for i in range(8):
135208
print(codext.encode("this is a test", "dna-%d" % (i + 1)))
136209
GTGAGCCAGCCGGTATACAAGCCGGTATACAAGCAGACAAGTGAGCGGGTATGTGA
@@ -158,30 +231,6 @@ CACTCGGTCGGCCATATGTTCGGCCATATGTTCGTCTGTTCACTCGCCCATACACT
158231
f.read()
159232
'this is a test'
160233

161-
>>> codext.decode("""
162-
=
163-
X
164-
:
165-
x
166-
n
167-
r
168-
y
169-
Y
170-
y
171-
p
172-
a
173-
`
174-
n
175-
|
176-
a
177-
o
178-
h
179-
`
180-
g
181-
o
182-
z """, "whitespace-after+before")
183-
'CSC{not_so_invisible}'
184-
185234
>>> print(codext.encode("An example test string", "baudot-tape"))
186235
***.**
187236
. *

src/codext/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.16.0
1+
1.16.1

src/codext/__common__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def list_categories():
671671
c.append(d.rstrip("s"))
672672
# particular category, hardcoded from base/_base.py
673673
c += ["base-generic"]
674-
return c
674+
return list(set(c))
675675
list_categories()
676676

677677

src/codext/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def _format_action_invocation(self, action):
170170
listi = sparsers.add_parser("list", help="list items")
171171
lsparsers = listi.add_subparsers(dest="type", help="type of item to be listed", required=True)
172172
liste = lsparsers.add_parser("encodings", help="list encodings")
173-
liste.add_argument("category", nargs="+", help="selected categories")
173+
liste.add_argument("category", nargs="*", help="selected categories")
174174
listm = lsparsers.add_parser("macros", help="list macros")
175175
addm = sparsers.add_parser("add-macro", help="add a macro to the registry")
176176
addm.add_argument("name", help="macro's name")
@@ -198,13 +198,13 @@ def _format_action_invocation(self, action):
198198
# list encodings or macros
199199
elif args.command == "list":
200200
if args.type == "encodings":
201-
cats = args.category or list_categories()
202-
for c in sorted(cats):
203-
l = list_encodings(c)
204-
if len(l) > 0:
205-
if len(cats) > 0:
201+
if args.category:
202+
for c in sorted(args.category):
203+
if len(l := list_encodings(c)) > 0:
206204
print(c.upper() + ":")
207-
__print_tabular(l)
205+
__print_tabular(l)
206+
else:
207+
__print_tabular(list_encodings())
208208
elif args.type == "macros":
209209
l = list_macros()
210210
if len(l) > 0:

0 commit comments

Comments
 (0)