Skip to content

Commit cc1e18d

Browse files
committed
Get rid of int_to_bytes() function
We know the exact length, so use int.to_bytes() directly. Thanks to mesonium for the suggestion in #48.
1 parent d036431 commit cc1e18d

File tree

3 files changed

+3
-30
lines changed

3 files changed

+3
-30
lines changed

secretstorage/dhcrypto.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
algorithm.'''
99

1010
import hmac
11-
import math
1211
import os
1312
from hashlib import sha256
1413

@@ -27,12 +26,6 @@
2726
)
2827

2928

30-
def int_to_bytes(number: int, length: int) -> bytes:
31-
result = number.to_bytes(math.ceil(number.bit_length() / 8), 'big')
32-
# Pad with zero bytes if needed.
33-
return b'\x00' * (length - len(result)) + result
34-
35-
3629
DH_PRIME_1024 = int.from_bytes(DH_PRIME_1024_BYTES, 'big')
3730

3831

@@ -48,7 +41,7 @@ def __init__(self) -> None:
4841
def set_server_public_key(self, server_public_key: int) -> None:
4942
common_secret_int = pow(server_public_key, self.my_private_key,
5043
DH_PRIME_1024)
51-
common_secret = int_to_bytes(common_secret_int, 128)
44+
common_secret = common_secret_int.to_bytes(128, 'big')
5245
# HKDF with null salt, empty info and SHA-256 hash
5346
salt = b'\x00' * 0x20
5447
pseudo_random_key = hmac.new(salt, common_secret, sha256).digest()

secretstorage/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
SS_PATH,
3636
SS_PREFIX,
3737
)
38-
from secretstorage.dhcrypto import Session, int_to_bytes
38+
from secretstorage.dhcrypto import Session
3939
from secretstorage.exceptions import (
4040
ItemNotFoundException,
4141
SecretServiceNotAvailableException,
@@ -102,7 +102,7 @@ def open_session(connection: DBusConnection) -> Session:
102102
output, result = service.call(
103103
'OpenSession', 'sv',
104104
ALGORITHM_DH,
105-
('ay', int_to_bytes(session.my_public_key, 128)))
105+
('ay', session.my_public_key.to_bytes(128, 'big')))
106106
except DBusErrorResponse as resp:
107107
if resp.name != DBUS_NOT_SUPPORTED:
108108
raise

tests/test_dhcrypto.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)