Skip to content

Instantly share code, notes, and snippets.

@CIPop
Created April 15, 2021 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CIPop/b719c12798461d5fad614b0148f6243a to your computer and use it in GitHub Desktop.
Save CIPop/b719c12798461d5fad614b0148f6243a to your computer and use it in GitHub Desktop.
Azure IoT SAS Enrollment Group
using System;
using System.Security.Cryptography;
using System.Text;
namespace dps_group_sas
{
class Program
{
public static string ComputeDerivedSymmetricKey(byte[] masterKey, string registrationId)
{
using (var hmac = new HMACSHA256(masterKey))
{
return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(registrationId)));
}
}
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("dps_group_sas <masterKey> <registrationID>");
return;
}
Console.WriteLine(ComputeDerivedSymmetricKey(Convert.FromBase64String(args[0]), args[1]));
}
}
}
@CIPop
Copy link
Author

CIPop commented Apr 15, 2021

To deploy:

  1. Install the .NET Core SDK (supported on Windows, Linux, OSX) from here: https://dotnet.microsoft.com/download
  2. Create a new folder (e.g. dps_group_sas)
  3. Run dotnet new within the folder.
  4. Download Program.cs and replace it within the same folder

To create a device identity run

dotnet run "SharedAccessKey_From_Group_Enrollment" device-registration-id

E.g.

dotnet run "8abcdefghz.....==" my-sasgroup1-device1

where the first quoted string is one of the two keys available in the Provisioning portal:
image

The output can be used as the shared access key for DPS authentication and, after provisioning, for IoT Hub operations.

References:

https://docs.microsoft.com/azure/iot-dps/how-to-legacy-device-symm-key?tabs=linux
https://docs.microsoft.com/azure/iot-dps/concepts-symmetric-key-attestation#group-enrollments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment