String+FoundationExtension.swift 955 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // String+Extension.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 13/10/15.
  6. // Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import Foundation
  9. extension String {
  10. /// Return Base64 back to String
  11. public func decryptBase64ToString(cipher: Cipher) throws -> String {
  12. guard let decodedData = NSData(base64EncodedString: self, options: []) else {
  13. throw CipherError.Decrypt
  14. }
  15. let decrypted = try decodedData.decrypt(cipher)
  16. if let decryptedString = String(data: decrypted, encoding: NSUTF8StringEncoding) {
  17. return decryptedString
  18. }
  19. throw CipherError.Decrypt
  20. }
  21. public func decryptBase64(cipher: Cipher) throws -> [UInt8] {
  22. guard let decodedData = NSData(base64EncodedString: self, options: []) else {
  23. throw CipherError.Decrypt
  24. }
  25. return try decodedData.decrypt(cipher).arrayOfBytes()
  26. }
  27. }