Skip to content

Commit 351f66c

Browse files
Refactor oids processing to fix bad encoding with big numbers
1 parent 6690150 commit 351f66c

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

‎CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Given a version number MAJOR.MINOR.PATCH, increment:
1313

1414

1515
## [Unreleased]
16+
### Fixed
17+
- OID integer encoding when single number has more than 2 bytes
1618

1719
## [2.0.2] - 2021-11-09
1820
### Fixed

‎ellipticcurve/utils/oid.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@ def oidFromHex(hexadecimal):
1010
byte, remainingBytes = remainingBytes[0:2], remainingBytes[2:]
1111
byteInt = intFromHex(byte)
1212
if byteInt >= 128:
13-
oidInt = byteInt - 128
13+
oidInt = (128 * oidInt) + (byteInt - 128)
1414
continue
15-
oidInt = oidInt * 128 + byteInt
15+
oidInt = (128 * oidInt) + byteInt
1616
oid.append(oidInt)
1717
oidInt = 0
1818
return oid
1919

2020

2121
def oidToHex(oid):
2222
hexadecimal = hexFromInt(40 * oid[0] + oid[1])
23-
byteArray = []
24-
for oidInt in oid[2:]:
25-
endDelta = 0
26-
while True:
27-
byteInt = oidInt % 128 + endDelta
28-
oidInt = oidInt // 128
29-
endDelta = 128
30-
byteArray.append(byteInt)
31-
if oidInt == 0:
32-
break
33-
hexadecimal += "".join(hexFromInt(byteInt).zfill(2) for byteInt in reversed(byteArray))
34-
byteArray = []
23+
for number in oid[2:]:
24+
hexadecimal += _oidNumberToHex(number)
3525
return hexadecimal
26+
27+
28+
def _oidNumberToHex(number):
29+
hexadecimal = ""
30+
endDelta = 0
31+
while number > 0:
32+
hexadecimal = hexFromInt((number % 128) + endDelta) + hexadecimal
33+
number //= 128
34+
endDelta = 128
35+
return hexadecimal or "00"

0 commit comments

Comments
 (0)