将NSString强制转换为无符号字符* [英] Casting NSString to unsigned char *

查看:56
本文介绍了将NSString强制转换为无符号字符*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用具有以下签名的函数来签名HTTP请求:

I'm trying to use a function that has the following signature to sign a HTTP request:

extern void hmac_sha1(const unsigned char *inText, int inTextLength, unsigned char* inKey, const unsigned int inKeyLength, unsigned char *outDigest);

这是我编写的使用方法:

And this is the method I wrote to use it:

- (NSString *)sign: (NSString *)stringToSign {
    NSString *secretKey = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    const unsigned char *inText = (unsigned char *)[stringToSign UTF8String];
    int inTextLength = [stringToSign length];
    unsigned char *inKey = (unsigned char *)[secretKey UTF8String];
    const unsigned int inKeyLength = (unsigned int)[secretKey length];
    unsigned char *outDigest;

    hmac_sha1(inText, inTextLength, inKey, inKeyLength, outDigest);
    NSString *output = [NSString stringWithUTF8String:(const char *)outDigest];

    return output;
}

问题是我确定这不是我应该执行的转换方式,因为在此hmac_sha1函数中,我得到了EXC_BAD_ACCESS异常.

The problem is I'm sure this is not the way I'm supposed to do this casting, as inside this hmac_sha1 function I get a EXC_BAD_ACCESS exception.

由于我是Objective-C的新手,几乎没有C的经验(惊讶!),我真的不知道要搜索什么.关于如何开始解决此问题的任何提示?

Since I am new to Objective-C and have close to no experience in C (surprise!) I don't really know what to search for. Any tips on how I can start solving this?

提前谢谢!

顺便说一句,我获得了此功能的参考

BTW, I got the reference for this function here in stackoverflow.

推荐答案

问题似乎不在于转换,而在于outDigest. hmac_sha1的第五个参数应该指向大小为20个字节的已分配缓冲区(我认为).

It looks like the problem is not with the casting, but with outDigest. The fifth argument to hmac_sha1 should point to an already allocated buffer of size 20 bytes (I think).

如果您更改显示以下内容的行

If you change the line that says

unsigned char *outDigest;

#define HMACSHA1_DIGEST_SIZE 20
void *outDigest = malloc(HMACSHA1_DIGEST_SIZE);

这应该使您摆脱hmac_sha1内部的崩溃.

That should get you past the crash inside hmac_sha1.

然后您就遇到了将outDigest处的数据转换为NSString的问题.看来hmac_sha1会将20个字节的随机查找数据放在outDigest处,而不是以null结尾的UTF-8字符串,因此stringWithUTF8String:将不起作用.如果您必须返回NSString:

Then you've got the problem of converting the data at outDigest into an NSString. It looks like hmac_sha1 will put 20 bytes of random-looking data at outDigest, and not a null terminated UTF-8 string, so stringWithUTF8String: won't work. You might want to use something like this instead if you have to return an NSString:

NSString *output = [[NSString alloc] initWithBytesNoCopy:outDigest
                                     length:HMACSHA1_DIGEST_SIZE
                                     encoding:NSASCIIStringEncoding
                                     freeWhenDone:YES];

我认为NSString确实不是摘要的正确类型,因此如果可能的话,可能值得更改您的方法以返回NSData.

I don't think NSString is really the right type for the digest, so it might be worth changing your method to return an NSData if you can.

这篇关于将NSString强制转换为无符号字符*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆