Подтвердить что ты не робот

Создание SHA256 в iphone/Objective C...?

Как создать SHA256 строки в iphone/объекте c...

Sha256 в Objective-C для iPhone

Я прочитал это... но я не могу этого понять.

Я хочу создать вывод, подобный функциям php, следующим образом: -

$hash = hash_hmac("sha256", implode(';', $hash_parameters), $api_key);

где хэш-параметры - это массив аргументов...

Можете ли вы написать это как метод, который примет строку ввода...?

А какой будет выход метода NSData или NSString..??

Мне нужно создать запрос с этим..??

Итак, в объекте запроса..

[theRequest setHTTPBody:requestBody];

Каким должен быть тип requestBody?

4b9b3361

Ответ 1

Я не уверен, что полностью понимаю ваши вопросы, но если вы хотите создать хэшированную строку, вы можете передать свои параметры в качестве аргументов хэш-функции.

-(void)generateHashedString {

    NSString *key = @"Some random string";
    //enter your objects you want to encode in the data object
    NSString *data = [NSString stringWithFormat:@"%@%@%@", @"sha256", hash_parameters, api_key];

    const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];

    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
                                          length:sizeof(cHMAC)];

    NSString *hash = [HMAC base64Encoding];

}

Это даст вам NSString хэша, который вы можете использовать для выполнения своих запросов. NSLog(@"%@",hash); Чтобы узнать, что вы создали,

Удостоверьтесь, что вы #import <CommonCrypto/CommonHMAC.h> тоже

Ответ 2

Я не сравнивал следующий код с выходом функции PHP, но он работает для меня:

+(NSString *)signWithKey:(NSString *)key usingData:(NSString *)data
{   
    const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];

    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];

    return [[HMAC.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
}

Сообщите мне, было ли это полезно...

Ответ 3

Я провел день дыры, пытаясь преобразовать сгенерированный хэш (байты) в читаемые данные. Я использовал base64, закодированный как ответ выше, и он не работал вообще для меня (b.t.w. вам нужно и внешний .h, чтобы иметь возможность использовать кодировку base64, которая у меня была).

Так что я сделал это (что отлично работает без внешнего .h):

CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:cHMAC length:CC_SHA256_DIGEST_LENGTH];

// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
// hash is now a string with just the 40char hash value in it
NSLog(@"%@",hash);

Ответ 4

Для справки это хеширование HMac будет работать на PHP.

- (NSString *)getToken:(NSString *)queryString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [formatter stringFromDate:[NSDate date]];
    NSDate *dateTodayUTC = [formatter dateFromString:dateString];
    NSString *nowTimestamp = [NSString stringWithFormat:@"%.f", [dateTodayUTC timeIntervalSince1970]];

    NSString *hashCombinations = [[NSString alloc] initWithFormat:@"%@%@%.f", queryString, public_api_key, [dateTodayUTC timeIntervalSince1970]];

    const char *privateKey  = [private_api_key cStringUsingEncoding:NSUTF8StringEncoding];
    const char *requestData = [hashCombinations cStringUsingEncoding:NSUTF8StringEncoding];
    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

    //HmacSHA256
    CCHmac(kCCHmacAlgSHA256, // algorithm
           privateKey, strlen(privateKey), // privateKey
           requestData, strlen(requestData), // requestData
           cHMAC); // length

    NSString *hash;
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", cHMAC[i]];
    hash = output;

    NSString *base64HashString = [self base64String:hash];
    self.tokenLabel.text = hash;

    NSLog(@"generated hash = %@", hash);
    NSLog(@"base64 hash = %@", base64HashString);
    NSLog(@"timestamp = %@ nsdate utc = %@", nowTimestamp, dateString);
    NSLog(@"combinations %@", hashCombinations);
    return [base64HashString urlencode];
}

Вы можете использовать этот метод base64.

- (NSString *)base64String:(NSString *)str
{
    NSData *theData = [str dataUsingEncoding: NSASCIIStringEncoding];
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

Ответ 5

Я думаю, что это более компактное решение:

#import <CommonCrypto/CommonCrypto.h>

...

-(NSData*)Sha256WithKey:(NSData*)key andData:(NSData*)data{
     NSMutableData* result = [NSMutableData 
                               dataWithCapacity:CC_SHA256_DIGEST_LENGTH];

     CCHmac(kCCHmacAlgSHA256, [key bytes], [key length],
            [data bytes], [data length], result.mutableBytes);

     return result;
 }
 ....