Created
April 17, 2014 04:04
-
-
Save iambigd/10952096 to your computer and use it in GitHub Desktop.
Generating an API Signature - Kayako Developer Resources - Kayako Wiki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
import java.security.GeneralSecurityException; | |
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
import org.bouncycastle.util.encoders.Base64Encoder; | |
public class Kayakoapi { | |
public static void main(String[] args) throws GeneralSecurityException, IOException { | |
String secretKey = "secretKey"; | |
String salt = "0123456789"; | |
String generateHmacSHA256Signature = generateHmacSHA256Signature(salt, secretKey); | |
System.out.println("Signature: " + generateHmacSHA256Signature); | |
String urlEncodedSign = URLEncoder.encode(generateHmacSHA256Signature, "UTF-8"); | |
System.out.println("Url encoded value: " + urlEncodedSign); | |
} | |
public static String generateHmacSHA256Signature(String data, String key) throws GeneralSecurityException { | |
byte[] hmacData = null; | |
try { | |
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"); | |
Mac mac = Mac.getInstance("HmacSHA256"); | |
mac.init(secretKey); | |
hmacData = mac.doFinal(data.getBytes("UTF-8")); | |
return new Base64Encoder().encode(hmacData); | |
} catch (UnsupportedEncodingException e) { | |
throw new GeneralSecurityException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment