-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ed25519 * fix lint * fix
- Loading branch information
Showing
7 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Crypto | ||
import Foundation | ||
|
||
public struct Ed25519 { | ||
public let secretKey: Curve25519.Signing.PrivateKey | ||
|
||
public var publicKey: Data32 { | ||
Data32(secretKey.publicKey.rawRepresentation)! | ||
} | ||
|
||
public var privateKey: Data32 { | ||
Data32(secretKey.rawRepresentation)! | ||
} | ||
|
||
public init() { | ||
secretKey = Curve25519.Signing.PrivateKey() | ||
} | ||
|
||
public init?(privateKey: Data32) { | ||
guard let key = try? Curve25519.Signing.PrivateKey(rawRepresentation: privateKey.data) else { | ||
return nil | ||
} | ||
secretKey = key | ||
} | ||
|
||
public func sign(message: Data) throws -> Data64 { | ||
let signature = try secretKey.signature(for: message) | ||
return Data64(signature)! | ||
} | ||
|
||
public func verify(signature: Data64, message: Data, publicKey: Data32) -> Bool { | ||
guard let publicKey = try? Curve25519.Signing.PublicKey(rawRepresentation: publicKey.data) else { | ||
return false | ||
} | ||
return publicKey.isValidSignature(signature.data, for: message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import XCTest | ||
|
||
@testable import Utils | ||
|
||
final class Ed25519Tests: XCTestCase { | ||
func testEd25519Signature() throws { | ||
let ed25519 = Ed25519() | ||
let publicKey = ed25519.publicKey | ||
|
||
let message = Data("test".utf8) | ||
let signature = try ed25519.sign(message: message) | ||
XCTAssertTrue(ed25519.verify(signature: signature, message: message, publicKey: publicKey)) | ||
|
||
let invalidMessage = Data("tests".utf8) | ||
XCTAssertFalse(ed25519.verify(signature: signature, message: invalidMessage, publicKey: publicKey)) | ||
|
||
var invalidSignature = signature.data | ||
invalidSignature.replaceSubrange(0 ... 1, with: [10, 12]) | ||
XCTAssertFalse(ed25519.verify(signature: Data64(invalidSignature)!, message: message, publicKey: publicKey)) | ||
} | ||
} |