[Python] Formating strings and converting between representations

Discussão em 'Python' iniciado por Stack, Janeiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a library in which I serialize data. The issue is when I tried to visualize the serialization, I get different results from if I had no formatting to the serialization.

    # ansi colour codes
    g='\033[0;32m'
    lp='\033[1;35m'
    lc='\033[1;36m'
    y='\033[1;33m'
    r='\033[0m'
    bb='\033[1;94m'
    gr='\033[1;90m'
    bc='\033[1;96m'
    re='\033[1;31m'
    rb='\033[1;91m'
    w='\033[1;97m'

    class Signature:
    def __init__(self, r_x, value):
    self.r_x = r_x
    self.value = value

    def der(self, display=False):
    if not display:
    rxbin = self.r_x.to_bytes(32, 'big')
    # removing all null bytes
    rxbin = rxbin.lstrip(b"\x00")
    # if rxbin has high bit add \x00
    if rxbin[0] & 0x80:
    rxbin = b'\x00' + rxbin
    result = bytes([2, len(rxbin)]) + rxbin
    sigbin = self.value.to_bytes(32, 'big')
    sigbin = sigbin.lstrip(b"\x00")
    if sigbin[0] & 0x80:
    sigbin = b'\x00' + sigbin
    result += bytes([2, len(sigbin)]) + sigbin
    return bytes([0x30, len(result)]) + result
    else:
    rxbin = self.r_x.to_bytes(32, 'big')
    # removing all null bytes
    rxbin = rxbin.lstrip(b"\x00")
    # if rxbin has high bit add \x00
    if rxbin[0] & 0x80:
    rxbin = b'\x00' + rxbin

    r_x_value = lp + f"{hex(int.from_bytes(rxbin, 'big'))}".lstrip("0x") + r
    r_x_length = lc + f"{hex(len(rxbin))}".lstrip('0x') + r
    r_prefxbyte = re + f"02" + r
    # b'\x02'
    result = bytes([2, len(rxbin)]) + rxbin

    sigbin = self.value.to_bytes(32, 'big')
    sigbin = sigbin.lstrip(b"\x00")
    if sigbin[0] & 0x80:
    sigbin = b'\x00' + sigbin
    result += bytes([2, len(sigbin)]) + sigbin


    sig_value = y + f"{hex(int.from_bytes(sigbin, 'big'))}".lstrip("0x") + r
    sig_length = gr + f"{hex(len(sigbin))}".lstrip('0x') + r
    sig_prefxbyte = bb + "02" + r


    prefix = g + f"{hex(0x30)}".lstrip("0x") + r + w + f"{hex(len(result))}".lstrip("0x") + r

    print()
    print("Distinguished encoding representation for signatures in hex base. der signature serialization:")
    print()
    print(prefix + r_prefxbyte +r_x_length + r_x_value + sig_prefxbyte + sig_length+ sig_value )
    print()
    print("key:")
    print(g + "Signature prefix "+ r )
    print(w+ "Length of complete signature" + r)
    print(y + "signature value " + r )
    print(gr + "signature length " + r )
    print(bb + "signature prefix " + r )

    print(lc + "r_x length " + r )
    print(lp + "r_x value " + r )
    print(re + "r_x prefix " + r )
    print()


    where I have added the if-else statement in .der() method to help with visualizing the serialization.

    using the given values from here, i.e.

    r = 0x37206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c6
    s = 0x8ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec


    I yield the correct serialization when using the original .der() method (without display)

    3045022037206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c60221008ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec


    but my colorized version, is slightly different:

    3045022037206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c602218ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec


    where all thats different is a missing 00 between the signature value length, and the actual signature value. i.e

    correct serialization: 3045022037206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c60221008ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec

    colorised serialization with missing 00: 3045022037206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c602218ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec

    Can anyone see what it is I am doing wrong? I have check and double checked the implementation and cannot see why my colorised printed version would be different than the version produced by the original .der() method.

    Here is a screenshot of the goal of this display option [​IMG]

    Continue reading...

Compartilhe esta Página