mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Swift: Improve the encryption in examples for swift/cleartext-* queries.
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import CryptoKit
|
||||
|
||||
func storeMyData(databaseObject : NSManagedObject, faveSong : String, creditCardNo : String) {
|
||||
private func encrypt(_ text: String, _ encryptionKey: SymmetricKey) -> String {
|
||||
let sealedBox = try! AES.GCM.seal(Data(text.utf8), using: encryptionKey)
|
||||
return sealedBox.combined!.base64EncodedString()
|
||||
}
|
||||
|
||||
func storeMyData(databaseObject : NSManagedObject, faveSong : String, creditCardNo : String, key: SymmetricKey) {
|
||||
// ...
|
||||
|
||||
// GOOD: not sensitive information
|
||||
@@ -9,7 +15,7 @@ func storeMyData(databaseObject : NSManagedObject, faveSong : String, creditCard
|
||||
databaseObject.setValue(creditCardNo, forKey: "myCreditCardNo")
|
||||
|
||||
// GOOD: encrypted sensitive information saved
|
||||
databaseObject.setValue(encrypt(creditCardNo), forKey: "myCreditCardNo")
|
||||
databaseObject.setValue(encrypt(creditCardNo, encryptionKey), forKey: "myCreditCardNo")
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import CryptoKit
|
||||
|
||||
func transmitMyData(connection : NWConnection, faveSong : String, creditCardNo : String) {
|
||||
private func encrypt(_ text: String, _ encryptionKey: SymmetricKey) -> String {
|
||||
let sealedBox = try! AES.GCM.seal(Data(text.utf8), using: encryptionKey)
|
||||
return sealedBox.combined!.base64EncodedString()
|
||||
}
|
||||
|
||||
func transmitMyData(connection : NWConnection, faveSong : String, creditCardNo : String, key: SymmetricKey) {
|
||||
// ...
|
||||
|
||||
// GOOD: not sensitive information
|
||||
@@ -9,7 +15,7 @@ func transmitMyData(connection : NWConnection, faveSong : String, creditCardNo :
|
||||
connection.send(content: creditCardNo, completion: .idempotent)
|
||||
|
||||
// GOOD: encrypted sensitive information saved
|
||||
connection.send(content: encrypt(creditCardNo), completion: .idempotent)
|
||||
connection.send(content: encrypt(creditCardNo, encryptionKey), completion: .idempotent)
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import CryptoKit
|
||||
|
||||
func storeMyData(faveSong : String, creditCardNo : String) {
|
||||
private func encrypt(_ text: String, _ encryptionKey: SymmetricKey) -> String {
|
||||
let sealedBox = try! AES.GCM.seal(Data(text.utf8), using: encryptionKey)
|
||||
return sealedBox.combined!.base64EncodedString()
|
||||
}
|
||||
|
||||
func storeMyData(faveSong : String, creditCardNo : String, encryptionKey: SymmetricKey) {
|
||||
// ...
|
||||
|
||||
// GOOD: not sensitive information
|
||||
@@ -9,7 +15,7 @@ func storeMyData(faveSong : String, creditCardNo : String) {
|
||||
UserDefaults.standard.set(creditCardNo, forKey: "myCreditCardNo")
|
||||
|
||||
// GOOD: encrypted sensitive information saved
|
||||
UserDefaults.standard.set(encrypt(creditCardNo), forKey: "myCreditCardNo")
|
||||
UserDefaults.standard.set(encrypt(creditCardNo, encryptionKey), forKey: "myCreditCardNo")
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user