Skip to content

Commit c0cd162

Browse files
committed
Added codec: hexagram
1 parent 1ebbdf8 commit c0cd162

4 files changed

Lines changed: 59 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ o
213213

214214
#### BaseXX
215215

216-
- [X] `ascii85`: classical ASCII85 (Python3 only)
217-
- [X] `baseN`: see [base encodings](https://python-codext.readthedocs.io/en/latest/enc/base.html) (incl base32, 36, 45, 58, 62, 63, 64, 91, 100, 122)
216+
- [X] `baseN`: see [base encodings](https://python-codext.readthedocs.io/en/latest/enc/base.html) (incl [z]base32, 36, 45, 58, 62, 63, 64, [z]85, 91, 100, 122)
218217
- [X] `base-genericN`: see [base encodings](https://python-codext.readthedocs.io/en/latest/enc/base.html) ; supports any possible base
219218

219+
This category also contains `ascii85`, `adobe`, `[x]btoa`, `zeromq` with the `base85` codec.
220+
220221
#### Binary
221222

222223
- [X] `baudot`: supports CCITT-1, CCITT-2, EU/FR, ITA1, ITA2, MTK-2 (Python3 only), UK, ...
@@ -302,6 +303,7 @@ o
302303

303304
#### Steganography
304305

306+
- [X] `hexagram`: uses Base64 and encodes the result to a charset of [I Ching hexagrams](https://en.wikipedia.org/wiki/Hexagram_%28I_Ching%29) (as implemented [here](https://github.com/qntm/hexagram-encode))
305307
- [X] `klopf`: aka Klopf code ; Polybius square with trivial alphabetical distribution
306308
- [X] `resistor`: aka resistor color codes
307309
- [X] `rick`: aka Rick cipher (in reference to Rick Astley's song "*Never gonna give you up*")

codext/stegano/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: UTF-8 -*-
2+
from .hexagram import *
23
from .klopf import *
34
from .resistor import *
45
from .rick import *

codext/stegano/hexagram.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Hexagram Codec - hexagram content encoding.
3+
4+
This codec:
5+
- en/decodes strings from str to str
6+
- en/decodes strings from bytes to bytes
7+
- decodes file content to str (read)
8+
- encodes file content from str to bytes (write)
9+
"""
10+
from ..__common__ import *
11+
12+
13+
if PY3:
14+
__examples__ = {
15+
'enc(hexagram|iching|i-ching-hexagrams)': {'this is a test': "䷰䷭䷚䷔䷞䷺䷗䷔䷞䷺䷗䷚䷏䷊䷂䷕䷞䷈䷇☯"},
16+
}
17+
18+
ENCMAP = {c1: c2 for c1, c2 in zip("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
19+
"䷁䷗䷆䷒䷎䷣䷭䷊䷏䷲䷧䷵䷽䷶䷟䷡䷇䷂䷜䷻䷦䷾䷯䷄䷬䷐䷮䷹䷞䷰䷛䷪䷖䷚䷃䷨䷳䷕"
20+
"䷑䷙䷢䷔䷿䷥䷷䷝䷱䷍䷓䷩䷺䷼䷴䷤䷸䷈䷋䷘䷅䷉䷠䷌䷫䷀☯")}
21+
DECMAP = {c2: c1 for c1, c2 in ENCMAP.items()}
22+
23+
def hexagram_encode(input, errors="strict"):
24+
return "".join(ENCMAP[c] for c in codecs.encode(input, "base64")), len(input)
25+
26+
def hexagram_decode(input, errors="strict"):
27+
r, ehandler = "", handle_error("hexagram", errors, decode=True)
28+
for i, c in enumerate(input):
29+
try:
30+
r += DECMAP[c]
31+
except KeyError:
32+
r += ehandler(c, i, r)
33+
return codecs.decode(r, "base64"), len(input)
34+
35+
add("hexagram", hexagram_encode, hexagram_decode, printables_rate=0.,
36+
pattern=r"^(?:(?:i-ching-)?hexagrams?|i-?ching)$")
37+

docs/enc/stegano.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44

55
-----
66

7+
### Hexagrams (I Ching)
8+
9+
This uses Base64 and then encodes output characters to [I Ching Hexagrams](https://en.wikipedia.org/wiki/Hexagram_%28I_Ching%29) such that implemented [here](https://github.com/qntm/hexagram-encode).
10+
11+
**Codec** | **Conversions** | **Aliases** | **Comment**
12+
:---: | :---: | --- | ---
13+
`hexagram` | text <-> hexagrams-encoded Base64 | `hexagrams`, `i-ching-hexagrams`, `iching` | Python3 only
14+
15+
```python
16+
>>> codext.encode("this is a test", "hexagram")
17+
'䷰䷭䷚䷔䷞䷺䷗䷔䷞䷺䷗䷚䷏䷊䷂䷕䷞䷈䷇☯'
18+
>>> codext.decode("䷰䷭䷚䷔䷞䷺䷗䷔䷞䷺䷗䷚䷏䷊䷂䷕䷞䷈䷇☯", "iching")
19+
'this is a test'
20+
```
21+
22+
-----
23+
724
### Klopf Code
825

926
This is a Polybius code with the trivial alphabetical distribution ("A" -> (1,1), "B" -> (2,1), ...). This can be tested [here](https://gc.de/gc/klopfcode/).

0 commit comments

Comments
 (0)