Skip to content

HMAC support

Ricardo Minguez (Rido) edited this page Sep 9, 2020 · 3 revisions

Sign with HMAC

The official IoTHub client SDK for node uses the nodejs crypto package. This library uses the native HMAC support in modern browsers

const cryptoKey = await window.crypto.subtle.importKey(
    'raw', keyBytes, { name: 'HMAC', hash: 'SHA-256' },
    true, ['sign']

window.crypto.subtle.sign('HMAC', cryptoKey, msgBytes)

To follow the authetication guidelines the messages must be encoded to/from base 64 and serialized using in byte arrays

const keyBytes = Uint8Array.from(window.atob(key), c => c.charCodeAt(0))
const msgBytes = Uint8Array.from(msg, c => c.charCodeAt(0))
window.btoa(String.fromCharCode(...new Uint8Array(signature)))

completed in #25 Support HMAC signatures

Clone this wiki locally