Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

EncryptionController.swift 942 B

12345678910111213141516171819202122232425262728293031323334
  1. //
  2. // File.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 4/2/24.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Crypto
  10. enum CryptoError: Error{
  11. case badRequest
  12. }
  13. struct CustomCrypto{
  14. public static func DecryptString(input: String) throws -> String{
  15. let apikey = Environment.get("API_KEY")
  16. let base64Key = apikey! // Replace with your actual key
  17. guard let keyData = Data(base64Encoded: base64Key) else {
  18. fatalError("Invalid Base64 key data")
  19. }
  20. let data = Data(base64Encoded: input)
  21. let encryptionKey = SymmetricKey(data: keyData)
  22. let sealedBox = try! AES.GCM.SealedBox(combined: data!)
  23. let decryptedData = try! AES.GCM.open(sealedBox, using: encryptionKey)
  24. guard let decryptedString = String(data: decryptedData, encoding: .utf8) else{
  25. throw CryptoError.badRequest
  26. }
  27. return decryptedString
  28. }
  29. }