SCADE

CryptoApp

Using cryptographic algorithms cross platform on Android and iOS using Swift and CryptoSwift

Introduction

The use of cryptographic algorithms is spreading very quickly.

Thanks to CryptoSwift (https://cryptoswift.io/) the Swift community now has a very powerful library offering a wide variety of algorithms thanks to Marcin Krzyzanowski. Find CryptoSwift here https://github.com/krzyzanowskim/CryptoSwift

You can now run these algorithms on Android and iOS using the same codebase by simply referencing the CryptoSwift SPM package in SCADE.

Example Source Code

Please find the Source code here: https://github.com/scadedoc/UgExamples/tree/master/UgCryptoSwift See Installing examples for information on how to download the examples.

Reference the CryptoSwift SPM package

  1. Create new SCADE project
  2. Open build file and set URL to https://github.com/krzyzanowskim/CryptoSwift
  3. Set from field to 0.11.0
  4. Set linkedLibs to "CryptoSwift"

Done. You can now use CryptoSwift to build native mobile apps on Android and iOS in Swift

Use CryptoSwift

For example, In the load method, compute MD5 value like this:

import ScadeKit
import CryptoSwift

class MainPageAdapter: SCDLatticePageAdapter {

	// page adapter initialization
	override func load(_ path: String) {		
		super.load(path)
		
		// CryptoSwift Example 1
		let strVal = "Awesome = CryptoSwift and SCADE"
		let strValMd5 = strVal.md5()
		print(strValMd5)
 }
}

which results in

Adding UI

To complete this little demo, lets add a more compelling UI that allows to enter values and compute Md5 values on the fly. The layout of the app is as such:

If you are not familiar with visual designing a UI, please see our video. The resulting UI looks like this:

Adding UI Code

Now lets add the code to capture each text entry and set the lbMd5 label with the MD5 value of the text that was entered:

import ScadeKit
import CryptoSwift

class MainPageAdapter: SCDLatticePageAdapter {

	// page adapter initialization
	override func load(_ path: String) {		
		super.load(path)
		
		// CryptoSwift Example 1
		let strVal = "Awesome = CryptoSwift and SCADE"
		let strValMd5 = strVal.md5()
		print(strValMd5)
		
		// this is the shortest way of wiring the logic
		// there are more elegant ways
		
		// get reference of label called lbMd5
		let lbMD5 = self.page!.getWidgetByName("lbMd5") as! SCDWidgetsLabel
		
		// get reference to textbox, and add editFinished event
		if let tbText = self.page!.getWidgetByName("tbText") as? SCDWidgetsTextbox {
			tbText.onEditFinish.append(SCDWidgetsEditFinishEventHandler{
				(e:SCDWidgetsEditFinishEvent?) in
					let newValue = e!.newValue
					let md5 = newValue.md5() // Use CrytoSwift
					lbMD5.text = md5
			})
		}
	}
}

Compile and run on Android and iOS