FSOpenSSL.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // FSOpenSSL.m
  3. // OpenSSL-for-iOS
  4. //
  5. // Created by Felix Schulze on 16.03.2013.
  6. // Copyright 2013 Felix Schulze. All rights reserved.
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License");
  9. // you may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at
  11. //
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS,
  16. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. // See the License for the specific language governing permissions and
  18. // limitations under the License.
  19. #import "FSOpenSSL.h"
  20. #include <openssl/md5.h>
  21. #include <openssl/sha.h>
  22. #import <openssl/evp.h>
  23. @implementation FSOpenSSL
  24. + (NSString *)md5FromString:(NSString *)string {
  25. unsigned char *inStrg = (unsigned char *) [[string dataUsingEncoding:NSASCIIStringEncoding] bytes];
  26. unsigned long lngth = [string length];
  27. unsigned char result[MD5_DIGEST_LENGTH];
  28. NSMutableString *outStrg = [NSMutableString string];
  29. MD5(inStrg, lngth, result);
  30. unsigned int i;
  31. for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
  32. [outStrg appendFormat:@"%02x", result[i]];
  33. }
  34. return [outStrg copy];
  35. }
  36. + (NSString *)sha256FromString:(NSString *)string {
  37. unsigned char *inStrg = (unsigned char *) [[string dataUsingEncoding:NSASCIIStringEncoding] bytes];
  38. unsigned long lngth = [string length];
  39. unsigned char result[SHA256_DIGEST_LENGTH];
  40. NSMutableString *outStrg = [NSMutableString string];
  41. SHA256_CTX sha256;
  42. SHA256_Init(&sha256);
  43. SHA256_Update(&sha256, inStrg, lngth);
  44. SHA256_Final(result, &sha256);
  45. unsigned int i;
  46. for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
  47. [outStrg appendFormat:@"%02x", result[i]];
  48. }
  49. return [outStrg copy];
  50. }
  51. + (NSString *)base64FromString:(NSString *)string encodeWithNewlines:(BOOL)encodeWithNewlines {
  52. BIO *mem = BIO_new(BIO_s_mem());
  53. BIO *b64 = BIO_new(BIO_f_base64());
  54. if (!encodeWithNewlines) {
  55. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  56. }
  57. mem = BIO_push(b64, mem);
  58. NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
  59. NSUInteger length = stringData.length;
  60. void *buffer = (void *) [stringData bytes];
  61. int bufferSize = (int)MIN(length, INT_MAX);
  62. NSUInteger count = 0;
  63. BOOL error = NO;
  64. // Encode the data
  65. while (!error && count < length) {
  66. int result = BIO_write(mem, buffer, bufferSize);
  67. if (result <= 0) {
  68. error = YES;
  69. }
  70. else {
  71. count += result;
  72. buffer = (void *) [stringData bytes] + count;
  73. bufferSize = (int)MIN((length - count), INT_MAX);
  74. }
  75. }
  76. int flush_result = BIO_flush(mem);
  77. if (flush_result != 1) {
  78. return nil;
  79. }
  80. char *base64Pointer;
  81. NSUInteger base64Length = (NSUInteger) BIO_get_mem_data(mem, &base64Pointer);
  82. NSData *base64data = [NSData dataWithBytesNoCopy:base64Pointer length:base64Length freeWhenDone:NO];
  83. NSString *base64String = [[NSString alloc] initWithData:base64data encoding:NSUTF8StringEncoding];
  84. BIO_free_all(mem);
  85. return base64String;
  86. }
  87. @end