//sha256校驗(yàn)
- (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;
}
根據(jù)跟后臺(tái)的約定烫罩,此時(shí)需要傳入的參數(shù)是json字符串類型宜岛,由于我們一般參數(shù)都存在字典里奈虾,需要做一個(gè)字典轉(zhuǎn)json字符串
//把字典轉(zhuǎn)為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;
}