重叠访问'salt',但修改需要独占访问;考虑复制到局部变量
Overlapping accesses to 'salt', but modification requires exclusive access; consider copying to a local variable
我正在开发具有敏感数据的高度安全的 iOS 应用程序。
我正在尝试使用 AES256 加密系统来保护数据。
我按照这里的教程做了 https://code.tutsplus.com/tutorials/securing-ios-data-at-rest-encryption--cms-28786
Xcode 11 (Swift 5) 告诉我 "Overlapping accesses to 'salt', but modification requires exclusive access; consider copying to a local variable"
请问我该如何解决这个问题?
谢谢。
这是我的代码:
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
}
}
最后,我像这样修改我的代码解决了我的问题:
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
}
}
我正在开发具有敏感数据的高度安全的 iOS 应用程序。 我正在尝试使用 AES256 加密系统来保护数据。
我按照这里的教程做了 https://code.tutsplus.com/tutorials/securing-ios-data-at-rest-encryption--cms-28786
Xcode 11 (Swift 5) 告诉我 "Overlapping accesses to 'salt', but modification requires exclusive access; consider copying to a local variable"
请问我该如何解决这个问题?
谢谢。
这是我的代码:
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
}
}
最后,我像这样修改我的代码解决了我的问题:
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
}
}