-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_barcode_string.py
81 lines (71 loc) · 2.48 KB
/
create_barcode_string.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
#!/usr/bin/env python
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2015 Tobias Mueller <[email protected]>
#
from hashlib import md5
import logging
log = logging.getLogger(__name__)
class quotedphrase(type("foo")):
def __str__(self):
return '"%s"' % super(quotedphrase, self).__str__()
def create_string(connection):
"""A string to be parsed may look like this:
WIFI:T:WPA;S:trololol;P:"12345678";;
"""
s_con = connection['connection']
security = connection['802-11-wireless-security']
ssid_bytes = connection['802-11-wireless']['ssid']
ssid = bytes(ssid_bytes).decode('ascii')
log.info("Using SSID: %r", ssid)
if 'wep-key0' in security:
sectype = 'WEP'
password = security['wep-key0']
passphrase = quotedphrase(password)
# Hm, let's assume that it's a 128 bit passphrase
# http://pygmalion.nitri.de/convert-wep-passphrase-into-hex-75.html
l = len(password)
ba = bytearray(64)
for i in range(64):
c = "%c" % bytes(password[i % l])
print("C: %r" % c)
ba[i] = c
digest = md5(ba)
WEPSTRONGKEYSIZE = 13
passphrase = digest.hexdigest()[:26]
else:
sectype = 'WPA'
passphrase = security['psk']
s = "WIFI:"
s += "T:%s;" % sectype
s += "S:%s;" % ssid
# When the passphrase can be interpreted as hex, it shold be quoted.
# We manage that quoting with the quoted phrase class
#s += "P:\"%s\";" % passphrase
s += "P:%s;" % passphrase
s += ";"
return s
if __name__ == '__main__':
connection = {
'config': {
'id': 'foo',
},
'802-11-wireless-security' : {
'key-mgmt': 'wpa',
'psk': 'my passphrase',
},
}
print((create_string (connection)))