[英]xcode ios HMAC SHA 256 hashing
So I'm trying to figure out how to do a hmacshad256 hash on ios as that's the hash I did for the wcf service api I made. 所以我想弄清楚如何在ios上做一个hmacshad256哈希,因为这是我为我所做的wcf服务api做的哈希。 I've been trying to look for some info about it but would usually just end up getting a SHA-256 hash.
我一直在尝试寻找有关它的一些信息,但通常最终会得到一个SHA-256哈希。
This is the only reference I have: 这是我唯一的参考:
Need to generate HMAC SHA256 hash in Objective C as in Java 需要像Java一样在Objective C中生成HMAC SHA256哈希
And I'm not sure if that's the only way to do it (importing a java hmac class) 而且我不确定这是否是唯一的方法(导入java hmac类)
Any help is appreciated. 任何帮助表示赞赏。
Thanks! 谢谢!
NSString * parameters = @"string to hash"
NSString *salt = @"saltStringHere";
NSData *saltData = [salt dataUsingEncoding:NSUTF8StringEncoding];
NSData *paramData = [parameters dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData* hash = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH ];
CCHmac(kCCHmacAlgSHA256, saltData.bytes, saltData.length, paramData.bytes, paramData.length, hash.mutableBytes);
NSString *base64Hash = [hash base64Encoding];
and also 并且
#import <CommonCrypto/CommonHMAC.h>
Since base64Encoding
is deprecated from iOS 7.0, the last line should be: 由于从iOS 7.0 弃用了
base64Encoding
,最后一行应该是:
NSString *base64Hash = [hash base64EncodedStringWithOptions:0];
Here is the solution I'm submitting that I put together from other answers on the matter: 这是我提交的解决方案,我从其他答案中汇总了这个问题:
This is easily adapted to other hash types by changing CC_SHA256_DIGEST_LENGTH and kCCHmacAlgSHA256. 通过更改CC_SHA256_DIGEST_LENGTH和kCCHmacAlgSHA256,可以轻松地将其调整为其他哈希类型。
If you're interested in doing that, check out the CommonDigest.h file within the CommonCrypto library. 如果您对此感兴趣,请查看CommonCrypto库中的CommonDigest.h文件。
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
+ (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 dataWithBytes:cHMAC length:sizeof(cHMAC)];
const unsigned char *buffer = (const unsigned char *)[HMACData bytes];
NSMutableString *HMAC = [NSMutableString stringWithCapacity:HMACData.length * 2];
for (int i = 0; i < HMACData.length; ++i){
[HMAC appendFormat:@"%02x", buffer[i]];
}
return HMAC;
}
This has been tested on iOS 8.x and iOS 7.x 这已经在iOS 8.x和iOS 7.x上进行了测试
+ (NSString *)hmacSHA256EncryptString{ NSString * parameterSecret = @"input secret key"; NSString *plainString = @"input encrypt content string"; const char *secretKey = [parameterSecret cStringUsingEncoding:NSUTF8StringEncoding]; const char *plainData = [plainString cStringUsingEncoding:NSUTF8StringEncoding]; unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA256, secretKey, strlen(secretKey), plainData, strlen(plainData), cHMAC); NSData *HMACData = [NSData dataWithBytes:cHMAC length:sizeof(cHMAC)]; const unsigned char *bufferChar = (const unsigned char *)[HMACData bytes]; NSMutableString *hmacString = [NSMutableString stringWithCapacity:HMACData.length * 2]; for (int i = 0; i < HMACData.length; ++i){ [hmacString appendFormat:@"%02x", bufferChar[i]]; } return hmacString; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.