美文网首页
ios md加密

ios md加密

作者: 飞鱼_T | 来源:发表于2016-04-29 15:55 被阅读37次

    #import<CommonCrypto/CommonDigest.h>

    -(NSString *) md5:(NSString *)str

    {

    const char *cStr = [str UTF8String];

    unsigned char digest[CC_MD5_DIGEST_LENGTH];

    CC_MD5( cStr, (unsigned int)strlen(cStr), digest );

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)

    [output appendFormat:@"%02X", digest[i]];

    //    16位需要这样截取

    //    output = (NSMutableString*)[output substringFromIndex:8];

    //    output = (NSMutableString*)[output substringToIndex:16];

    return output;

    }

    -(NSString*) sha1:(NSString *)str

    {

    const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSData dataWithBytes:cstr length:str.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, (unsigned int)data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)

    [output appendFormat:@"%02x", digest[i]];

    return output;

    }

    相关文章

      网友评论

          本文标题:ios md加密

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