@simontiger/

Parity & Checksum

Python

No description

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.1389742202594353","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"ZGVmIHBhcml0eShtZXNzYWdlLCBkZWJ1Zz1GYWxzZSk6CiAgYnl0ZXMgPSBbb3JkKGkpIGZvciBpIGluIG1lc3NhZ2VdCgogIHBhcml0eSA9IEZhbHNlCiAgZGVidWdfb3V0ID0gIiIKCiAgZm9yIGJ5dGUgaW4gYnl0ZXM6CiAgICBmb3IgaSBpbiByYW5nZSg4KToKICAgICAgaWYgYnl0ZSAmICgweDgwID4+IGkpOgogICAgICAgIHBhcml0eSA9IG5vdCBwYXJpdHkKICAgICAgICBkZWJ1Z19vdXQgKz0gIjEiCiAgICAgIGVsc2U6CiAgICAgICAgZGVidWdfb3V0ICs9ICIwIgogICAgZGVidWdfb3V0ICs9ICIgIgogIAogIGRlYnVnX291dCArPSAiMSIgaWYgcGFyaXR5IGVsc2UgIjAiCgogIGlmIGRlYnVnOiBwcmludChkZWJ1Z19vdXQpCiAgcmV0dXJuIHBhcml0eQoKZGVmIGNoZWNrc3VtKG1lc3NhZ2UsIGRlYnVnPUZhbHNlKToKICBieXRlcyA9IFtvcmQoaSkgZm9yIGkgaW4gbWVzc2FnZV0KCiAgY2hlY2tzdW0gPSAwCiAgZGVidWdfb3V0ID0gIiIKCiAgZm9yIGJ5dGUgaW4gYnl0ZXM6CiAgICBjaGVja3N1bSArPSBieXRlCiAgICBkZWJ1Z19vdXQgKz0gIlxuICAiCiAgICBmb3IgaSBpbiByYW5nZSg4KToKICAgICAgZGVidWdfb3V0ICs9ICIxIiBpZiBieXRlICYgKDB4ODAgPj4gaSkgZWxzZSAiMCIKICAKICBkZWJ1Z19vdXQgKz0gIlxuLS0tLS0tLS0tLSIKICBkZWJ1Z19vdXQgKz0gIlxuIiArIGJpbihjaGVja3N1bSlbMjpdCgogIGlmIGRlYnVnOiBwcmludChkZWJ1Z19vdXQpCiAgcmV0dXJuIHBhcml0eQoKY2hlY2tzdW0oIkhlbGxvLCB3b3JsZCEiLCBUcnVlKQ=="},"asBuffer":null},"loaded":true}}
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
def parity(message, debug=False):
  bytes = [ord(i) for i in message]

  parity = False
  debug_out = ""

  for byte in bytes:
    for i in range(8):
      if byte & (0x80 >> i):
        parity = not parity
        debug_out += "1"
      else:
        debug_out += "0"
    debug_out += " "
  
  debug_out += "1" if parity else "0"

  if debug: print(debug_out)
  return parity

def checksum(message, debug=False):
  bytes = [ord(i) for i in message]

  checksum = 0
  debug_out = ""

  for byte in bytes:
    checksum += byte
    debug_out += "\n  "
    for i in range(8):
      debug_out += "1" if byte & (0x80 >> i) else "0"
  
  debug_out += "\n----------"
  debug_out += "\n" + bin(checksum)[2:]

  if debug: print(debug_out)
  return parity

checksum("Hello, world!", True)