我花了最近幾個小時嘗試獲得以下哈希算法,以將JavaScript從JavaScript轉換爲Objective-C,而我似乎無法使其工作。任何形式的幫助將不勝感激,謝謝十億。這是JavaScript。下面令牌創建算法是OpenTok TokBox,你可以在這裏找到:將哈希算法從javascript轉換爲IOS
http://www.tokbox.com/blog/generating-tokens-without-server-side-sdk/
我需要將此轉換爲Objective-C代碼,這樣我可以創建令牌和會議客戶端,謝謝!
<script src="http://code.jquery.com/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="https://raw.github.com/carlo/jquery-base64/master/jquery.base64.min.js" type="text/javascript"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha1.js"></script>
<script type="text/javascript">
var secondsInDay = 86400;
// Credentials
var apiKey = '<apiKey>';
var secret = '<secret>';
var sessionId = '1_MX4xMTMzMTg5Mn4xMjcuMC4wLjF-U3VuIERlYyAwMiAwNjo0NTozMSBQU1QgMjAxMn4wLjQwMjY2OH4';
// Token Params
var timeNow = Math.floor(Date.now()/1000);
var expire = timeNow+secondsInDay;
var role = "publisher";
var data = "bob";
TB.setLogLevel(TB.DEBUG);
// Calculation
data = escape(data);
var rand = Math.floor(Math.random()*999999);
var dataString = "session_id="+sessionId+"&create_time="+timeNow+"&expire_time="+expire+"&role="+role+"&connection_data="+data+"&nonce="+rand;
// Encryption
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA1, secret);
hmac.update(dataString);
hash = hmac.finalize();
preCoded = "partner_id="+apiKey+"&sig="+hash+":"+dataString;
token = "T1=="+$.base64.encode(preCoded)
</script>
我搞砸圍繞這一點,但它不工作如我所料,它是從這裏(Objective-C sample code for HMAC-SHA1):
-(NSString *)hmac:(NSString *)plaintext withKey:(NSString *)key
{
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [plaintext cStringUsingEncoding:NSASCIIStringEncoding];
uint8_t cHMAC[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSString *Hash1 = @"";
for (int i=0; i< CC_SHA1_DIGEST_LENGTH; i++)
{
Hash1 = [Hash1 stringByAppendingString:[NSString stringWithFormat:@"%02X", cHMAC[i]]];
}
return Hash1;
}
這是我的代碼:
-(void)CreateToken {
NSString* apiKey = @"KEY";
NSString* secret = @"SECRET";
NSString* sessionId = @"ID";
NSString* role = @"Publisher";
NSString* name = @"bob";
double random = floor(rand()*999999);
double secontIn24Hour = 86400;
double CurrentTime = CACurrentMediaTime();
NSString* dataString = [NSString stringWithFormat:@"session_id=%@&create_time=%f&expire_time=%f&role=%@&connection_data=%@&nonce=%f",sessionId,CurrentTime,(secontIn24Hour+CurrentTime),role,name,random];
NSString* hash = [self hmacsha1:dataString secret:secret];
NSString* preCoded = [NSString stringWithFormat:@"partner_id=%@&sig=%@:%@",apiKey,hash,dataString];
NSData *data = [preCoded dataUsingEncoding:NSUTF8StringEncoding];
NSString* token = [NSString stringWithFormat:@"T1==%@",[data base64EncodedStringWithOptions:0]];
NSLog(@"token %@", token);
}
- (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 base64EncodedStringWithOptions:0];
return hash;
}
https://developer.apple.com/library/ma c/documentation/Darwin/Reference/ManPages/man3/CCHmac.3cc.html –
用您嘗試的Objective-C代碼更新您的問題。 – rmaddy