我正在使用Lumberjack作为日志记录平台(Objective C / Swift) 有没有办法将日志写入文件加密?
答案 0 :(得分:2)
import CocoaLumberjack
import Security
public class EncryptedLogger: DDAbstractLogger {
let key: SecKey!
let blockSize : Int
let padding : SecPadding
init(key: SecKey!, blockSize : Int = 128, padding: SecPadding = .PKCS1) {
self.key = key
self.blockSize = blockSize
self.padding = padding
}
convenience init(keyFilePath: String, blockSize: Int = 128, padding: SecPadding = .PKCS1) {
//TODO: load key from file
self.init(key: nil, blockSize: blockSize, padding: padding)
}
/**
* The log message method
*
* @param logMessage the message (model)
*/
public override func logMessage(logMessage: DDLogMessage!) {
let plainText = logFormatter != nil ? logFormatter.formatLogMessage(logMessage) : logMessage.message;
let plainTextData = [UInt8](plainText.utf8)
var encryptedData = [UInt8](count: Int(blockSize), repeatedValue: 0)
var encryptedDataLength = blockSize
let result = SecKeyEncrypt(key, padding, plainTextData, plainTextData.count, &encryptedData, &encryptedDataLength)
//TODO: write the encryptedData to a file or post it to some endpoint
//...
}
@objc
public override var loggerName: String! {
get {
return "\(self.dynamicType)"
}
}
}
答案 1 :(得分:1)
在您应用的plist中设置Apple文件系统加密并忘记此问题:)
或较短的摘要(页面底部):
http://www.darthnull.org/2014/10/06/ios-encryption
为您的应用ID设置启用的数据保护权利,以保护您应用的所有文件:
替代方法:您可以在写入时将NSFileProtection标志设置为文件。
objC代码:
NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
if (![[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:filePath error:error]) {
return NO;
}
return YES;