about summary refs log tree commit diff
path: root/.venv/lib/python3.12/site-packages/ellipticcurve/utils/oid.py
blob: dbfebe79a5cd1189d72aad8e002d53b639f792d9 (plain)
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
from .binary import intFromHex, hexFromInt


def oidFromHex(hexadecimal):
    firstByte, remainingBytes = hexadecimal[:2], hexadecimal[2:]
    firstByteInt = intFromHex(firstByte)
    oid = [firstByteInt // 40, firstByteInt % 40]
    oidInt = 0
    while len(remainingBytes) > 0:
        byte, remainingBytes = remainingBytes[0:2], remainingBytes[2:]
        byteInt = intFromHex(byte)
        if byteInt >= 128:
            oidInt = (128 * oidInt) + (byteInt - 128)
            continue
        oidInt = (128 * oidInt) + byteInt
        oid.append(oidInt)
        oidInt = 0
    return oid


def oidToHex(oid):
    hexadecimal = hexFromInt(40 * oid[0] + oid[1])
    for number in oid[2:]:
        hexadecimal += _oidNumberToHex(number)
    return hexadecimal


def _oidNumberToHex(number):
    hexadecimal = ""
    endDelta = 0
    while number > 0:
        hexadecimal = hexFromInt((number % 128) + endDelta) + hexadecimal
        number //= 128
        endDelta = 128
    return hexadecimal or "00"