|
32 | 32 | import gc
|
33 | 33 | import json
|
34 | 34 | import time
|
35 |
| -import circuitpython_base64 as base64 |
36 |
| -import circuitpython_hmac as hmac |
37 | 35 | import circuitpython_parse as parse
|
38 | 36 | import adafruit_requests as requests
|
39 | 37 | import adafruit_logging as logging
|
40 | 38 | from adafruit_logging import Logger
|
41 |
| -import adafruit_hashlib as hashlib |
42 | 39 | from . import constants
|
| 40 | +from .keys import compute_derived_symmetric_key |
43 | 41 |
|
44 | 42 | # Azure HTTP error status codes
|
45 | 43 | AZURE_HTTP_ERROR_CODES = [400, 401, 404, 403, 412, 429, 500]
|
@@ -89,17 +87,6 @@ def __init__(self, socket, id_scope: str, device_id: str, key: str, logger: Logg
|
89 | 87 |
|
90 | 88 | requests.set_socket(socket)
|
91 | 89 |
|
92 |
| - @staticmethod |
93 |
| - def compute_derived_symmetric_key(secret: str, msg: str) -> bytes: |
94 |
| - """Computes a derived symmetric key from a secret and a message |
95 |
| - :param str secret: The secret to use for the key |
96 |
| - :param str msg: The message to use for the key |
97 |
| - :returns: The derived symmetric key |
98 |
| - :rtype: bytes |
99 |
| - """ |
100 |
| - secret = base64.b64decode(secret) |
101 |
| - return base64.b64encode(hmac.new(secret, msg=msg.encode("utf8"), digestmod=hashlib.sha256).digest()) |
102 |
| - |
103 | 90 | def _loop_assign(self, operation_id, headers) -> str:
|
104 | 91 | uri = "https://%s/%s/registrations/%s/operations/%s?api-version=%s" % (
|
105 | 92 | constants.DPS_END_POINT,
|
@@ -205,7 +192,7 @@ def register_device(self, expiry: int) -> str:
|
205 | 192 | """
|
206 | 193 | # pylint: disable=C0103
|
207 | 194 | sr = self._id_scope + "%2Fregistrations%2F" + self._device_id
|
208 |
| - sig_no_encode = DeviceRegistration.compute_derived_symmetric_key(self._key, sr + "\n" + str(expiry)) |
| 195 | + sig_no_encode = compute_derived_symmetric_key(self._key, sr + "\n" + str(expiry)) |
209 | 196 | sig_encoded = parse.quote(sig_no_encode, "~()*!.'")
|
210 | 197 | auth_string = "SharedAccessSignature sr=" + sr + "&sig=" + sig_encoded + "&se=" + str(expiry) + "&skn=registration"
|
211 | 198 |
|
|
0 commit comments