Monday, February 29, 2016

Apex code example for Kayako signature

Was looking at Kayako's API documentation - don't ask me why - and noticed there's no Apex example for their API request signature scheme (why would they?), so this might be useful for people who's in the same situation as I'm.

public class KayakoAPI {

    public static string getSignature(string secretKey) {
        string signature;

        if (secretKey!=null && secretKey.length()>0) {
            Blob salt = crypto.generateAesKey(128);  //can use other ways for random string generation

            // keyed hash
            Blob hmacsha256 = crypto.generateMac('HmacSHA256', salt, Blob.valueOf(secretKey));

            // Base64 and URL encoding
            signature = EncodingUtil.urlEncode(EncodingUtil.base64Encode(hmacsha256), 'UTF-8');
        }

        return signature;
    }
}

No comments:

Post a Comment