No Description

RSADataSigner.m 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // RSADataSigner.m
  3. // SafepayService
  4. //
  5. // Created by wenbi on 11-4-11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "RSADataSigner.h"
  9. #import "openssl_wrapper.h"
  10. #import "NSDataEx.h"
  11. @implementation RSADataSigner
  12. - (id)initWithPrivateKey:(NSString *)privateKey {
  13. if (self = [super init]) {
  14. _privateKey = [privateKey copy];
  15. }
  16. return self;
  17. }
  18. - (NSString*)urlEncodedString:(NSString *)string
  19. {
  20. NSString * encodedString = (__bridge_transfer NSString*) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, (__bridge CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
  21. return encodedString;
  22. }
  23. - (NSString *)formatPrivateKey:(NSString *)privateKey {
  24. const char *pstr = [privateKey UTF8String];
  25. int len = [privateKey length];
  26. NSMutableString *result = [NSMutableString string];
  27. [result appendString:@"-----BEGIN PRIVATE KEY-----\n"];
  28. int index = 0;
  29. int count = 0;
  30. while (index < len) {
  31. char ch = pstr[index];
  32. if (ch == '\r' || ch == '\n') {
  33. ++index;
  34. continue;
  35. }
  36. [result appendFormat:@"%c", ch];
  37. if (++count == 79)
  38. {
  39. [result appendString:@"\n"];
  40. count = 0;
  41. }
  42. index++;
  43. }
  44. [result appendString:@"\n-----END PRIVATE KEY-----"];
  45. return result;
  46. }
  47. - (NSString *)algorithmName {
  48. return @"RSA";
  49. }
  50. //该签名方法仅供参考,外部商户可用自己方法替换
  51. - (NSString *)signString:(NSString *)string {
  52. //在Document文件夹下创建私钥文件
  53. NSString * signedString = nil;
  54. NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  55. NSString *path = [documentPath stringByAppendingPathComponent:@"AlixPay-RSAPrivateKey"];
  56. //
  57. // 把密钥写入文件
  58. //
  59. NSString *formatKey = [self formatPrivateKey:_privateKey];
  60. [formatKey writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
  61. const char *message = [string cStringUsingEncoding:NSUTF8StringEncoding];
  62. int messageLength = strlen(message);
  63. unsigned char *sig = (unsigned char *)malloc(256);
  64. unsigned int sig_len;
  65. int ret = rsa_sign_with_private_key_pem((char *)message, messageLength, sig, &sig_len, (char *)[path UTF8String]);
  66. //签名成功,需要给签名字符串base64编码和UrlEncode,该两个方法也可以根据情况替换为自己函数
  67. if (ret == 1) {
  68. NSString * base64String = base64StringFromData([NSData dataWithBytes:sig length:sig_len]);
  69. //NSData * UTF8Data = [base64String dataUsingEncoding:NSUTF8StringEncoding];
  70. signedString = [self urlEncodedString:base64String];
  71. }
  72. free(sig);
  73. return signedString;
  74. }
  75. @end