-
Notifications
You must be signed in to change notification settings - Fork 10
/
ps1.py
90 lines (65 loc) · 1.87 KB
/
ps1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import typing
def example(data: bytes) -> bytes:
"""
Convert utf-8 encoded bytes to uppercase and return modified utf-8 encoded bytes
>>> example(b'hello')
b'HELLO'
>>> example(b'hello').decode()
'HELLO'
>>> example('привіт'.encode())
b'\xd0\x9f\xd0\xa0\xd0\x98\xd0\x92\xd0\x86\xd0\xa2'
>>> example('привіт'.encode()).decode()
'ПРИВІТ'
"""
return data.decode("utf-8").upper().encode("utf-8")
def problem1(n: int) -> typing.List[int]:
"""
Generate a list of `n` random numbers in range [0,256)
Please use cryptographically-secure entropy source
see secrets module in python
# not doctest as output is random
> problem1(5)
[140, 7, 218, 46, 104]
"""
def problem2(n: int) -> bytes:
"""
Generate random `n` bytes
Please use cryptographically-secure entropy source
see secrets module in python
# not doctest as output is random
> problem2(5)
b'\x18s\x0b8B'
"""
def problem3(data: bytes) -> bytes:
"""
Manipulate given data bytes where each byte is multiplied * 2 % 256
In other words, input is a collection of bytes
You should multiply each of those bytes by 2 mod 256
(not to overflow)
and then return resulting bytes
>>> problem3(b'hello')
b'\xd0\xca\xd8\xd8\xde'
"""
def problem4(data: typing.List[bytes]) -> bytes:
"""
XOR all given bytes and output resulting XORed bytes
All inputs will be of same length
>>> problem4([
... b'hello',
... b'world',
... b'hello',
... ])
b'world'
"""
def problem5(data: str) -> bytes:
"""
Decode given hex-encoded string to bytes
>>> problem5('d0cad8d8de')
b'\xd0\xca\xd8\xd8\xde'
"""
def problem6(data: bytes) -> str:
"""
Encode given bytes to hex-encoded string
>>> problem6(b'hello')
'68656c6c6f'
"""