-
Notifications
You must be signed in to change notification settings - Fork 0
/
regis.py
73 lines (63 loc) · 2.05 KB
/
regis.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
#!/usr/bin/env python
import base64
import struct
import secrets
import bytes
from Crypto.Cipher import AES
from Crypto.Hash import CMAC
def parse_bytes(typ, fmt, data):
try:
return struct.unpack(fmt, data)
except struct.error:
raise StructParseError(typ, fmt, data) from None
class Register:
"""
注册过程 生成密钥
"""
def __init__(self, joineui, deveui, appkey, nwkkey):
self.joineui = bytes.fromhex(joineui)
self.deveui = bytes.fromhex(deveui)
self.appkey = bytes.fromhex(appkey)
self.nwkkey = bytes.fromhex(nwkkey)
self.txdr = 5 # Uplink data rate index
self.txch = 1 # Channel index
self.gen_jskeys()
self.activation = False
self.activation_mode = 'OTAA'
self.ack = False
self.version = "1.1"
self.msg_file = "message.json"
# self.save()
def _initialize_session(self): # 私有方法
"""
Initialize session context according to optneg flag
Args:
optneg: 0 or 1
Returns:
None
"""
# if optneg:
# Server supports LoRaWAN 1.1 and later
# Generate FNwkSIntKey, SNwkSIntKey, NwkSEncKey and AppSKey
nwkskey_prefix = b''.join([
self.joinnonce[::-1],
self.joineui[::-1],
self.devnonce[::-1],
])
fnwksint_msg, snwksint_msg, nwksenc_msg = [
(prefix + nwkskey_prefix).ljust(AES_BLOCK, b'\x00')
for prefix in (b'\x01', b'\x03', b'\x04')
]
self.fnwksintkey, self.snwksintkey, self.nwksenckey = self.gen_keys(
self.nwkkey, (fnwksint_msg, snwksint_msg, nwksenc_msg)
)
appsmsg = b''.join([
b'\x02',
self.joinnonce[::-1],
self.joineui[::-1],
self.devnonce[::-1],
]).ljust(AES_BLOCK, b'\x00')
self.appskey, = self.gen_keys(self.appkey, (appsmsg,))
self.fcntup = self.rjcount0 = 0
self.activation = True
# self.save()