Skip to content

Commit

Permalink
Add key init from hex string. Better README usage examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
kukabi committed Apr 12, 2021
1 parent 757826d commit 9fabfaf
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 5 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,35 @@ end

## Usage

Please view [TariCryptoTests.swift](TariCryptoTests/TariCryptoTests.swift) for usage examples.
Sample program that illustrates message signing and verification:

```swift
import TariCrypto

// create a key pair from hex string representations, and sign & verify
let keyPairFromHex = TariKeyPair(
privateKey: TariKey(hexString: "A35F68A5054C9721874CDE2553B80F98338952783C6E1687A2E8454B0D5BA200")!,
publicKey: TariKey(hexString: "50C5624924CE9B806FE40A94CA20B029DB6AC6DA098B3E48B89CE2695B265E71")!
)
let message = "A random message."
let signedMessage = try keyPairFromHex.privateKey.signMessage(message: message)
var isVerified = try keyPairFromHex.publicKey.verifySignature(signedMessage: signedMessage)
print("Is first message verified? \(isVerified)")

// or, do the same with a random key pair, and sign & verify
let randomKeyPair = TariKeyPair.generateRandom()
let anotherMessage = "Another random message."
let anotherSignedMessage = try randomKeyPair.privateKey.signMessage(message: anotherMessage)
isVerified = try randomKeyPair.publicKey.verifySignature(signedMessage: anotherSignedMessage)
print("Is second message verified? \(isVerified)")
```

Program output:

```
Is first message verified? true
Is second message verified? true
```

## Testing

Expand Down
2 changes: 1 addition & 1 deletion TariCrypto.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Please visit the [GitHub repository](https://github.com/tari-labs/tari-crypto-io
spec.pod_target_xcconfig = { 'VALID_ARCHS' => 'arm64 x86_64', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
spec.user_target_xcconfig = { 'VALID_ARCHS' => 'arm64 x86_64', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }

spec.source = { :git => "https://github.com/tari-labs/tari-crypto-ios.git", :tag => "v#{spec.version}" }
spec.source = { :git => "https://github.com/tari-labs/tari-crypto-ios.git", :tag => "#{spec.version}" }
spec.source_files = "TariCrypto/**/*.{h,swift}"
spec.ios.vendored_library = 'TariCrypto/TariLib/libtari_crypto.a'

Expand Down
2 changes: 1 addition & 1 deletion TariCrypto.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@
"$(PROJECT_DIR)/TariCrypto/TariLib",
);
MARKETING_VERSION = 0.9.0;
ONLY_ACTIVE_ARCH = NO;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.tari.TariCrypto;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand Down
8 changes: 8 additions & 0 deletions TariCrypto/Model/TariKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public struct TariKey: CustomStringConvertible {
self.bytes = bytes
}

public init?(hexString: String) {
if let bytes = hexStringToBytes(hexString: hexString) {
self.bytes = bytes
} else {
return nil
}
}

public func signMessage(message: String) throws -> TariSignedMessage {
// FFI interface needs to change, this can be done cleaner
// (C arrays are imported in swift as arrays of tuples, not values)
Expand Down
22 changes: 22 additions & 0 deletions TariCrypto/TariUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ func bytesToHexString(bytes: ByteArray) -> String {
+ String(format: "%02X", bytes.28) + String(format: "%02X", bytes.29)
+ String(format: "%02X", bytes.30) + String(format: "%02X", bytes.31)
}


func hexStringToBytes(hexString: String) -> ByteArray? {
let stringArray = Array(hexString)
var data: Data = Data()
for i in stride(from: 0, to: hexString.count, by: 2) {
let pair: String = String(stringArray[i]) + String(stringArray[i+1])
if let byteNum = UInt8(pair, radix: 16) {
let byte = Data([byteNum])
data.append(byte)
}
else{
return nil
}
}
return (
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
data[16], data[17], data[18], data[19], data[20], data[21], data[22], data[23],
data[24], data[25], data[26], data[27], data[28], data[29], data[30], data[31]
)
}
13 changes: 12 additions & 1 deletion TariCryptoTests/TariCryptoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,23 @@ class TariCryptoTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testKeyFromHex() {
let keyPair = TariKeyPair.generateRandom()
let publicKeyFromHexString = "\(keyPair.publicKey)"
XCTAssertEqual("\(keyPair.publicKey)", "\(publicKeyFromHexString)")
}

func testSignatureVerification() throws {
let message = "A random message."
let keyPair = TariKeyPair.generateRandom()
print("Generated key pair: \(keyPair)")
let signedMessage = try keyPair.privateKey.signMessage(message: message)
print("Signed message: \(signedMessage)")
XCTAssertTrue(try keyPair.publicKey.verifySignature(signedMessage: signedMessage))
// also try to verify with hex-generated public key
let publicKeyFromHex = TariKey(hexString: "\(keyPair.publicKey)")
XCTAssertNotNil(publicKeyFromHex)
XCTAssertTrue(try publicKeyFromHex?.verifySignature(signedMessage: signedMessage) ?? false)
}

func testSignatureFailure() throws {
Expand All @@ -69,12 +79,13 @@ class TariCryptoTests: XCTestCase {
XCTAssertFalse(try keyPair.publicKey.verifySignature(signedMessage: signedMessage))
}

/*
func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

*/

}

0 comments on commit 9fabfaf

Please sign in to comment.