about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFelipe Martins Diel <41558831+felipediel@users.noreply.github.com>2020-04-19T06·53-0300
committerGitHub <noreply@github.com>2020-04-19T06·53+0200
commit6cf9292b4498a5a42299cf51a0e41033c87ede4a (patch)
tree802bac86036ae94d1431e7d19e20e3a9e1c4bf47
parent65a1b04666026322ba8b1db6a044eb953a45d71a (diff)
Revert adler32 (#345)
* Revert adler32

* Fix checksum
-rw-r--r--broadlink/__init__.py30
1 files changed, 23 insertions, 7 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py
index 3a78b03b89..5253d7cfda 100644
--- a/broadlink/__init__.py
+++ b/broadlink/__init__.py
@@ -8,7 +8,6 @@ import struct
 import threading
 import time
 from datetime import datetime
-from zlib import adler32
 
 from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
@@ -121,8 +120,11 @@ def discover(timeout=None, local_ip_address=None, discover_ip_address='255.255.2
     packet[0x1c] = port & 0xff
     packet[0x1d] = port >> 8
     packet[0x26] = 6
+    
+    checksum = 0xbeaf
+    for b in packet:
+        checksum = (checksum + b) & 0xffff
 
-    checksum = adler32(packet, 0xbeaf) & 0xffff
     packet[0x20] = checksum & 0xff
     packet[0x21] = checksum >> 8
 
@@ -268,7 +270,10 @@ class device:
         if payload:
             payload += bytearray((16 - len(payload)) % 16)
 
-        checksum = adler32(payload, 0xbeaf) & 0xffff
+        checksum = 0xbeaf
+        for b in payload:
+            checksum = (checksum + b) & 0xffff
+
         packet[0x34] = checksum & 0xff
         packet[0x35] = checksum >> 8
 
@@ -276,7 +281,10 @@ class device:
         for i in range(len(payload)):
             packet.append(payload[i])
 
-        checksum = adler32(packet, 0xbeaf) & 0xffff
+        checksum = 0xbeaf
+        for b in packet:
+            checksum = (checksum + b) & 0xffff
+
         packet[0x20] = checksum & 0xff
         packet[0x21] = checksum >> 8
 
@@ -409,7 +417,10 @@ class bg1(device):
         for i in range(len(js)):
             packet.append(js[i])
 
-        checksum = adler32(packet[0x08:], 0xc0ad) & 0xffff
+        checksum = 0xc0ad
+        for b in packet[0x08:]:
+            checksum = (checksum + b) & 0xffff
+
         packet[0x06] = checksum & 0xff
         packet[0x07] = checksum >> 8
 
@@ -1034,7 +1045,9 @@ class lb1(device):
         packet[0x0a] = len(command)
         packet[0x0e:] = map(ord, command)
 
-        checksum = adler32(packet, 0xbeaf) & 0xffff
+        checksum = 0xbeaf
+        for b in packet:
+            checksum = (checksum + b) & 0xffff
 
         packet[0x00] = (0x0c + len(command)) & 0xff
         packet[0x06] = checksum & 0xff  # Checksum 1 position
@@ -1091,7 +1104,10 @@ def setup(ssid, password, security_mode):
     payload[0x85] = pass_length  # Character length of password
     payload[0x86] = security_mode  # Type of encryption (00 - none, 01 = WEP, 02 = WPA1, 03 = WPA2, 04 = WPA1/2)
 
-    checksum = adler32(payload, 0xbeaf) & 0xffff
+    checksum = 0xbeaf
+    for b in payload:
+        checksum = (checksum + b) & 0xffff
+
     payload[0x20] = checksum & 0xff  # Checksum 1 position
     payload[0x21] = checksum >> 8  # Checksum 2 position