Created
June 25, 2011 21:03
-
-
Save jwoertink/1046894 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* Android Push API | |
| * | |
| * * * * */ | |
| import android.net.http; | |
| import org.json; | |
| import javax.crypto.*; | |
| public class Pusher { | |
| private final static String host = "api.pusherapp.com"; | |
| private final static String apiKey = ""; | |
| private final static String apiSecret = ""; | |
| private final static String appId = ""; | |
| private static URL buildURI(String uriPath, String query, String signature) { | |
| StringBuffer buffer = new StringBuffer(); | |
| //Protocol | |
| buffer.append("http://"); | |
| //Host | |
| buffer.append(host); | |
| //URI Path | |
| buffer.append(uriPath); | |
| //Query string | |
| buffer.append("?"); | |
| buffer.append(query); | |
| //Authentication signature | |
| buffer.append("&auth_signature="); | |
| buffer.append(signature); | |
| //Build URI | |
| try { | |
| return new URL(buffer.toString()); | |
| } catch (MalformedURLException e) { | |
| throw new RuntimeException("Malformed URI"); | |
| } | |
| } | |
| private static String hmacsha256Representation(String data) { | |
| try { | |
| final SecretKeySpec signingKey = new SecretKeySpec( pusherApplicationSecret.getBytes(), "HmacSHA256"); | |
| final Mac mac = Mac.getInstance("HmacSHA256"); | |
| mac.init(signingKey); | |
| byte[] digest = mac.doFinal(data.getBytes("UTF-8")); | |
| digest = mac.doFinal(data.getBytes()); | |
| BigInteger bigInteger = new BigInteger(1,digest); | |
| return String.format("%0" + (digest.length << 1) + "x", bigInteger); | |
| } catch (NoSuchAlgorithmException nsae) { | |
| throw new RuntimeException("No HMac SHA256 algorithm"); | |
| } catch (UnsupportedEncodingException e) { | |
| throw new RuntimeException("No UTF-8"); | |
| } catch (InvalidKeyException e) { | |
| throw new RuntimeException("Invalid key exception while converting to HMac SHA256"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment