Converting hashing algorithm from javascript to IOS

| | August 11, 2015

I’ve spent the last couple of hours trying to get the following hashing algorithm to convert from JavaScript to Objective-C and I just can’t seem to get it to work. Any sort of help would be greatly appreciated, thanks a billion. Here’s the JavaScript:

<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>

I’ve messed around with this, but it’s not working as I expected, it’s from here (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;
}

this is my code:

-(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;
}

One Response to “Converting hashing algorithm from javascript to IOS”

  1. solved it

    -(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;
    }
    

Leave a Reply