[OBJC]View Plain copy
- The OBJECTIVE-C implementation of the MD5 and SHA1 algorithms is relatively simple, you can directly call the system's C + + shared library to implement the call
- the MD 5, message Digest algorithm 5 (Information-Digest algorithm 5), is used to ensure complete consistency of information transmission. is one of the most widely used hashing algorithms in Computers
- The SHA-Secure Hash algorithm (Secure Hash Algorithm) is a series of cryptographic hashing functions issued by the National Institute of Standards and Technology (NIST), the United States Department of State Security (NSA) design.
- Use the following methods:
- MD5 Encryption Method
- -(NSString *) MD5
- {
- Const Char char*cstr = [self utf8string];
- unsigned char digest[cc_md5_digest_length];
- CC_MD5 (cStr, 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]];
- return output;
- }
- SHA1 Encryption Method
- -(NSString *) SHA1: (NSString *) input
{
const char *CSTR = [input cstringusingencoding:nsutf8stringencoding];
NSData *data = [NSData datawithbytes:cstr length:input.length];
uint8_t Digest[cc_sha1_digest_length];
CC_SHA1 (Data.bytes, 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;
}
- Of course it can also be used in conjunction with base64来, where the base64 encoding is implemented using Gtmbase64 and needs to be imported
- -(NSString *) sha1_base64
- {
- Const Char char*cstr = [self cstringusingencoding:nsutf8StringEncoding];
- NSData *data = [NSData datawithbytes:cstr length:self. length];
- UINT8_t Digest[cc_sha1_digest_length];
- Cc_sha1 (databytes, data. length, digest);
- NSData * Base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length];
- Base64 = [gtmbase64 encodedata:base64];
- NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8StringEncoding];
- return output;
- }
- -(NSString *) MD5_base64
- {
- Const Char char*cstr = [self utf8string];
- unsigned char digest[cc_md5_digest_length];
- CC_MD5 (cStr, strlen (CSTR), Digest);
- NSData * Base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length];
- Base64 = [gtmbase64 encodedata:base64];
- NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf 8StringEncoding];
- return output;
- }
- By expanding NSString to achieve full functionality, all code
- @interface NSString (Encrypto)
- -(NSString *) MD5;
- -(NSString *) Sha1;
- -(NSString *) sha1_base64;
- -(NSString *) MD5_base64;
- -(NSString *) Base64;
- @end
- @implementation NSString (Encrypto)
- -(nsstring*) Sha1
- {
- Const Char char*cstr = [self cstringusingencoding:nsutf8StringEncoding];
- NSData *data = [NSData datawithbytes:cstr length:self. length];
- UINT8_t Digest[cc_sha1_digest_length];
- Cc_sha1 (databytes, 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;
- }
- -(NSString *) MD5
- {
- Const Char char*cstr = [self utf8string];
- unsigned char digest[cc_md5_digest_length];
- CC_MD5 (cStr, 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]];
- return output;
- }
- -(NSString *) sha1_base64
- {
- Const Char char*cstr = [self cstringusingencoding:nsutf8StringEncoding];
- NSData *data = [NSData datawithbytes:cstr length:self. length];
- UINT8_t Digest[cc_sha1_digest_length];
- Cc_sha1 (databytes, data. length, digest);
- NSData * Base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length];
- Base64 = [gtmbase64 encodedata:base64];
- NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf 8StringEncoding];
- return output;
- }
- -(NSString *) MD5_base64
- {
- Const Char char*cstr = [self utf8string];
- unsigned char digest[cc_md5_digest_length];
- CC_MD5 (cStr, strlen (CSTR), Digest);
- NSData * Base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length];
- Base64 = [gtmbase64 encodedata:base64];
- NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8StringEncoding];
- return output;
- }
- -(NSString *) Base64
- {
- NSData * data = [self datausingencoding:nsasciistringencoding allowlossyconversion:YES];
- data = [Gtmbase64 encodedata:data];
- NSString * output = [[NSString alloc] initwithdata:data encoding:nsutf8StringEncoding];
- return output;
- }
- @end
- Do not forget to import cc-related library header files when implementing
- Commoncrypto/commondigest. h
Implementation of the OBJECTIVE-C MD5/SHA1 encryption algorithm for iOS development