|
@@ -10,14 +10,27 @@ import Foundation
|
|
|
|
|
|
extension String {
|
|
|
|
|
|
- /// Return Base64 representation
|
|
|
- public func encrypt(cipher: Cipher) throws -> String {
|
|
|
- let encrypted = try self.utf8.lazy.map({ $0 as UInt8 }).encrypt(cipher)
|
|
|
- return NSData(bytes: encrypted).base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
|
|
|
+ /// Return Base64 back to String
|
|
|
+ public func decryptBase64ToString(cipher: Cipher) throws -> String {
|
|
|
+ guard let decodedData = NSData(base64EncodedString: self, options: []) else {
|
|
|
+ throw CipherError.Decrypt
|
|
|
+ }
|
|
|
+
|
|
|
+ let decrypted = try decodedData.decrypt(cipher)
|
|
|
+
|
|
|
+ if let decryptedString = String(data: decrypted, encoding: NSUTF8StringEncoding) {
|
|
|
+ return decryptedString
|
|
|
+ }
|
|
|
+
|
|
|
+ throw CipherError.Decrypt
|
|
|
}
|
|
|
|
|
|
- public func decrypt(cipher: Cipher) throws -> String {
|
|
|
- let decrypted = try self.utf8.lazy.map({ $0 as UInt8 }).decrypt(cipher)
|
|
|
- return NSData(bytes: decrypted).base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
|
|
|
+ public func decryptBase64(cipher: Cipher) throws -> [UInt8] {
|
|
|
+ guard let decodedData = NSData(base64EncodedString: self, options: []) else {
|
|
|
+ throw CipherError.Decrypt
|
|
|
+ }
|
|
|
+
|
|
|
+ return try decodedData.decrypt(cipher).arrayOfBytes()
|
|
|
}
|
|
|
+
|
|
|
}
|