我需要在Objective C中生成HMAC-SHA1.但是我找不到任何有用的东西.我尝试使用CCHMAC使用CommonCrypto,但没有用.我需要生成一个hmac并在生成HOTP号后.
有人在Objective C或C中有任何示例代码吗?
以下是使用SHA-256生成HMAC的方法:
NSString *key; NSString *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 *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)]; NSString *hash = [HMAC base64Encoding];
我不知道HOTP库,但如果我没记错的话,算法非常简单.
以下是如何生成HMAC-SHA1 base64的方法.
您需要将Base64.h和Base64.m添加到项目中.你可以从这里得到它.
如果您使用ARC,它将在Base64.m中显示一些错误.找到类似这样的行
return [[[self alloc] initWithBase64String:base64String] autorelease];
你需要的是删除自动释放部分.最终结果应如下所示:
return [[self alloc] initWithBase64String:base64String];
现在在您的常规项目中导入"Base64.h"和以下代码
#import "Base64.h" #include#include - (NSString *)hmacsha1:(NSString *)data secret:(NSString *)key { const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding]; unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC); NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)]; NSString *hash = [HMAC base64String]; return hash; }
同
NSLog(@"Hash: %@", hash);
你会得到类似的东西:
ghVEjPvxwLN1lBi0Jh46VpIchOc=
这是完整的解决方案,无需任何额外的库或黑客即可运行:
+(NSString *)hmac:(NSString *)plainText withKey:(NSString *)key { const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; const char *cData = [plainText cStringUsingEncoding:NSASCIIStringEncoding]; unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC); NSData *HMACData = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)]; const unsigned char *buffer = (const unsigned char *)[HMACData bytes]; NSString *HMAC = [NSMutableString stringWithCapacity:HMACData.length * 2]; for (int i = 0; i < HMACData.length; ++i) HMAC = [HMAC stringByAppendingFormat:@"%02lx", (unsigned long)buffer[i]]; return HMAC; }
您不必包含任何已编码的第三方base64库.
这可以在不使用自定义协议的情况下工作,使用http://cocoawithlove.com/2009/07/hashvalue-object-for-holding-md5-and.html中的一些代码
HashSHA256.h
#import#import @interface HashSHA256 : NSObject { } - (NSString *) hashedValue :(NSString *) key andData: (NSString *) data ; @end
HashSHA256.m
#import "HashSHA256.h" #import@implementation HashSHA256 - (NSString *) hashedValue :(NSString *) key andData: (NSString *) data { const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding]; const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding]; unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC); NSString *hash; NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2]; for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) [output appendFormat:@"%02x", cHMAC[i]]; hash = output; return hash; } @end
用法:
- (NSString *) encodePassword: (NSString *) myPassword { HashSHA256 * hashSHA256 = [[HashSHA256 alloc] init]; NSString * result = [hashSHA256 hashedValue:mySecretSalt andData:myPassword]; return result; }