-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhmac.js
More file actions
19 lines (17 loc) · 750 Bytes
/
hmac.js
File metadata and controls
19 lines (17 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import crypto from 'crypto'
export const createHmac = async (key, msg) => {
const hmac = crypto.createHmac('sha256', Buffer.from(key, 'base64'))
hmac.update(msg)
return hmac.digest('base64')
}
export async function generateSasToken (resourceUri, signingKey, policyName, expiresInMins) {
resourceUri = encodeURIComponent(resourceUri)
let expires = (Date.now() / 1000) + expiresInMins * 60
expires = Math.ceil(expires)
const toSign = resourceUri + '\n' + expires
const hmac = await createHmac(signingKey, toSign)
const base64UriEncoded = encodeURIComponent(hmac)
let token = 'SharedAccessSignature sr=' + resourceUri + '&sig=' + base64UriEncoded + '&se=' + expires
if (policyName) token += '&skn=' + policyName
return token
}