Syndace / python-omemo

An open python implementation of the OMEMO Multi-End Message and Object Encryption protocol.
MIT License
41 stars 6 forks source link

Use a much simpler way to generate the colon-separated hex format #28

Closed horazont closed 4 years ago

horazont commented 4 years ago

This assumes that ik_pub is a bytes object, otherwise this will go down the drain. Removes the need for re and codecs.

FWIW, using binascii.b2a_hex would be preferred over explicitly invoking codecs I guess.

Example:

>>> import random
>>> noise = random.getrandbits(128).to_bytes(16, "little")
>>> ":".join("{:02x}".format(octet) for octet in noise)
'10:76:1f:19:21:56:15:0e:33:f6:92:bf:ca:b2:ab:19'
>>> import binascii
>>> binascii.b2a_hex(noise)
b'10761f192156150e33f692bfcab2ab19'
>>> import codecs
>>> import re
>>> noise_hex = codecs.getencoder("hex")(noise)[0].decode("US-ASCII").upper()
>>> noise_hex = ":".join(re.findall("..?", noise_hex))
>>> noise_hex
'10:76:1F:19:21:56:15:0E:33:F6:92:BF:CA:B2:AB:19'

Note that this PR introduces lower case format, which I personally find easier to read. To change it to upper case, change the x to a X (or let me know so that I can fix it).

Syndace commented 4 years ago

That's obviously a lot easier and cleaner!

Two things:

  1. I don't care whether it's upper or lower case, but I'd like the output of the code to be consistent with the "wanted format" as defined in the comment above (line 132). Please update either the comment or the x of the format specifier.
  2. As you know I want to switch the license soon(ish). To do so, I need the permission of all contributors. As a future contributor, do you give me the permission to switch the license to MIT?

Thanks!

horazont commented 4 years ago

@Syndace This change shall be under CC0.

This allows you to redistribute it with whatever license you like, right now or in the future.