2015-08-08 42 views
-5

我花了最近幾個小時嘗試獲得以下哈希算法,以將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; 
} 
+0

https://developer.apple.com/library/ma c/documentation/Darwin/Reference/ManPages/man3/CCHmac.3cc.html –

+0

用您嘗試的Objective-C代碼更新您的問題。 – rmaddy

回答

-4

解決它

-(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 = floor([[NSDate date] timeIntervalSince1970]); 
    NSString* dataString = [NSString stringWithFormat:@"session_id=%@&create_time=%i&expire_time=%i&role=%@&connection_data=%@&nonce=%f",sessionId,(int)CurrentTime,(int)(secontIn24Hour+CurrentTime),role,name,random]; 

    NSString* hash = [self hmac:dataString withKey: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 *)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; 
} 
+1

'rand()'是一個不錯的選擇,它會在每個應用程序執行時重複使用,使用'arc4random()'或'arc4random_uniform()'[手冊頁](https://developer.apple.com/library/mac/documentation /Darwin/Reference/ManPages/man3/arc4random_uniform.3.html)。 – zaph

+0

在這種情況下,它工作得很好,在字符串末尾的隨機數基本上是無用的,可能是簡單的東西作爲被寫作「吉米」爲我創造一切令牌,但我知道你的意思,arc4random會已經被使用,如果它真的很重要。 – Loxx