Ingestion service problem : Failed to upload file to Edge Impulse 400 b'Invalid value for protected.alg - only "none" and "HS256" supported'

Hello when i run the “test” python code below I get the error below

Failed to upload file to Edge Impulse 400 b’Invalid value for protected.alg - only “none” and “HS256” supported’

If i try and upload the exported file which looks like the below :

‘{“protected”: {“ver”: “v1”, “alg”: null}, “signature”: “my code”, “payload”: {“device_name”: “robot_welder”, “device_type”: “robot_welder”, “interval_ms”: 1000, “sensors”: [{“name”: “ApparentPower”, “units”: “KWH”}], “values”: [[1]]}}’

I get :

image

Code :

ver = “v1” # always v1 (required).
alg = None # the algorithm used to sign this file. Either HS256 (HMAC-SHA256) or none (required).
iat = None # date when the file was created in seconds since epoch. Only set this when the device creating the file has an accurate clock (optional).

#signature.
signature = "emptySignature" #ryptographic signature for this file (see signing data) (required).

#payload.
device_type = "bigmachine" #device_type - device type, for example the exact model of the device. Should be the same for all similar devices (required).
device_name = "machine01" #device_name - unique identifier for this device. Only set this when the device has a globally unique identifier (e.g. MAC address). If this field is set the device shows up on the 'Devices' page in the studio (optional).
interval_ms = 1000 #interval_ms - the frequency of the data in this file (in milliseconds). E.g. for 100Hz fill in 10 (new data every 10 ms.). You can use a float here if you need the precision (required).
sensors =  [
            { "name": "ApparentPower", "units": "KWH" },
        ]
values =[[1]]

HMAC_KEY = "my key "
API_KEY = "my api key "

empty signature (all zeros). HS256 gives 32 byte signature, and we encode in hex, so we need 64 characters here

emptySignature = ‘’.join([‘0’] * 64)

data = {
“protected”: {
“ver”: ver,
“alg”: None,
“iat”: 1564128599
},
“signature”: signature,
“payload”: {
“device_name”: device_name,
“device_type”: device_type,
“interval_ms”: interval_ms,
“sensors”: sensors,
“values”: values
}
}

encode in JSON

encoded = json.dumps(data)

sign message

signature = hmac.new(bytes(HMAC_KEY, ‘utf-8’), msg = encoded.encode(‘utf-8’), digestmod = hashlib.sha256).hexdigest()

set the signature again in the message, and encode again

data[‘signature’] = signature
encoded = json.dumps(data)

and upload the file

res = requests.post(url=‘https://ingestion.edgeimpulse.com/api/training/data’,
data=encoded,
headers={
‘Content-Type’: ‘application/json’,
‘x-file-name’: ‘idle01’,
‘x-api-key’: API_KEY
})
if (res.status_code == 200):
print(‘Uploaded file to Edge Impulse’, res.status_code, res.content)
else:
print(‘Failed to upload file to Edge Impulse’, res.status_code, res.content)

Hi @kelter101757,

If you don’t use HS256 to sign the file, you need to set "alg: "none" in the data payload (using quotes).

Aurelien

1 Like

Got it, thanks. for other people readings, changed code to :

data = {
“protected”: {
“ver”: ver,
“alg”: “none”,
“iat”: “none”
},
“signature”: signature,
“payload”: {
“device_name”: device_name,
“device_type”: device_type,
“interval_ms”: interval_ms,
“sensors”: sensors,
“values”: values
}
}

2 Likes