Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

35 righe
942 B

  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. }