Skip to content

Instantly share code, notes, and snippets.

@adambair
Created December 11, 2008 20:12
Show Gist options
  • Save adambair/34854 to your computer and use it in GitHub Desktop.
Save adambair/34854 to your computer and use it in GitHub Desktop.
Needed to convert this java method to Objective-C. It turnes out that it's only doing starndard HmacSHA1. Don't ask how long it took me to figure it out.
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public static String someHashMethod(String data, String hash) {
byte[] hashBytes = hash.getBytes();
SecretKeySpec signKey = new SecretKeySpec(hashBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
BigInteger number = new BigInteger(1, rawHmac);
String returnHash = number.toString(16);
if (returnHash.length() == 39) {
returnHash = "0" + returnHash;
}
return returnHash;
}
#import "GHNSString+HMAC.h"
[payload gh_hmacSha1:hashKey];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment