Skip to content

Instantly share code, notes, and snippets.



Created Dec 31, 2018
What would you like to do?
import java.math.BigInteger;
import java.util.Map;
import java.util.HashMap;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Urlbox {
private String key;
private String secret;
Urlbox(String api_key, String api_secret) {
this.key = api_key;
this.secret = api_secret;
// main method demos Example Usage
public static void main(String[] args) {
String urlboxKey = "your-urlbox-api-key";
String urlboxSecret = "your-urlbox-secret";
// Set request options
Map<String, Object> options = new HashMap<String, Object>();
options.put("width", 1280);
options.put("height", 1024);
options.put("thumb_width", 240);
options.put("full_page", "false");
options.put("force", "false");
// Create urlbox object with api key and secret
Urlbox urlbox = new Urlbox(urlboxKey, urlboxSecret);
try {
// Call generateUrl function of urlbox object
String urlboxUrl = urlbox.generateUrl("", options);
// Now do something with urlboxUrl.. put in img tag, etc..
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Problem with url encoding", ex);
public String generateUrl(String url, Map<String,Object> options) throws UnsupportedEncodingException {
String encodedUrl = URLEncoder.encode(url, "UTF-8");
String queryString = String.format("url=%s", encodedUrl);
for (Map.Entry<String, Object> entry : options.entrySet()) {
String queryParam = "&"+entry.getKey()+"="+entry.getValue();
queryString += queryParam;
String token = generateToken(queryString, this.secret);
String result = String.format("", this.key, token, queryString);
return result;
private String generateToken(String input, String key) {
String lSignature = "None";
try {
Mac lMac = Mac.getInstance("HmacSHA1");
SecretKeySpec lSecret = new SecretKeySpec(key.getBytes(), "HmacSHA1");
byte[] lDigest = lMac.doFinal(input.getBytes());
BigInteger lHash = new BigInteger(1, lDigest);
lSignature = lHash.toString(16);
if ((lSignature.length() % 2) != 0) {
lSignature = "0" + lSignature;
} catch (NoSuchAlgorithmException lEx) {
throw new RuntimeException("Problems calculating HMAC", lEx);
} catch (InvalidKeyException lEx) {
throw new RuntimeException("Problems calculating HMAC", lEx);
return lSignature;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.