Created
May 21, 2021 19:18
-
-
Save tomball/26cf057bd808add837a82bb790c05e3a to your computer and use it in GitHub Desktop.
Java/j2objc example of iOS CommonCrypto's CCHmac function
This file contains 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
/*-[ | |
#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