parent
4fe9743a32
commit
3da45f62db
@ -0,0 +1,47 @@ |
||||
// |
||||
// String+Crypto.swift |
||||
// PadelClub |
||||
// |
||||
// Created by Laurent Morvillier on 30/04/2024. |
||||
// |
||||
|
||||
import Foundation |
||||
import CryptoKit |
||||
|
||||
enum CryptoError: Error { |
||||
case invalidUTF8 |
||||
case cantConvertUTF8 |
||||
case invalidBase64String |
||||
case nilSeal |
||||
} |
||||
|
||||
extension Data { |
||||
|
||||
func encrypt(pass: String) throws -> Data { |
||||
let key = try self._createSymmetricKey(fromString: pass) |
||||
let sealedBox = try AES.GCM.seal(self, using: key) |
||||
if let combined = sealedBox.combined { |
||||
return combined |
||||
} |
||||
throw CryptoError.nilSeal |
||||
} |
||||
|
||||
func decryptData(pass: String) throws -> String { |
||||
let key = try self._createSymmetricKey(fromString: pass) |
||||
let sealedBox = try AES.GCM.SealedBox(combined: self) |
||||
let decryptedData = try AES.GCM.open(sealedBox, using: key) |
||||
guard let decryptedMessage = String(data: decryptedData, encoding: .utf8) else { |
||||
throw CryptoError.invalidUTF8 |
||||
} |
||||
return decryptedMessage |
||||
} |
||||
|
||||
fileprivate func _createSymmetricKey(fromString keyString: String) throws -> SymmetricKey { |
||||
guard let keyData = Data(base64Encoded: keyString) else { |
||||
throw CryptoError.invalidBase64String |
||||
} |
||||
return SymmetricKey(data: keyData) |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,12 @@ |
||||
// |
||||
// Key.swift |
||||
// PadelClub |
||||
// |
||||
// Created by Laurent Morvillier on 30/04/2024. |
||||
// |
||||
|
||||
import Foundation |
||||
|
||||
enum Key: String { |
||||
case pass = "Aa9QDV1G5MP9ijF2FTFasibNbS/Zun4qXrubIL2P+Ik=" |
||||
} |
||||
@ -0,0 +1,46 @@ |
||||
// |
||||
// PaymentTests.swift |
||||
// PadelClubTests |
||||
// |
||||
// Created by Laurent Morvillier on 01/05/2024. |
||||
// |
||||
|
||||
import XCTest |
||||
@testable import PadelClub |
||||
|
||||
final class PaymentTests: XCTestCase { |
||||
|
||||
override func setUpWithError() throws { |
||||
// Put setup code here. This method is called before the invocation of each test method in the class. |
||||
} |
||||
|
||||
override func tearDownWithError() throws { |
||||
// Put teardown code here. This method is called after the invocation of each test method in the class. |
||||
} |
||||
|
||||
func testPayments() throws { |
||||
|
||||
let tournament = Tournament.fake() |
||||
tournament.setPayment(.free) |
||||
assert(tournament.decryptPayment() == .free) |
||||
tournament.setPayment(.subscriptionUnit) |
||||
assert(tournament.decryptPayment() == .subscriptionUnit) |
||||
tournament.setPayment(.unit) |
||||
assert(tournament.decryptPayment() == .unit) |
||||
tournament.setPayment(.unlimited) |
||||
assert(tournament.decryptPayment() == .unlimited) |
||||
|
||||
} |
||||
|
||||
func testCanceled() throws { |
||||
|
||||
let tournament = Tournament.fake() |
||||
|
||||
tournament.setCanceled(true) |
||||
assert(tournament.decryptCanceled() == true) |
||||
tournament.setCanceled(false) |
||||
assert(tournament.decryptCanceled() == false) |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue