From 4d4dbd73f022930f65208100702447f3f6c0b333 Mon Sep 17 00:00:00 2001 From: Felipe Sueto Date: Wed, 16 Mar 2022 14:55:22 -0300 Subject: [PATCH] Add README --- README.md | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 57bb8c8..9afdf48 100644 --- a/README.md +++ b/README.md @@ -1 +1,125 @@ -# ecdsa-swift \ No newline at end of file +## A lightweight and fast pure Swift ECDSA + +### Overview + +This is a pure Swift implementation of the Elliptic Curve Digital Signature Algorithm. It is compatible with Swift 5.3. It is also compatible with OpenSSL. It uses some elegant math such as Jacobian Coordinates to speed up the ECDSA on pure Swift. + +### Curves + +We currently support `secp256k1`, but it's super easy to add more curves to the project. Just add them on `Curve.swift` + +### Speed + +We ran a test on a MAC Pro i5 2017. The libraries were run 100 times and the averages displayed bellow were obtained: + +| Library | sign | verify | +| ------------------ |:-------------:| -------:| +| starkbank-ecdsa | 0.3ms | 1.1ms | + +### Sample Code + +How to sign a json message for [Stark Bank]: + +```swift +import starkbank + +// Generate privateKey from PEM string +let privateKey = try PrivateKey.fromPem(""" +-----BEGIN EC PARAMETERS----- +BgUrgQQACg== +-----END EC PARAMETERS----- +-----BEGIN EC PRIVATE KEY----- +MHQCAQEEIODvZuS34wFbt0X53+P5EnSj6tMjfVK01dD1dgDH02RzoAcGBSuBBAAK +oUQDQgAE/nvHu/SQQaos9TUljQsUuKI15Zr5SabPrbwtbfT/408rkVVzq8vAisbB +RmpeRREXj5aog/Mq8RrdYy75W9q/Ig== +-----END EC PRIVATE KEY----- +""") + +// Create message from json +let message = "This is a text message" + +let signature = Ecdsa.sign(message: message, privateKey: privateKey) + +// Generate Signature in base64. This result can be sent to Stark Bank in the request header as the Digital-Signature parameter. +print(signature.toBase64()) + +// To double check if the message matches the signature, do this: +let publicKey = privateKey.publicKey() + +print(Ecdsa.verify(message: message, signature: signature, publicKey: publicKey)) + +``` + +Simple use: + +```swift +import starkbank + +// Generate new Keys +let privateKey = try PrivateKey() +let publicKey = privateKey.publicKey() + +let message = "My test message" + +// Generate Signature +let signature = Ecdsa.sign(message: message, privateKey: privateKey) + +// To verify if the signature is valid +print(Ecdsa.verify(message: message, signature: signature, publicKey: publicKey)) +``` + +### OpenSSL + +This library is compatible with OpenSSL, so you can use it to generate keys: + +``` +openssl ecparam -name secp256k1 -genkey -out privateKey.pem +openssl ec -in privateKey.pem -pubout -out publicKey.pem +``` + +Create a message.txt file and sign it: + +``` +openssl dgst -sha256 -sign privateKey.pem -out signatureDer.txt message.txt +``` + +To verify, do this: + +```swift +import starkbank + +let publicKeyPem = try? NSString(contentsOfFile: NSString(string:"/path/to/your/public-key/publicKey.pem").expandingTildeInPath, encoding: String.Encoding.utf8.rawValue) +let signatureDer = try? NSString(contentsOfFile: NSString(string:"/path/to/your/signature/signatureDer.txt").expandingTildeInPath, encoding: String.Encoding.utf8.rawValue) +let message = try? NSString(contentsOfFile: NSString(string:"/path/to/your/message/message.txt").expandingTildeInPath, encoding: String.Encoding.utf8.rawValue) + +let publicKey = try PrivateKey.fromPem(privateKeyPem! as String) +let signature = try Signature.fromDer(signatureDer) + +print(Ecdsa.verify(message: message! as String, signature: signature, publicKey: publicKey)) +``` + +You can also verify it on terminal: + +``` +openssl dgst -sha256 -verify publicKey.pem -signature signatureDer.txt message.txt +``` + +NOTE: If you want to create a Digital Signature to use with [Stark Bank], you need to convert the binary signature to base64. + +``` +openssl base64 -in signatureDer.txt -out signatureBase64.txt +``` + +You can do the same with this library: + +```swift +import starkbank + +let signatureDer = try? NSString(contentsOfFile: NSString(string:"/path/to/your/signature/signatureDer.txt").expandingTildeInPath, encoding: String.Encoding.utf8.rawValue) + +let signature = try Signature.fromDer(signatureDer) + +print(signature.toBase64()) +``` + +[Stark Bank]: https://starkbank.com