· 5 years ago · Jun 12, 2020, 10:48 PM
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import hashlib
5import requests
6import xml.etree.ElementTree as ElementTree
7from md5crypt import md5crypt
8
9def _parse(response):
10 return ElementTree.fromstring(response)
11
12def token():
13 return None
14
15def common_headers():
16 return {'User-Agent': 'xxxxx',
17 'X-Uuid': 'uuid'
18 }
19
20def _find(xml, key):
21 """Find text for element. If element is not found empty string is returned"""
22 return xml.findtext(key, '')
23
24def _post(path, data=None):
25 """
26 :type data: dict
27 """
28 if data is None:
29 data = {}
30 data.setdefault('wst', token)
31 headers = common_headers()
32 response = requests.post('https://webshare.cz/api{}'.format(path), data=data, headers=headers)
33 return response.content
34
35def get_salt(username):
36 """
37 POST /api/salt/ HTTP/1.1
38 Accept-Encoding: identity
39 Host: webshare.cz
40 Referer: https://webshare.cz/
41 Content-Type: application/x-www-form-urlencoded
42 """
43 response = _post('/salt/', data={'username_or_email': username})
44 root = _parse(response)
45 status = _find(root, 'status')
46 if status == 'OK':
47 return _find(root, 'salt')
48 else:
49 return None
50
51
52def hash_password(username, password, salt=None):
53 if salt is None:
54 salt = get_salt(username)
55 """Creates password hash with salt from Webshare API"""
56 print hashlib.sha1(md5crypt(password, salt=salt).encode('utf-8')).hexdigest()
57
58
59print hash_password('username', 'password')