Skip to content

Instantly share code, notes, and snippets.

@flukeout
Created November 20, 2009 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flukeout/239757 to your computer and use it in GitHub Desktop.
Save flukeout/239757 to your computer and use it in GitHub Desktop.
Zeep Mobile - Generating the authentication header - Java
// Created by Simon Wex (simon@zeepmobile.com) on 2008-07-12
// Copyright (c) 2008. All rights reserved.
// Released under MIT License
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class HmacSha1 {
public static final String API_KEY = "cef7a046258082993759bade995b3ae8";
public static final String SECRET_ACCESS_KEY = "19c87eb3e3a28404e7ea8197d4401540";
public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
/**
* @param args
*/
public static void main(String[] args) {
try {
SimpleDateFormat httpDateFormat = new SimpleDateFormat();
httpDateFormat.applyPattern(PATTERN_RFC1123);
httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String httpDate = httpDateFormat.format(new Date());
// (ex. Sat, 12 Jul 2008 09:04:28 GMT) http_date = Time.now.httpdate
String body = URLEncoder.encode("Art thou not Romeo, and a Montague?", "UTF-8");
//user_id=1234&body=Art+thou+not+Romeo%2C+and+a+Montague%3F
String parameters = "user_id=1234&body=" + body;
String canonicalString = API_KEY + httpDate + parameters;
System.out.println(canonicalString);
Mac mac = Mac.getInstance("HmacSHA1");
byte[] keyBytes = SECRET_ACCESS_KEY.getBytes("UTF8");
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
mac.init(signingKey);
byte[] signBytes = mac.doFinal(canonicalString.getBytes("UTF8"));
String b64Mac = Base64.encode(signBytes);
String authentication = "Zeep " + API_KEY + ":" + b64Mac;
System.out.println(authentication);
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment