【发布时间】:2012-07-28 05:35:30
【问题描述】:
更具体地说,我在代码中放置了一个断点
NSString *signStr = [NSString stringWithCString:text encoding:NSASCIIStringEncoding];
我用secret= @"12345678901234567890" 和movingFactor = 1 调用函数
我注意到在断点处,signStr 是一个空字符串,
代码中还有其他明显的错误吗?
谢谢
-(NSString*) generateOTP:(NSString*) secret with:(uint64_t) movingFactor
{
NSString* result;
const int DIGITS = 6;
const int SIX_DIGITS = 1000000;
char text[8];
for (int i = 7; i >= 0; i--) {
text[i] = movingFactor & 0xff;
movingFactor >>= 8;
}
NSString *signStr = [NSString stringWithCString:text encoding:NSASCIIStringEncoding];
NSString* hash = [self sha1UseKey:secret toSign:signStr];
int offset = [hash characterAtIndex:[hash length] - 1] & 0xf;
int binary = (([hash characterAtIndex:offset] & 0x7f) << 24) |
(([hash characterAtIndex:offset+1] & 0xff) << 16) |
(([hash characterAtIndex:offset+2] & 0xff) << 8) | ([hash characterAtIndex:offset+3] & 0xff);
int otp = binary % SIX_DIGITS;
result = [NSString stringWithFormat:@"%d", otp];
NSString* zero = @"0";
while ([result length] < DIGITS) {
result = [zero stringByAppendingString:result];
}
return result;
}
【问题讨论】:
标签: objective-c arrays nsstring char