File tree 3 files changed +35
-15
lines changed
3 files changed +35
-15
lines changed Original file line number Diff line number Diff line change 1
1
import base64
2
2
3
3
4
- def encode_to_b16 (inp : str ) -> bytes :
4
+ def base16_encode (inp : str ) -> bytes :
5
5
"""
6
6
Encodes a given utf-8 string into base-16.
7
7
8
- >>> encode_to_b16 ('Hello World!')
8
+ >>> base16_encode ('Hello World!')
9
9
b'48656C6C6F20576F726C6421'
10
- >>> encode_to_b16 ('HELLO WORLD!')
10
+ >>> base16_encode ('HELLO WORLD!')
11
11
b'48454C4C4F20574F524C4421'
12
- >>> encode_to_b16 ('')
12
+ >>> base16_encode ('')
13
13
b''
14
14
"""
15
15
# encode the input into a bytes-like object and then encode b16encode that
16
16
return base64 .b16encode (inp .encode ("utf-8" ))
17
17
18
18
19
- def decode_from_b16 (b16encoded : bytes ) -> str :
19
+ def base16_decode (b16encoded : bytes ) -> str :
20
20
"""
21
21
Decodes from base-16 to a utf-8 string.
22
22
23
- >>> decode_from_b16 (b'48656C6C6F20576F726C6421')
23
+ >>> base16_decode (b'48656C6C6F20576F726C6421')
24
24
'Hello World!'
25
- >>> decode_from_b16 (b'48454C4C4F20574F524C4421')
25
+ >>> base16_decode (b'48454C4C4F20574F524C4421')
26
26
'HELLO WORLD!'
27
- >>> decode_from_b16 (b'')
27
+ >>> base16_decode (b'')
28
28
''
29
29
"""
30
30
# b16decode the input into bytes and decode that into a human readable string
File renamed without changes.
Original file line number Diff line number Diff line change 1
1
import base64
2
2
3
3
4
- def main () -> None :
5
- inp = input ("->" )
6
- encoded = inp .encode ("utf-8" ) # encoded the input (we need a bytes like object)
7
- a85encoded = base64 .a85encode (encoded ) # a85encoded the encoded string
8
- print (a85encoded )
9
- print (base64 .a85decode (a85encoded ).decode ("utf-8" )) # decoded it
4
+ def base85_encode (string : str ) -> bytes :
5
+ """
6
+ >>> base85_encode("")
7
+ b''
8
+ >>> base85_encode("12345")
9
+ b'0etOA2#'
10
+ >>> base85_encode("base 85")
11
+ b'@UX=h+?24'
12
+ """
13
+ # encoded the input to a bytes-like object and then a85encode that
14
+ return base64 .a85encode (string .encode ("utf-8" ))
15
+
16
+
17
+ def base85_decode (a85encoded : bytes ) -> str :
18
+ """
19
+ >>> base85_decode(b"")
20
+ ''
21
+ >>> base85_decode(b"0etOA2#")
22
+ '12345'
23
+ >>> base85_decode(b"@UX=h+?24")
24
+ 'base 85'
25
+ """
26
+ # a85decode the input into bytes and decode that into a human readable string
27
+ return base64 .a85decode (a85encoded ).decode ("utf-8" )
10
28
11
29
12
30
if __name__ == "__main__" :
13
- main ()
31
+ import doctest
32
+
33
+ doctest .testmod ()
You can’t perform that action at this time.
0 commit comments