-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathNetflixCredentials.py
More file actions
84 lines (72 loc) · 2.36 KB
/
Copy pathNetflixCredentials.py
File metadata and controls
84 lines (72 loc) · 2.36 KB
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
import base64
from Cryptodome import Random
from Cryptodome.Cipher import AES
from Cryptodome.Util import Padding
from utils import uniq_id
class NetflixCredentials(object):
"""
Stuff shared between / used from service and addon"""
def __init__(self):
self.bs = 32
self.crypt_key = uniq_id()
def encode_credentials(self, email, password):
"""Returns the users stored credentials
Returns
-------
:obj:`dict` of :obj:`str`
The users stored account data
"""
# if everything is fine, we encode the values
if '' != email and '' != password:
return {
'email': self.encode(raw=email),
'password': self.encode(raw=password)
}
# if email is empty, we return an empty map
return {
'email': '',
'password': ''
}
def decode_credentials(self, email, password):
"""Returns the users stored credentials
Returns
-------
:obj:`dict` of :obj:`str`
The users stored account data
"""
# if everything is fine, we decode the values
if (email and '' != email) and (password and '' != password):
return {
'email': self.decode(enc=email),
'password': self.decode(enc=password)
}
# if email is empty, we return an empty map
return {
'email': '',
'password': ''
}
def encode(self, raw):
"""
Encodes data
:param data: Data to be encoded
:type data: str
:returns: string -- Encoded data
"""
raw = bytes(Padding.pad(data_to_pad=raw, block_size=self.bs))
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.crypt_key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw))
def decode(self, enc):
"""
Decodes data
:param data: Data to be decoded
:type data: str
:returns: string -- Decoded data
"""
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(self.crypt_key, AES.MODE_CBC, iv)
decoded = Padding.unpad(
padded_data=cipher.decrypt(enc[AES.block_size:]),
block_size=self.bs).decode('utf-8')
return decoded