Lumberjack iOS:如何编写加密日志(块加密)

时间:2016-05-22 07:12:17

标签: ios lumberjack

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

  • 如果是,那么任何一个例子都是有用的
  • 此外,如何阅读加密日志
  • 密集记录是否有不同类型的加密?我听说过Block Encryption

2 个答案:

答案 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文件系统加密并忘记此问题:)

在此处详细了解:
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

或较短的摘要(页面底部):
http://www.darthnull.org/2014/10/06/ios-encryption

工作原理:

为您的应用ID设置启用的数据保护权利,以保护您应用的所有文件: required capability

替代方法:您可以在写入时将NSFileProtection标志设置为文件。

objC代码:

NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
if (![[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:filePath error:error]) {
    return NO;
}
return YES;