4

我使用 Lumberjack 作为日志平台(Objective C/Swift) 有没有办法将日志写入加密文件?

  • 如果是,那么任何示例都会很有用
  • 另外,之后如何读取加密日志
  • 密集日志记录是否有不同类型的加密?我听说了块加密
4

2 回答 2

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)"
        }
    }
}
于 2016-06-01T07:17:12.673 回答
1

如果您可以使用可用的设备加密

在您的应用程序的 plist 中设置苹果文件系统加密并忘记这个问题:)

在这里阅读更多信息:
https ://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

或更短的摘要(页面底部):
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;
于 2016-05-22T07:24:23.857 回答