JSPatch部署JS代碼控制OC代碼,jspatch部署jsoc

來源:互聯網
上載者:User

JSPatch部署JS代碼控制OC代碼,jspatch部署jsoc
前言:

這裡算是比較實用的一篇了吧,進行了網路安全部署

準備:

bmob帳號,JSPatch,公開金鑰,私密金鑰,MD5加密,AFNetWorking下載檔案

#import <CommonCrypto/CommonDigest.h>

#import <BmobSDK/Bmob.h>

#import "AFNetworking.h"

#import "JPEngine.h"

#import "RSA.h"

原理:

1.使用bmob,得到私密金鑰加密後的jsMD5,JS檔案地址

2.根據地址下載js檔案,並且儲存到沙箱中

3.根據加密MD5匹配JS檔案內容,成功就執行

4.沒有進行網路判斷,只有有網的情況才能執行,沒網的時候就使用以前的OC代碼

程式碼片段:

1.請求bmob上的資料

    //這裡是bmob的key自己去申請個帳號,建立一個應用就有了    [Bmob registerWithAppKey:@"這裡填寫自己的key"];    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"JSPathString"];    //約束,只取版本號碼為1的    [bquery whereKey:@"version" equalTo:@"1"]; //是1得就取得    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {        //刪除操作,如果。。。。刪除檔案。。。。        if (!error) {            if (array.count > 0) {                BmobObject *object = array[0];                NSString *PrivateMD5String = [object objectForKey:@"PrivkeyMD5String"];                BmobFile *file = (BmobFile*)[object objectForKey:@"JSData"];                NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);                //取得第一個Documents檔案夾的路徑                NSString *filePath = [path objectAtIndex:0];                //                NSLog(@"%@",filePath);                [self downloadFileURL:file.url savePath:filePath fileName:file.name tag:0 PrivateMD5String:PrivateMD5String];            }        }else {            NSLog(@"失敗的情況。。。");        }    }];

2.根據提供的地址下載檔案

/** * 下載檔案,這是用的,網路請求,要換,現在都是af3.0了 */+ (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName tag:(NSInteger)aTag PrivateMD5String:(NSString *)PrivateMD5String{    NSFileManager *fileManager = [NSFileManager defaultManager];        //檢查本地檔案是否已存在    NSString *fileName = [NSString stringWithFormat:@"%@/%@", aSavePath, aFileName];        //檢查附件是否存在    if ([fileManager fileExistsAtPath:fileName]) {        //        NSLog(@"本地已經存在");        [self doJSPathWith:PrivateMD5String fileName:fileName];            }else{        //建立附件儲存目錄        if (![fileManager fileExistsAtPath:aSavePath]) {            [fileManager createDirectoryAtPath:aSavePath withIntermediateDirectories:YES attributes:nil error:nil];        }                //下載附件        NSURL *url = [[NSURL alloc] initWithString:aUrl];        NSURLRequest *request = [NSURLRequest requestWithURL:url];                AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];        operation.inputStream   = [NSInputStream inputStreamWithURL:url];        operation.outputStream  = [NSOutputStream outputStreamToFileAtPath:fileName append:NO];                //下載進度控制                [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {            NSLog(@"is download:%f", (float)totalBytesRead/totalBytesExpectedToRead);        }];                //已經完成下載        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * operation, id responseObject) {            //下載的資料            NSLog(@"下載成功");            [self doJSPathWith:PrivateMD5String fileName:fileName];        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {            NSLog(@"下載失敗");        }];        [operation start];    }}

3.匹配檢查JS檔案是否正確

+ (void)doJSPathWith:(NSString *)PrivateMD5String fileName:(NSString *)fileName{#pragma mark - 用戶端操作,可以等待伺服器,等待時間可以10's左右,網路請求後得到資料,然後還可以加一個開關按鈕,可決定是否傳這個js檔案,以免出現BUG    //公開金鑰    NSString *pubkey = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCiw8lUn/RkpTFrIsC49i9ETR1\nRZuc265x7emOYgSLF4E2LkvxZk0klIC74MBDABhyN+TFQ2J9FkJX8XkzQkrnTv/G\nh0td+U+0D84tK81NJzBa1+pPXE55JjarwNLzf1VEfUBUWMoGKTVQV05sC0JjsYyX\nm6mbFq8j9v7ygO/FQQIDAQAB";        //伺服器返回的RSA私密金鑰加密的字串字串    NSString *fuwuqiMD5String = PrivateMD5String;    //伺服器返回的JS字串    NSString *script = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];;        //公開金鑰解密得到解密後的MD5字串    NSString *decWithPrivKey = [RSA decryptString:fuwuqiMD5String publicKey:pubkey];    [JPEngine startEngine];    if ([[self md5:script] isEqualToString:decWithPrivKey]) {        //        NSLog(@"匹配成功");        [JPEngine evaluateScript:script];    }else {        //        NSLog(@"匹配失敗");    }}

4.其它(MD5加密)

//md5加密+ (NSString *)md5:(NSString *)input {    const char* str = [input UTF8String];    unsigned char result[CC_MD5_DIGEST_LENGTH];    CC_MD5(str, (unsigned)strlen((const char *)str), result);    NSMutableString *ret = [NSMutableString string];    for(int i = 0; i<16; i++) {        [ret appendFormat:@"%02X",result[i]];    }    return ret;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.