I have an encrypting method in Swift 2.2. How can I get bytes from data in Swift 3?
Here is my code:
func myEncrypt(encryptData:String) -> String? {
//security key must be 24charachters
let myKeyData : Data = "*******".data(using: String.Encoding.utf8)!
let myRawData : Data = encryptData.data(using: String.Encoding.utf8)!
let mykeydatamd5 = Data(bytes: myKeyData.bytes, count: 24)
let Crypto_status: CCCryptorStatus = CCCrypt(operation, algoritm, options, mykeydatamd5.bytes , keyLength, nil, myRawData.bytes, myRawData.count, buffer, buffer_size, &num_bytes_encrypted)
if UInt32(Crypto_status) == UInt32(kCCSuccess){
let myResult: Data = Data(bytes: buffer, count: num_bytes_encrypted)
free(buffer)
return myResult.base64EncodedString()
}else{
free(buffer)
return nil
}
}
The error occurs in myKeyData.bytes
and myRawData.bytes
.
As shown in the error message, you need to use
withUnsafeBytes
instead.Generally, if you have some expression like this:
You need to rewrite it as:
If you use multiple
.bytes
in a single expression, you may need to use nestedwithUnsafeBytes
. In your case the related part of your code should be something like this: