Skip to content

Commit

Permalink
Update Swift, ObjC examples code to showcase keygen and passphrase AP…
Browse files Browse the repository at this point in the history
…Is (#688)

* updated swift examples code to showcase keygen and passphrase APIs

* imrpove changelog

* small fixes according to pr review

* update macos example

* update objc examples

* updated changelog

* Apply suggestions from code review

upd changelog

* Apply suggestions from code review

Co-authored-by: Alexei Lozovsky <[email protected]>

Co-authored-by: Alexei Lozovsky <[email protected]>
  • Loading branch information
vixentael and ilammy committed Jul 30, 2020
1 parent 1ca96de commit 850f9ab
Show file tree
Hide file tree
Showing 15 changed files with 784 additions and 326 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ _Code:_

- Minor dependency updates making the world a better place ([#680](https://github.com/cossacklabs/themis/pull/680)).

- **Swift**

- Updated Swift examples (iOS and macOS, Carthage and CocoaPods) to showcase usage of the newest Secure Cell API: generating symmetric keys and using Secure Cell with Passphrase ([#688](https://github.com/cossacklabs/themis/pull/688)).

- **Objective-C**

- Updated Objective-C examples (iOS and macOS, Carthage and CocoaPods) to showcase usage of the newest Secure Cell API: generating symmetric keys and using Secure Cell with Passphrase ([#688](https://github.com/cossacklabs/themis/pull/688)).


_Infrastructure:_

- Improved package split making `libthemis` thinner ([#678](https://github.com/cossacklabs/themis/pull/678)).
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/objc/iOS-Carthage/Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "cossacklabs/themis" "0.13.0"
github "cossacklabs/themis" "gothemis/v0.13.0"
github "krzyzanowskim/OpenSSL" "990bd88219da80d7a77289aeae245b3eb400d834"
321 changes: 207 additions & 114 deletions docs/examples/objc/iOS-Carthage/ThemisTest/AppDelegate.m

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

// Secure Cell:

[self runExampleSecureCellSealMode];
[self runExampleSecureCellTokenProtectMode];
[self runExampleSecureCellImprint];
NSData * fixedKey = [[NSData alloc]
initWithBase64EncodedString:@"UkVDMgAAAC13PCVZAKOczZXUpvkhsC+xvwWnv3CLmlG0Wzy8ZBMnT+2yx/dg"
options:NSDataBase64DecodingIgnoreUnknownCharacters];
[self runExampleSecureCellSealMode:fixedKey];
[self runExampleSecureCellTokenProtectMode:fixedKey];
[self runExampleSecureCellImprint:fixedKey];


NSData * generatedKey = TSGenerateSymmetricKey();
[self runExampleSecureCellSealMode:generatedKey];
[self runExampleSecureCellTokenProtectMode:generatedKey];
[self runExampleSecureCellImprint:generatedKey];


// Secure Comparator
Expand All @@ -56,19 +65,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

#pragma mark - Examples


- (NSData *)generateMasterKey {
NSString * masterKeyString = @"UkVDMgAAAC13PCVZAKOczZXUpvkhsC+xvwWnv3CLmlG0Wzy8ZBMnT+2yx/dg";
NSData * masterKeyData = [[NSData alloc] initWithBase64EncodedString:masterKeyString
options:NSDataBase64DecodingIgnoreUnknownCharacters];
return masterKeyData;
}


- (void)runExampleSecureCellSealMode {
- (void)runExampleSecureCellSealMode:(NSData *)masterKeyData {
NSLog(@"----------------- %s -----------------", sel_getName(_cmd));

NSData * masterKeyData = [self generateMasterKey];
TSCellSeal * cellSeal = [[TSCellSeal alloc] initWithKey:masterKeyData];

if (!cellSeal) {
Expand Down Expand Up @@ -105,10 +104,9 @@ - (void)runExampleSecureCellSealMode {
}


- (void)runExampleSecureCellTokenProtectMode {
- (void)runExampleSecureCellTokenProtectMode:(NSData *)masterKeyData {
NSLog(@"----------------- %s -----------------", sel_getName(_cmd));

NSData * masterKeyData = [self generateMasterKey];
TSCellToken * cellToken = [[TSCellToken alloc] initWithKey:masterKeyData];

if (!cellToken) {
Expand Down Expand Up @@ -144,10 +142,9 @@ - (void)runExampleSecureCellTokenProtectMode {
}


- (void)runExampleSecureCellImprint {
- (void)runExampleSecureCellImprint:(NSData *)masterKeyData {
NSLog(@"----------------- %s -----------------", sel_getName(_cmd));

NSData * masterKeyData = [self generateMasterKey];
TSCellContextImprint * contextImprint = [[TSCellContextImprint alloc] initWithKey:masterKeyData];

if (!contextImprint) {
Expand Down Expand Up @@ -182,8 +179,52 @@ - (void)runExampleSecureCellImprint {
NSLog(@"%s resultString = %@", sel_getName(_cmd), resultString);
}

- (void)runExampleSecureCellWithPassphrase {
NSLog(@"----------------- %s -----------------", sel_getName(_cmd));

TSCellSeal * cellSeal = [[TSCellSeal alloc] initWithPassphrase:@"We are the champions"];

if (!cellSeal) {
NSLog(@"%s Error occurred while initializing object cellSeal", sel_getName(_cmd));
return;
}

NSString * message = @"Your secret is safe with us!";
NSString * context = @"Many secrets are safe";
NSError * themisError;


// context is optional parameter and may be omitted
NSData * encryptedMessage = [cellSeal encrypt:[message dataUsingEncoding:NSUTF8StringEncoding]
context:[context dataUsingEncoding:NSUTF8StringEncoding]
error:&themisError];

if (themisError) {
NSLog(@"%s Error occurred while enrypting %@", sel_getName(_cmd), themisError);
return;
}
NSLog(@"encryptedMessage = %@", encryptedMessage);

NSData * decryptedMessage = [cellSeal decrypt:encryptedMessage
context:[context dataUsingEncoding:NSUTF8StringEncoding]
error:&themisError];
if (themisError) {
NSLog(@"%s Error occurred while decrypting %@", sel_getName(_cmd), themisError);
return;
}
NSString * resultString = [[NSString alloc] initWithData:decryptedMessage
encoding:NSUTF8StringEncoding];
NSLog(@"%s resultString = %@", sel_getName(_cmd), resultString);
}


- (void)runExampleGeneratingKeys {
[self runExampleGeneratingAsymKeys];
[self runExampleGeneratingSymmKeys];
}


- (void)runExampleGeneratingAsymKeys {
NSLog(@"----------------- %s -----------------", sel_getName(_cmd));

NSData * privateKey;
Expand Down Expand Up @@ -220,6 +261,40 @@ - (void)runExampleGeneratingKeys {
}


- (void)runExampleGeneratingSymmKeys {
NSLog(@"----------------- %s -----------------", sel_getName(_cmd));

NSData *masterKey = TSGenerateSymmetricKey();
TSCellSeal * cellSeal = [[TSCellSeal alloc] initWithKey:masterKey];

NSString * message = @"All your base are belong to us!";
NSString * context = @"For great justice";
NSError * themisError;

// context is optional parameter and may be omitted
NSData * encryptedMessage = [cellSeal encrypt:[message dataUsingEncoding:NSUTF8StringEncoding]
context:[context dataUsingEncoding:NSUTF8StringEncoding]
error:&themisError];

if (themisError) {
NSLog(@"%s Error occurred while enrypting %@", sel_getName(_cmd), themisError);
return;
}
NSLog(@"encryptedMessage = %@", encryptedMessage);

NSData * decryptedMessage = [cellSeal decrypt:encryptedMessage
context:[context dataUsingEncoding:NSUTF8StringEncoding]
error:&themisError];
if (themisError) {
NSLog(@"%s Error occurred while decrypting %@", sel_getName(_cmd), themisError);
return;
}
NSString * resultString = [[NSString alloc] initWithData:decryptedMessage
encoding:NSUTF8StringEncoding];
NSLog(@"%s resultString = %@", sel_getName(_cmd), resultString);
}


- (void)runExampleSecureMessageEncryptionDecryption {
NSLog(@"----------------- %s -----------------", sel_getName(_cmd));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="16097.2" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -15,12 +13,6 @@
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2015-2019 Cossack Labs. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye" userLabel="Copyright (c) 2015-2017 Cossack Labs. All rights reserved.">
<rect key="frame" x="20" y="439" width="440" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Themis Example ObjC" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<rect key="frame" x="20" y="139.5" width="440" height="43"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
Expand All @@ -32,9 +24,6 @@
<constraints>
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
<constraint firstAttribute="bottom" secondItem="8ie-xW-0ye" secondAttribute="bottom" constant="20" id="Kzo-t9-V3l"/>
<constraint firstItem="8ie-xW-0ye" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="MfP-vx-nX0"/>
<constraint firstAttribute="centerX" secondItem="8ie-xW-0ye" secondAttribute="centerX" id="ZEH-qu-HZ9"/>
<constraint firstItem="kId-c2-rCX" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="fvb-Df-36g"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/objc/macOS-Carthage/Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "cossacklabs/themis" "0.13.0"
github "cossacklabs/themis" "gothemis/v0.13.0"
github "krzyzanowskim/OpenSSL" "990bd88219da80d7a77289aeae245b3eb400d834"
Loading

0 comments on commit 850f9ab

Please sign in to comment.