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

我正在使用Lumberjack作为日志记录平台(Objective C /
Swift)

有没有办法将日志写入文件加密?

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

最佳答案 如果要滚动自己的自定义记录器

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)"
        }
    }
}
点赞