BASE64 encoding-MD5 Encryption-URL encoding

Source: Internet
Author: User

#pragma mark--------Code--------------

+ (NSString *) EncodeBase64: (NSString *) text

{

NSData * data = [text datausingencoding:nsutf8stringencoding allowlossyconversion:yes];

Convert to Base64

data = [GTMBase64 encodedata:data];

NSString * base64string = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease];

return base64string;

}

+ (NSString *) DecodeBase64: (NSString *) text

{

NSData * data = [text datausingencoding:nsutf8stringencoding allowlossyconversion:yes];

Convert to Normal

data = [GTMBase64 decodedata:data];

NSString * string = [[NSString alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease];

return string;

}

URL encoding

+ (NSString *) encodetopercentescapestring: (NSString *) input

{

NSString *outputstr = (NSString *)

Cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault,

(cfstringref) input,

Null

(cfstringref) @ "!*" ();: @&=+$,/?%#[] ",

KCFSTRINGENCODINGUTF8);

if (!outputstr | | (NSNull *) Outputstr = = [NSNull Null] | | OUTPUTSTR.LENGTH<1)

{

return @ "";

}

return outputstr;

}

MD5 encryption

+ (NSString *) md5string: (NSString *) str

{

const char *ORIGINAL_STR = [Str utf8string];

unsigned char result[cc_md5_digest_length];

CC_MD5 (ORIGINAL_STR, (int) strlen (ORIGINAL_STR), result);

nsmutablestring *hash = [nsmutablestring string];

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

[Hash appendformat:@ "%02x", Result[i]];

return [hash lowercasestring];

}

Static inline char Hexchar (unsigned char c) {

Return C < 10? ' 0 ' + C: ' a ' + c-10;

}

static inline void hexstring (unsigned char *from, char *to, Nsuinteger length) {

for (Nsuinteger i = 0; i < length; ++i) {

unsigned char c = from[i];

unsigned char Chigh = c >> 4;

unsigned char Clow = c & 0xf;

To[2 * I] = Hexchar (Chigh);

to[2 * i + 1] = Hexchar (Clow);

}

to[2 * Length] = ' + ';

}

NSString * SHA1 (const char *string)

{

if (!string)

{

return nil;

}

static const Nsuinteger LENGTH = 20;

unsigned char result[length];

CC_SHA1 (String, (Cc_long) strlen (string), result);

Char hexresult[2 * LENGTH + 1];

HexString (result, Hexresult, LENGTH);

return [NSString Stringwithutf8string:hexresult];

}

BASE64 encoding-MD5 Encryption-URL encoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.