IOS---SHA1 encryption of a pit

Source: Internet
Author: User
Tags sha1 sha1 encryption

OC Language write SHA1 encryption algorithm, can be found on the Internet (such as the following), but I have to say that some people are not responsible, not to remind you to import the necessary system header files. resulting in errors

+ (nsstring *) SHA1: ( NSString *) Inputstr {

constchar *cstr = [inputstr cstringusingencoding: nsutf8stringencoding];

nsdata *data = [nsdatadatawithbytes: CStr length : input. length ];

uint8_t digest[cc_sha1_digest_length];

cc_sha1(data. bytes , (unsignedint) data. length , digest);

nsmutablestring *outputstr = [nsmutablestring Stringwithcapacity:cc_sha1_digest_length *2];

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

[outputstr appendformat:@ "%02x", Digest[i]];

}

return outputstr;

}


before using the SHA1 encryption algorithm, we need to #import <CommonCrypto/CommonDigest.h>

Hope that the vast number of blog enthusiasts, later can not write a little more careful, thank you!


In addition to SHA1, there are MD5, or their combination with Base64! This is often used, and of course there are very many better!

IOS---SHA1 encryption of a pit

Related Article

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.