Skip to content

Commit 799dc5d

Browse files
author
Jim Bennett
committed
Adding HMAC in code instead of a library
Doing this removes a dependency on Adafruit_hashlib saving 18K in memory usage
1 parent f3ff110 commit 799dc5d

File tree

6 files changed

+571
-19
lines changed

6 files changed

+571
-19
lines changed

README.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ This driver depends on:
4747
* `Adafruit CircuitPython MiniMQTT <https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT>`_
4848

4949
* `CircuitPython Base64 <https://github.com/jimbobbennett/CircuitPython_Base64>`_
50-
* `CircuitPython HMAC <https://github.com/jimbobbennett/CircuitPython_HMAC>`_
5150
* `CircuitPython Parse <https://github.com/jimbobbennett/CircuitPython_Parse>`_
5251

5352
Please ensure all dependencies are available on the CircuitPython filesystem.

adafruit_azureiot/device_registration.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@
3232
import gc
3333
import json
3434
import time
35-
import circuitpython_base64 as base64
36-
import circuitpython_hmac as hmac
3735
import circuitpython_parse as parse
3836
import adafruit_requests as requests
3937
import adafruit_logging as logging
4038
from adafruit_logging import Logger
41-
import adafruit_hashlib as hashlib
4239
from . import constants
40+
from .keys import compute_derived_symmetric_key
4341

4442
# Azure HTTP error status codes
4543
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
8987

9088
requests.set_socket(socket)
9189

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-
10390
def _loop_assign(self, operation_id, headers) -> str:
10491
uri = "https://%s/%s/registrations/%s/operations/%s?api-version=%s" % (
10592
constants.DPS_END_POINT,
@@ -205,7 +192,7 @@ def register_device(self, expiry: int) -> str:
205192
"""
206193
# pylint: disable=C0103
207194
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))
209196
sig_encoded = parse.quote(sig_no_encode, "~()*!.'")
210197
auth_string = "SharedAccessSignature sr=" + sr + "&sig=" + sig_encoded + "&se=" + str(expiry) + "&skn=registration"
211198

0 commit comments

Comments
 (0)