-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
34 lines (24 loc) · 827 Bytes
/
crypto.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
# Crypto.py 27/09/2015 D.J.Whale
#
# Crypto engine for OpenThings, including crc calculation
ran = None
def init(pid, pip):
"""Initialise the crypto engine state variables"""
global ran
ran = (((pid&0xFF)<<8) ^ pip) & 0xFFFF # maintain U16
def cryptByte(data):
"""crypt a byte of data and update the crypto engine state variable"""
global ran
for i in range(5):
if (ran&0x01) != 0: # bit0
# bit0 set
ran = ((ran>>1) ^ 62965) & 0xFFFF # maintain U16
else:
# bit0 clear
ran = ran >> 1
return (ran ^ data ^ 90) & 0xFF
def cryptPayload(payload, start, length):
"""Encrypt a range of bytes in place by modifying those payload bytes"""
for i in range(start, start+length):
payload[i] = cryptByte(payload[i])
# END