好久没更新技术文章了,正好最近做了点东西,拿出来分享一下。
鉴于行业数据的保密性,公司要求在集成了融云及时聊天的App中对消息做加、解密处理,保证在消息转发过程中,消息是以密文传输的。关于如何加、解密以及密钥的传输、存放我就不说了,自己百度去,下面来谈谈怎么对RCTextMessage做加解密。
由于官方的SDK只对开发者暴露了头文件,对里面的实现方式我们并不能知道,怀着研究的心态,我给它反编译(class-dump)了,多少能看到点东西~~~😝😝😝
#import "RCMessageContent.h"
#import "NSCoding.h"
@class NSString;
@interface RCTextMessage : RCMessageContent <NSCoding>
{
NSString *_content;
NSString *_extra;
}
+ (id)getObjectName;
+ (unsigned long long)persistentFlag;
+ (id)messageWithContent:(id)arg1;
@property(retain, nonatomic) NSString *extra; // @synthesize extra=_extra;
@property(retain, nonatomic) NSString *content; // @synthesize content=_content;
- (void).cxx_destruct;
- (id)conversationDigest;
- (id)getSearchableWords;
- (void)decodeWithData:(id)arg1;
- (id)encode;
- (void)encodeWithCoder:(id)arg1;
- (id)initWithCoder:(id)arg1;
@end
看到了吗,我们要的东西出来了,"encodeWithCoder:"、 "encode"、 "decodeWithData:"。自然而然,思路也就顺了——在它编码、解码的时候,做加、解密。
怎么做呢?原本我是想用继承的,后来考虑到代码侵入性太大,不如使用Category给它重写了,不要谢我,就是这么刚,这么邪恶~~ 😝😝😝言简意赅,能用代码解决的就不逼逼,自己看代码去吧。
#import "RCTextMessage+Custom.h"
@implementation RCTextMessage (Custom)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
/// NSCoding
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.content = [aDecoder decodeObjectForKey:@"content"];
self.extra = [aDecoder decodeObjectForKey:@"extra"];
}
return self;
}
/// NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.content.ib_encrypt forKey:@"content"];
[aCoder encodeObject:self.extra forKey:@"extra"];
}
///将消息内容编码成json
- (NSData *)encode {
NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
[dataDict setObject:self.content.ib_encrypt forKey:@"content"];
if (self.extra) {
[dataDict setObject:self.extra forKey:@"extra"];
}
if (self.senderUserInfo) {
NSMutableDictionary *userInfoDic = [[NSMutableDictionary alloc] init];
if (self.senderUserInfo.name) {
[userInfoDic setObject:self.senderUserInfo.name forKeyedSubscript:@"name"];
}
if (self.senderUserInfo.portraitUri) {
[userInfoDic setObject:self.senderUserInfo.portraitUri forKeyedSubscript:@"portrait"];
}
if (self.senderUserInfo.userId) {
[userInfoDic setObject:self.senderUserInfo.userId forKeyedSubscript:@"id"];
}
[dataDict setObject:userInfoDic forKey:@"user"];
}
NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:kNilOptions error:nil];
return data;
}
///将json解码生成消息内容
- (void)decodeWithData:(NSData *)data {
if (data) {
__autoreleasing NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (dictionary) {
self.extra = dictionary[@"extra"];
NSString *originContent = dictionary[@"content"];
self.content = originContent.ib_decrypt;
NSDictionary *userinfoDic = dictionary[@"user"];
[self decodeUserInfo:userinfoDic];
}
}
}
#pragma clang diagnostic pop
@end
"ib_encrypt"、"ib_decrypt"为加、解密方法。其他类型的Message也可以这样做,当然,这里我只对content做了加、解密,如果对data直接做加、解密也是可以的。
欢迎前辈给予指点,谢谢!
网友评论