Implementation of the OBJECTIVE-C MD5/SHA1 encryption algorithm for iOS development

Source: Internet
Author: User
Tags sha1 sha1 encryption vars

[OBJC]View Plain copy
    1. 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
    2. 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
    3. 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.
    4. Use the following methods:
    5. MD5 Encryption Method
    6. -(NSString *) MD5
    7. {
    8. Const Char char*cstr = [self utf8string];
    9. unsigned char digest[cc_md5_digest_length];
    10. CC_MD5 (cStr, strlen (CSTR), Digest);
    11. nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];
    12. For (int i = 0; i < cc_md5_digest_length; i++)
    13. [Output AppendFormat:@ "%02x", Digest[i]];
    14. return output;
    15. }
    16. SHA1 Encryption Method
    17. -(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;
      }
    18. Of course it can also be used in conjunction with base64来, where the base64 encoding is implemented using Gtmbase64 and needs to be imported
    19. -(NSString *) sha1_base64
    20. {
    21. Const Char char*cstr = [self cstringusingencoding:nsutf8StringEncoding];
    22. NSData *data = [NSData datawithbytes:cstr length:self. length];
    23. UINT8_t Digest[cc_sha1_digest_length];
    24. Cc_sha1 (databytes, data. length, digest);
    25. NSData * Base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length];
    26. Base64 = [gtmbase64 encodedata:base64];
    27. NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8StringEncoding];
    28. return output;
    29. }
    30. -(NSString *) MD5_base64
    31. {
    32. Const Char char*cstr = [self utf8string];
    33. unsigned char digest[cc_md5_digest_length];
    34. CC_MD5 (cStr, strlen (CSTR), Digest);
    35. NSData * Base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length];
    36. Base64 = [gtmbase64 encodedata:base64];
    37. NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf    8StringEncoding];
    38. return output;
    39. }
    40. By expanding NSString to achieve full functionality, all code
    41. @interface NSString (Encrypto)
    42. -(NSString *) MD5;
    43. -(NSString *) Sha1;
    44. -(NSString *) sha1_base64;
    45. -(NSString *) MD5_base64;
    46. -(NSString *) Base64;
    47. @end
    48. @implementation NSString (Encrypto)
    49. -(nsstring*) Sha1
    50. {
    51. Const Char char*cstr = [self cstringusingencoding:nsutf8StringEncoding];
    52. NSData *data = [NSData datawithbytes:cstr length:self. length];
    53. UINT8_t Digest[cc_sha1_digest_length];
    54. Cc_sha1 (databytes, data. length, digest);
    55. nsmutablestring* output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length * 2];
    56. For (int i = 0; i < Cc_sha1_digest_length; i++)
    57. [Output AppendFormat:@ "%02x", Digest[i]];
    58. return output;
    59. }
    60. -(NSString *) MD5
    61. {
    62. Const Char char*cstr = [self utf8string];
    63. unsigned char digest[cc_md5_digest_length];
    64. CC_MD5 (cStr, strlen (CSTR), Digest);
    65. nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];
    66. For (int i = 0; i < cc_md5_digest_length; i++)
    67. [Output AppendFormat:@ "%02x", Digest[i]];
    68. return output;
    69. }
    70. -(NSString *) sha1_base64
    71. {
    72. Const Char char*cstr = [self cstringusingencoding:nsutf8StringEncoding];
    73. NSData *data = [NSData datawithbytes:cstr length:self. length];
    74. UINT8_t Digest[cc_sha1_digest_length];
    75. Cc_sha1 (databytes, data. length, digest);
    76. NSData * Base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length];
    77. Base64 = [gtmbase64 encodedata:base64];
    78. NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf    8StringEncoding];
    79. return output;
    80. }
    81. -(NSString *) MD5_base64
    82. {
    83. Const Char char*cstr = [self utf8string];
    84. unsigned char digest[cc_md5_digest_length];
    85. CC_MD5 (cStr, strlen (CSTR), Digest);
    86. NSData * Base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length];
    87. Base64 = [gtmbase64 encodedata:base64];
    88. NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8StringEncoding];
    89. return output;
    90. }
    91. -(NSString *) Base64
    92. {
    93. NSData * data = [self datausingencoding:nsasciistringencoding allowlossyconversion:YES];
    94. data = [Gtmbase64 encodedata:data];
    95. NSString * output = [[NSString alloc] initwithdata:data encoding:nsutf8StringEncoding];
    96. return output;
    97. }
    98. @end
    99. Do not forget to import cc-related library header files when implementing
    100. Commoncrypto/commondigest. h

Implementation of the OBJECTIVE-C MD5/SHA1 encryption algorithm for iOS development

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.