No Description

RSADataVerifier.m 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // AlixPayDataVerifier.m
  3. // SafepayService
  4. //
  5. // Created by wenbi on 11-4-11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "RSADataVerifier.h"
  9. #import "openssl_wrapper.h"
  10. #import "NSDataEx.h"
  11. #import "base64.h"
  12. @implementation RSADataVerifier
  13. - (id)initWithPublicKey:(NSString *)publicKey {
  14. if (self = [super init]) {
  15. _publicKey = [publicKey copy];
  16. }
  17. return self;
  18. }
  19. - (NSString *)formatPublicKey:(NSString *)publicKey {
  20. NSMutableString *result = [NSMutableString string];
  21. [result appendString:@"-----BEGIN PUBLIC KEY-----\n"];
  22. int count = 0;
  23. for (int i = 0; i < [publicKey length]; ++i) {
  24. unichar c = [publicKey characterAtIndex:i];
  25. if (c == '\n' || c == '\r') {
  26. continue;
  27. }
  28. [result appendFormat:@"%c", c];
  29. if (++count == 76) {
  30. [result appendString:@"\n"];
  31. count = 0;
  32. }
  33. }
  34. [result appendString:@"\n-----END PUBLIC KEY-----\n"];
  35. return result;
  36. }
  37. - (NSString *)algorithmName {
  38. return @"RSA";
  39. }
  40. - (BOOL)verifyString:(NSString *)string withSign:(NSString *)signString {
  41. // const char *message = [string cStringUsingEncoding:NSUTF8StringEncoding];
  42. // int messageLength = strlen(message);
  43. //
  44. // unsigned char *signature = (unsigned char *)[signString UTF8String];
  45. // unsigned int signatureLength = (unsigned int)strlen((char *)signature);
  46. // char *encodedPath = (char *)[_pathForPEMFile cStringUsingEncoding:NSUTF8StringEncoding];
  47. NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  48. NSString *path = [documentPath stringByAppendingPathComponent:@"AlixPay-RSAPublicKey"];
  49. //
  50. // 把密钥写入文件
  51. //
  52. NSString *formatKey = [self formatPublicKey:_publicKey];
  53. [formatKey writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
  54. BOOL ret;
  55. rsaVerifyString(string, signString, path, &ret);
  56. return ret;
  57. }
  58. @end