美文网首页
SHA256校验

SHA256校验

作者: 琪琪_729b | 来源:发表于2019-10-08 09:46 被阅读0次

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

    }

    相关文章

      网友评论

          本文标题:SHA256校验

          本文链接:https://www.haomeiwen.com/subject/zkisyctx.html