def hex_to_der()

in tools/rfc-to-der/rfc-to-der.py [0:0]


def hex_to_der(p: str, g: int=2) -> bytearray:
    # only accept generators of 2 and 5
    if g not in (2, 5):
        raise ValueError("invalid generator")

    # compile the asn1 specification
    path = os.path.join(__dir__, "dhparam.asn")
    encoder = asn1tools.compile_files(path)

    # remove formatting and convert hex to integer
    p = p.replace(" ", "").replace("\n", "")
    p = int(p, 16)

    return encoder.encode("DHParameter", {
        "prime": p,
        "base": g,
    })