0

I'm creating a Kotlin app the requires as SAS Token creating which needs HMAC authentication.

The Microsoft website includes various code examples but not a Kotlin example:

https://learn.microsoft.com/en-us/rest/api/eventhub/generate-sas-token

Having researched this further the following example looked promising but the post is very old:

Is there any function for creating Hmac256 string in android?

There's a dependency that needs adding, following which is no longer supported in Android Studio, I wrongly assumed replacing compile with implementation would solve the issue:

compile 'org.apache.directory.studio:org.apache.commons.codec:1.8'

So in short I'm struggling a little, I've setup Postman with a Pre request script and this is generating the required key and works, just need to find a way to migrate this to Kotlin.

The key line of code that I would like some assistance on is:

const hash = CryptoJS.HmacSHA256(signature, saKey).toString(CryptoJS.enc.Base64)

The entire script:

function createSharedAccessToken(uri, saName, saKey) { 
    if (!uri || !saName || !saKey) { 
            throw "Missing required parameter"; 
        } 
    var encoded = encodeURIComponent(uri); 
    var now = new Date(); 
    var week = 60*60*24*7;
    var ttl = Math.round(now.getTime() / 1000) + week;
    var signature = encoded + '\n' + ttl;
    const hash = CryptoJS.HmacSHA256(signature, saKey).toString(CryptoJS.enc.Base64)

    return 'SharedAccessSignature sr=' + encoded + '&sig=' +  
    encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName; 

}

Thanks for any assistance or links that may help resolve my problem.

Duncan Hill
  • 547
  • 2
  • 5
  • 16

0 Answers0