Skip to content

Instantly share code, notes, and snippets.

@lfalck
Created December 1, 2019 18:18
Show Gist options
  • Save lfalck/a0294f86080a7960734afa64e2d298b1 to your computer and use it in GitHub Desktop.
Save lfalck/a0294f86080a7960734afa64e2d298b1 to your computer and use it in GitHub Desktop.
function createServiceBusOrEventHubsSASToken(resourceUri, sasKeyName, sasKey) {
if (!resourceUri || !sasKeyName || !sasKey) {
throw "Missing required parameter";
}
const encoded = encodeURIComponent(resourceUri);
const now = new Date();
const minute = 60;
const ttl = Math.round(now.getTime() / 1000) + minute;
const signature = encoded + '\n' + ttl;
const hash = CryptoJS.HmacSHA256(signature, sasKey)
.toString(CryptoJS.enc.Base64);
return 'SharedAccessSignature sr=' + encoded + '&sig=' +
encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + sasKeyName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment