SHA256校验

2019-10-08  本文已影响0人  琪琪_729b

//sha256校验

- (NSString*)SHA256:(NSString*)string

{

    const char *s = [string cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];

    uint8_t digest[CC_SHA256_DIGEST_LENGTH] = {0};

    CC_SHA256(keyData.bytes, (CC_LONG)keyData.length, digest);

    NSData *out = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];

    NSString *hash = [out description];

    hash = [hashstringByReplacingOccurrencesOfString:@" " withString:@""];

    hash = [hashstringByReplacingOccurrencesOfString:@"<" withString:@""];

    hash = [hashstringByReplacingOccurrencesOfString:@">" withString:@""];

    returnhash;

}

根据跟后台的约定,此时需要传入的参数是json字符串类型,由于我们一般参数都存在字典里,需要做一个字典转json字符串

//把字典转为json格式字符串

- (NSString*)gs_jsonStringCompactFormatForDictionary:(NSDictionary*)dicJson {

    if (![dicJson isKindOfClass:[NSDictionary class]] || ![NSJSONSerialization isValidJSONObject:dicJson]) {

        returnnil;

    }

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];

    NSString *strJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    returnstrJson;

}

上一篇下一篇

猜你喜欢

热点阅读