Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generating and verifying digital signatures #7

Merged
merged 40 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
17700bc
Added digital signature functions
paulw11 Apr 14, 2016
3be604a
Add tests for digital signature functions
paulw11 Apr 14, 2016
60f5377
Add digital signature functions
paulw11 Apr 14, 2016
10a4d07
Add CommonCrypto framework
paulw11 Apr 15, 2016
7a8c6f8
Add SHA1 digest to sign/verify
paulw11 Apr 15, 2016
5f14451
Update tests for digest verification
paulw11 Apr 15, 2016
f979ad2
Update test for sign/verify
paulw11 Apr 15, 2016
46721be
Change verification method signatures for compatibility with Objective-C
paulw11 Apr 15, 2016
8eff5ab
Update tests for new verification function signatures
paulw11 Apr 15, 2016
c83111c
Formatting
paulw11 Apr 17, 2016
98737a8
Formatting
paulw11 Apr 17, 2016
d7809b7
Include additional signature tests
paulw11 Apr 17, 2016
275e1fe
Formatting
paulw11 Apr 17, 2016
c76a818
Include SHA1 in test target
paulw11 Apr 17, 2016
2705858
Change signature for SHA1 verification function
paulw11 Apr 17, 2016
4c81248
Include additional tests
paulw11 Apr 17, 2016
dd88c28
Include SHA1 tests
paulw11 Apr 17, 2016
933187d
Added change detail for digital signature
paulw11 Apr 17, 2016
5fc1182
Add documentation
paulw11 Apr 17, 2016
b2a112e
Add change details
paulw11 Apr 17, 2016
e01236e
Reflect changed function signature for `verifySHA1SignatureData`
paulw11 Apr 17, 2016
4f94066
Added docs
paulw11 Apr 17, 2016
59bddfa
Return a `VerificationResult` that indicates whether the signature wa…
paulw11 Apr 17, 2016
0e3cdf7
Test `VerificationResult` that indicates whether the signature was v…
paulw11 Apr 17, 2016
ef8e5ed
Updated for `VerificationResult`
paulw11 Apr 17, 2016
addf66e
Added objective-C examples on sign/verify & usage of VerificationResult
paulw11 Apr 17, 2016
356443f
Added
paulw11 Apr 17, 2016
9eac2dc
Added documentation for VerificationResult
paulw11 Apr 17, 2016
d413cbd
Merge branch 'master' of github.com:paulw11/SwiftyRSA
paulw11 Apr 17, 2016
859948c
Merge github.com:TakeScoop/SwiftyRSA
paulw11 Apr 18, 2016
f41cdcd
Move change description under `master`
paulw11 Apr 18, 2016
3469e29
Remove docs
paulw11 Apr 18, 2016
e66ec43
Include result in verification examples
paulw11 Apr 18, 2016
6802609
Formatting
paulw11 Apr 18, 2016
5448fda
Formatting
paulw11 Apr 18, 2016
0bbe518
Update s.source_files to include all Swift sources
paulw11 Apr 18, 2016
c63ce89
Migrate SHA1 from Swift to Objective-C
paulw11 Apr 19, 2016
4dfaf5b
Remove delete files from project
paulw11 Apr 19, 2016
ff04942
include .m & .h files in sources
paulw11 Apr 19, 2016
1ec902e
Remove local SHA1 function
paulw11 Apr 19, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ SwiftyRSA Changelog

# [master]

- Added digital signature creation & verification support (https://github.com/TakeScoop/SwiftyRSA/pull/7)

# [0.2.1]

- Fixed compiler warnings for Carthage. [#8](https://github.com/TakeScoop/SwiftyRSA/issues/8)
Expand All @@ -29,4 +31,4 @@ Initial release.
[master]: https://github.com/TakeScoop/SwiftyRSA/compare/0.2.0...master
[0.2.1]: https://github.com/TakeScoop/SwiftyRSA/releases/tag/0.2.1
[0.2.0]: https://github.com/TakeScoop/SwiftyRSA/releases/tag/0.2.0
[0.1.0]: https://github.com/TakeScoop/SwiftyRSA/releases/tag/0.1.0
[0.1.0]: https://github.com/TakeScoop/SwiftyRSA/releases/tag/0.1.0
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,39 @@ let decryptedString = try! SwiftyRSA.decryptString(str, privateKeyPEM: pemString
let decryptedData = try! SwiftyRSA.decryptData(data, privateKeyPEM: pemString)
```

### Sign

SwiftyRSA can sign data with a private key. SwiftyRSA will calculate an SHA1 digest
of the supplied `String`/`NSData` and use this to generate the digital signature.

```
// String
let signatureString = try! SwitfyRSA.signString(str, privateKeyPEM: pemString)

// Data
let signatureData = try! SwiftyRSA.signData(data, privateKeyPEM: pemString)
```

## Verify

SwiftyRSA can verify digital signatures with a public key. SwiftyRSA will calculate
an SHA1 digest of the supplied `String`/`NSData` and use this to verify the digital
signature.

```
// String
let verificationResult = try! SwitfyRSA.verifySignatureString(str, signature: sigString, publicKeyPEM: pemString)
if (verificationResult) {
// verification was successful
}

// Data
let verificationResult = try! SwitfyRSA.verifySignatureData(data, signature: sigData, publicKeyPEM: String)
if (verificationResult) {
// verification was successful
}
```

Advanced Usage
--------------

Expand Down Expand Up @@ -105,6 +138,18 @@ let decryptedString = try! rsa.decryptString(str, privateKey: privKey)
let decryptedData = try! rsa.decryptData(data, privateKey: privKey)
```

### Sign or verify an SHA1 digest

```
let rsa = SwiftyRSA()
let digestSignature = try! rsa.signSHA1Digest(digest, privateKey: privKey)

let verificationResult = try! rsa.verifySHA1SignatureData(digest, signature: digestSignature, publicKey: pubKey)
if (verificationResult) {
// verification was successful
}
```

### Use with Objective-C

```
Expand All @@ -122,6 +167,13 @@ NSString* privString = [NSString stringWithContentsOfFile:privPath encoding:NSUT

NSString* encrypted = [SwiftyRSA encryptString:str publicKeyPEM:pubString padding:kSecPaddingPKCS1 error:nil];
NSString* decrypted = [SwiftyRSA decryptString:encrypted privateKeyPEM:privString padding:kSecPaddingPKCS1 error:nil];

NSString* signature = [SwiftyRSA signString:str] privateKeyPEM:privString error:&error];
VerificationResult* result = [SwiftyRSA verifySignatureString:str signature:signature publicKeyDER:pubData error:&error];
if (result.boolValue) {
// verification was successful
}

```

Under the hood
Expand Down
2 changes: 1 addition & 1 deletion SwiftyRSA.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Pod::Spec.new do |s|
s.author = { "Scoop" => "ops@takescoop.com" }

s.source = { :git => "https://github.com/TakeScoop/SwiftyRSA.git", :tag => "0.2.1" }
s.source_files = "SwiftyRSA/SwiftyRSA.swift"
s.source_files = "SwiftyRSA/*.{swift,m,h}"
s.framework = "Security"

s.requires_arc = true
Expand Down
10 changes: 9 additions & 1 deletion SwiftyRSA.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
BB8460AE1CC608F6006F802C /* NSData+SHA1.h in Headers */ = {isa = PBXBuildFile; fileRef = BB8460AC1CC608F6006F802C /* NSData+SHA1.h */; settings = {ATTRIBUTES = (Public, ); }; };
BB8460AF1CC608F6006F802C /* NSData+SHA1.m in Sources */ = {isa = PBXBuildFile; fileRef = BB8460AD1CC608F6006F802C /* NSData+SHA1.m */; };
C01F96141C5AC3E300F232AC /* SwiftyRSAObjcTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C01F96131C5AC3E300F232AC /* SwiftyRSAObjcTests.m */; };
C03D827C1B45E649008711CF /* SwiftyRSA.h in Headers */ = {isa = PBXBuildFile; fileRef = C03D827B1B45E649008711CF /* SwiftyRSA.h */; settings = {ATTRIBUTES = (Public, ); }; };
C03D82821B45E649008711CF /* SwiftyRSA.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C03D82761B45E649008711CF /* SwiftyRSA.framework */; };
Expand All @@ -31,6 +33,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
BB8460AC1CC608F6006F802C /* NSData+SHA1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+SHA1.h"; sourceTree = "<group>"; };
BB8460AD1CC608F6006F802C /* NSData+SHA1.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+SHA1.m"; sourceTree = "<group>"; };
C01F96131C5AC3E300F232AC /* SwiftyRSAObjcTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SwiftyRSAObjcTests.m; sourceTree = "<group>"; };
C03D82761B45E649008711CF /* SwiftyRSA.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftyRSA.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C03D827A1B45E649008711CF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -87,6 +91,8 @@
C03D82781B45E649008711CF /* SwiftyRSA */ = {
isa = PBXGroup;
children = (
BB8460AC1CC608F6006F802C /* NSData+SHA1.h */,
BB8460AD1CC608F6006F802C /* NSData+SHA1.m */,
C03D827B1B45E649008711CF /* SwiftyRSA.h */,
C03D82921B45E663008711CF /* SwiftyRSA.swift */,
C03D82791B45E649008711CF /* Supporting Files */,
Expand Down Expand Up @@ -141,6 +147,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
BB8460AE1CC608F6006F802C /* NSData+SHA1.h in Headers */,
C03D827C1B45E649008711CF /* SwiftyRSA.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -190,7 +197,7 @@
C03D826D1B45E649008711CF /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = Scoop;
TargetAttributes = {
Expand Down Expand Up @@ -247,6 +254,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
BB8460AF1CC608F6006F802C /* NSData+SHA1.m in Sources */,
C03D82931B45E663008711CF /* SwiftyRSA.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
15 changes: 15 additions & 0 deletions SwiftyRSA/NSData+SHA1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSData_SHA1.h
// SwiftyRSA
//
// Created by Paul Wilkinson on 19/04/2016.
// Copyright © 2016 Scoop. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSData (NSData_SHA1)

- (nonnull NSData*) SHA1;

@end
22 changes: 22 additions & 0 deletions SwiftyRSA/NSData+SHA1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// NSData_SHA1.h
// SwiftyRSA
//
// Created by Paul Wilkinson on 19/04/2016.
// Copyright © 2016 Scoop. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>

@implementation NSData (NSData_SHA1)

- (nonnull NSData*) SHA1 {
unsigned int outputLength = CC_SHA1_DIGEST_LENGTH;
unsigned char output[outputLength];

CC_SHA1(self.bytes, (unsigned int) self.length, output);
return [NSData dataWithBytes:output length:outputLength];
}

@end
2 changes: 1 addition & 1 deletion SwiftyRSA/SwiftyRSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ FOUNDATION_EXPORT const unsigned char SwiftyRSAVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <SwiftyRSA/PublicHeader.h>


#import "NSData+SHA1.h"
Loading