diff --git a/README.md b/README.md index 924b94f..7e6debb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/TariCrypto.podspec b/TariCrypto.podspec index 1697240..18dc0b4 100644 --- a/TariCrypto.podspec +++ b/TariCrypto.podspec @@ -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' diff --git a/TariCrypto.xcodeproj/project.pbxproj b/TariCrypto.xcodeproj/project.pbxproj index f7befe1..9aa44ce 100644 --- a/TariCrypto.xcodeproj/project.pbxproj +++ b/TariCrypto.xcodeproj/project.pbxproj @@ -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; diff --git a/TariCrypto.xcodeproj/xcshareddata/xcschemes/TariCrypto.xcscheme b/TariCrypto.xcodeproj/xcshareddata/xcschemes/TariCrypto.xcscheme index 94e8670..f61948b 100644 --- a/TariCrypto.xcodeproj/xcshareddata/xcschemes/TariCrypto.xcscheme +++ b/TariCrypto.xcodeproj/xcshareddata/xcschemes/TariCrypto.xcscheme @@ -41,7 +41,7 @@ TariSignedMessage { // FFI interface needs to change, this can be done cleaner // (C arrays are imported in swift as arrays of tuples, not values) diff --git a/TariCrypto/TariUtil.swift b/TariCrypto/TariUtil.swift index 24e19f9..e23e9aa 100644 --- a/TariCrypto/TariUtil.swift +++ b/TariCrypto/TariUtil.swift @@ -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] + ) +} diff --git a/TariCryptoTests/TariCryptoTests.swift b/TariCryptoTests/TariCryptoTests.swift index accc4be..fda9c58 100644 --- a/TariCryptoTests/TariCryptoTests.swift +++ b/TariCryptoTests/TariCryptoTests.swift @@ -44,6 +44,12 @@ 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() @@ -51,6 +57,10 @@ class TariCryptoTests: XCTestCase { 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 { @@ -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. } } - + */ }