Skip to content

Instantly share code, notes, and snippets.

@tomball
Created May 21, 2021 19:18
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 tomball/26cf057bd808add837a82bb790c05e3a to your computer and use it in GitHub Desktop.
Save tomball/26cf057bd808add837a82bb790c05e3a to your computer and use it in GitHub Desktop.
Java/j2objc example of iOS CommonCrypto's CCHmac function
/*-[
#import <CommonCrypto/CommonHMAC.h>
]-*/;
class HashTest {
private static native String hash(String key, String data) /*-[
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *out = [NSData dataWithBytes:cHMAC length:CC_SHA256_DIGEST_LENGTH];
return [out base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
]-*/;
public static void main(String... args) throws Exception {
System.out.println(hash("mykey", "my_message"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment