To work with file encryption, create the HHFWFilesEncrypted application, which uses the framework methods:
Method name | Brief description |
initWithCredentials (_: host: environment: project: device:) | The method initializes the framework. |
auth (_: password:) | The method authenticates the user by login and password. |
getFile (_: fromServerPath: mountName: password: handler:) | The method downloads file from a mobile platform server. |
putFile (_: toServerPath: mountName: password: handler:) | The method uploads file to a mobile platform server. |
decryptFileToString(_: withPassword:) | The method decrypts file to a string. |
decryptFile(_: toFile: withPassword:) | The method descrypts file to a new file. |
decryptFileToData(_: withPassword:) | The method decrypts file to a byte buffer. |
The HHFWFilesEncrypted application includes one screen, the UITextField text box, the UITextView text view, and UIButton buttons:
Get file. Get file.
Put file. Send file.
Decrypt to string. It decrypts file to a string.
Decrypt to file. It decrypts file to a new file.
Decrypt to data. It decrypts file to byte buffer.
NOTE. When the application is initialized, authentication is executed, buttons and text boxes are not available. If the authentication is successful, buttons and text boxes are available, and the text view displays the text "Authentication success".
To execute the example:
Enter the name of the file.txt file and encryption key to the text boxes and click the Get File button.
As a result, the specified file is obtained from a mobile platform server, the file is encrypted with the specified key:
The file.txt file is written to Documents folder in the mobile application directory:
The file contents is encrypted:
Create a copy of the file.txt file in the Documents folder and rename it to file1.txt:
Enter name for the file1.txt file and encryption key to the text boxes and click the Put File button.
As a result, the specified file is sent to a mobile platform server:
Enter name for the file.txt file and encryption key to the text boxes and click the Decrypt to String button.
The text view displays the result of file decryption to a string:
Enter name for the file1.txt file and encryption key to the text boxes and click the Decrypt to File button:
The decriptedFile.txt: file will appear in the Documents folder in the mobile application directory:
The context of the decriptedFile.txt file corresponds with the decrypted contents of the file1.txt file:
Enter name for the file1.txt file and encryption key to the text boxes and click the Decrypt to Data button.
The text view displays file decryption result as byte buffer contents:
Application code:
import UIKit
class ViewController: UIViewController {
// Declare variables for user interface elements
@IBOutlet weak var fileNameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var getFileButton: UIButton!
@IBOutlet weak var putFileButton: UIButton!
@IBOutlet weak var decryptToStringButton: UIButton!
@IBOutlet weak var decryptToFileButton: UIButton!
@IBOutlet weak var decryptToDataButton: UIButton!
// Redetermine methods of display life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButtons()
self.resultTextView.text = "Authentication success"
}
}
// Determine methods for handling events initialized by the user from user interface
@IBAction func viewTapped(_ sender: UITapGestureRecognizer) {
self.fileNameTextField.resignFirstResponder()
}
@IBAction func getFilePressed(_ sender: UIButton) {
self.getFile { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
@IBAction func putFilePressed(_ sender: UIButton) {
self.putFile { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
@IBAction func decryptToStringPressed(_ sender: UIButton) {
self.decryptFileToString { (string) in
self.resultTextView.text = String(format: "%@", string)
}
}
@IBAction func decryptToFilePressed(_ sender: UIButton) {
self.decryptFileToFile {
self.resultTextView.text = String(format: "%@", "Decripted to file")
}
}
@IBAction func decryptToDataPressed(_ sender: UIButton) {
self.decryptFileToData { data in
self.resultTextView.text = String(format: "%@", data as NSData)
}
}
// Determine framework initialization framework
private func initializeFramework() {
let apiVersion: String = "v1"
let host: String = "http://testmasterfmp.fsight.cloud/"
let environment: String = "viktoriia_test"
let project: String = "project"
let device: String = (UIDevice.current.identifierForVendor?.uuidString)!
HHFWController.sharedInstance().initWithCredentials(
apiVersion,
host: host,
environment: environment,
project: project,
device: device
)
}
// Determine authentication method
private func auth(completion: @escaping ()->()) {
let username: String = "test"
let password: String = "test123"
HHFWController.sharedInstance().auth(username, password: password){ (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Auth success")
completion()
} else {
print("Auth error")
}
}
}
// Determine method for getting file
private func getFile(completion: @escaping (NSDictionary)->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
guard let password: String = self.passwordTextField.text else { return }
let mountName: String = "nfs4"
HHFWController.sharedInstance().getFile(fileName, fromServerPath: fileName, mountName: mountName, password: password) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Get file success")
completion(jsonDict)
} else {
print("Get file error")
}
}
}
// Determine method for sending file
private func putFile(completion: @escaping (NSDictionary)->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
guard let password: String = self.passwordTextField.text else { return }
let mountName: String = "nfs4"
HHFWController.sharedInstance().putFile(fileName, toServerPath: fileName, mountName: mountName, password: password) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Put file success")
completion(jsonDict)
} else {
print("Put file error")
}
}
}
// Determine method for decrypting file to string
private func decryptFileToString(completion: @escaping (String)->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
guard let password: String = self.passwordTextField.text else { return }
let string = HHFWController.sharedInstance().decryptFileToString(fileName, withPassword: password) ?? ""
completion(string)
}
// Determine method for file decryption to new file
private func decryptFileToFile(completion: @escaping ()->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
guard let password: String = self.passwordTextField.text else { return }
let destinationFile: String = "decriptedFile.txt"
HHFWController.sharedInstance().decryptFile(fileName, toFile: destinationFile, withPassword: password)
completion()
}
// Determine method for file decryption to byte buffer
private func decryptFileToData(completion: @escaping (Data)->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
guard let password: String = self.passwordTextField.text else { return }
let data = HHFWController.sharedInstance().decryptFileToData(fileName, withPassword: password)
completion(data)
}
// Determine methods for changing user interface display elements
private func deactivateButtons() {
self.getFileButton.isEnabled = false
self.putFileButton.isEnabled = false
self.decryptToStringButton.isEnabled = false
self.decryptToFileButton.isEnabled = false
self.decryptToDataButton.isEnabled = false
}
private func activateButtons() {
self.getFileButton.isEnabled = true
self.putFileButton.isEnabled = true
self.decryptToStringButton.isEnabled = true
self.decryptToFileButton.isEnabled = true
self.decryptToDataButton.isEnabled = true
}
}
See also: