【问题标题】:Overlapping accesses to 'salt', but modification requires exclusive access; consider copying to a local variable对“salt”的访问重叠,但修改需要独占访问;考虑复制到局部变量
【发布时间】:2020-05-18 22:32:46
【问题描述】:

我正在开发一个包含敏感数据的高度安全的 iOS 应用程序。 我正在尝试使用 AES256 加密系统来保护数据。

我按照这里的教程https://code.tutsplus.com/tutorials/securing-ios-data-at-rest-encryption--cms-28786

Xcode 11 (Swift 5) 告诉我“对 'salt' 的重叠访问,但修改需要独占访问;考虑复制到局部变量”

你能告诉我如何解决这个问题吗?

谢谢。

这是我的代码:

var key = Data(repeating:0, count:kCCKeySizeAES256)
    var salt = Data(count: 8)
    salt.withUnsafeMutableBytes {
        (saltBytes: UnsafeMutablePointer<UInt8>) in//-> Void in
        let saltStatus = SecRandomCopyBytes(kSecRandomDefault, salt.count, saltBytes)
        if saltStatus == errSecSuccess
        {
            let passwordData = password.data(using:String.Encoding.utf8)!
            key.withUnsafeMutableBytes { (keyBytes : UnsafeMutablePointer<UInt8>) in
                let derivationStatus = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), password, passwordData.count, saltBytes, salt.count, CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA512), 14271, keyBytes, key.count)
                if derivationStatus != Int32(kCCSuccess)
                {
                    setupSuccess = false
                }
            }
        }
        else
        {
            setupSuccess = false
        }
    }

【问题讨论】:

  • 错误是针对哪一行产生的?生成部分还是 PBKDF 部分?
  • 在salt.withUnsafeMutableBytes {和key.withUnsafeMutableBytes { (keyBytes : UnsafeMutablePointer&lt;UInt8&gt;) in这行产生错误

标签: swift encryption aes


【解决方案1】:

最后,我解决了像这样修改代码的问题:

salt.withUnsafeMutableBytes { (saltBuffer: UnsafeMutableRawBufferPointer) in  
let saltBytes = saltBuffer.bindMemory(to: UInt8.self)  
let saltStatus = SecRandomCopyBytes(kSecRandomDefault, saltBytes.count, saltBytes.baseAddress!)  
if saltStatus == errSecSuccess {  
    let passwordData = password.data(using: .utf8)!  
    key.withUnsafeMutableBytes { (keyBuffer: UnsafeMutableRawBufferPointer) in  
        let keyBytes = keyBuffer.bindMemory(to: UInt8.self)  
        let derivationStatus = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), password, passwordData.count, saltBytes.baseAddress!, saltBytes.count, CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA512), 14271, keyBytes.baseAddress!, keyBytes.count)  
        if derivationStatus != Int32(kCCSuccess) {  
            setupSuccess = false  
        }  
    }  
} else {  
    setupSuccess = false  
}  

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-30
    • 2018-02-17
    • 2019-01-11
    • 2018-03-07
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2018-02-21
    相关资源
    最近更新 更多