Skip to content

Commit

Permalink
Merge pull request #2 from vsgobbi/master
Browse files Browse the repository at this point in the history
ecdsa-java
  • Loading branch information
Rafael Stark authored May 23, 2019
2 parents 9984293 + 388a846 commit 8e803f4
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 33 deletions.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ nb-configuration.xml
##########################
## OS X
##########################
.DS_Store
.DS_Store

##########################
## Example Files
##########################
src/main/java/Generate*
message.txt
publicKey.pem
privateKey.pem
signatureBinary.txt
80 changes: 61 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,32 @@ We currently support `secp256k1`, but it's super easy to add more curves to the
How to use it:

```java
// Generate Keys
PrivateKey privateKey = new PrivateKey();
PublicKey publicKey = privateKey.publicKey();
import com.starkbank.ellipticcurve.PrivateKey;
import com.starkbank.ellipticcurve.PublicKey;
import com.starkbank.ellipticcurve.Signature;
import com.starkbank.ellipticcurve.Ecdsa;

String message = "My test message";

// Generate Signature
Signature signature = Ecsda.sign(message, privateKey);
public class GenerateKeys{

// Verify if signature is valid
System.out.println(Ecsda.verify(message, signature, publicKey));
```
public static void main(String[] args){
// Generate Keys
PrivateKey privateKey = new PrivateKey();
PublicKey publicKey = privateKey.publicKey();

String message = "Testing message";
// Generate Signature
Signature signature = Ecdsa.sign(message, privateKey);

// Verify if signature is valid
boolean verified = Ecdsa.verify(message, signature, publicKey) ;

// Return the signature verification status
System.out.println("Verified: " + verified);

}
}
```
### OpenSSL

This library is compatible with OpenSSL, so you can use it to generate keys:
Expand All @@ -50,14 +63,31 @@ openssl dgst -sha256 -sign privateKey.pem -out signatureBinary.txt message.txt
It's time to verify:

```java
String publicKeyPem = new String(Files.readAllBytes(Path.get("publicKey.pem")));
byte[] signatureBin = Files.readAllBytes(Path.get("signatureBinary.txt"));
String message = new String(Files.readAllBytes(Path.get("message.txt")));
import com.starkbank.ellipticcurve.Ecdsa;
import com.starkbank.ellipticcurve.PublicKey;
import com.starkbank.ellipticcurve.Signature;
import com.starkbank.ellipticcurve.utils.ByteString;
import com.starkbank.ellipticcurve.utils.File;


PublicKey publicKey = PublicKey.fromPem(publicKeyPem);
Signature signature = Signature.fromDer(signatureBin);
public class VerifyKeys {

System.out.println(Ecdsa.verify(message, signature, publicKey));
public static void main(String[] args){
// Read files
String publicKeyPem = File.read("publicKey.pem");
byte[] signatureBin = File.readBytes("signatureBinary.txt");
String message = File.read("message.txt");

ByteString byteString = new ByteString(signatureBin);

PublicKey publicKey = PublicKey.fromPem(publicKeyPem);
Signature signature = Signature.fromDer(byteString);

// Get verification status:
boolean verified = Ecdsa.verify(message, signature, publicKey);
System.out.println("Verification status: " + verified);
}
}
```

You can also verify it on terminal:
Expand All @@ -75,11 +105,21 @@ openssl base64 -in signatureBinary.txt -out signatureBase64.txt
With this library, you can do it:

```java
byte[] signatureBin = Files.readAllBytes(Path.get("signatureBinary.txt"));
import com.starkbank.ellipticcurve.utils.ByteString;
import com.starkbank.ellipticcurve.Signature;
import com.starkbank.ellipticcurve.utils.File;

Signature signature = Signature.fromDer(new ByteString(signatureBin));

System.out.println(signature.toBase64());
public class GenerateSignature {

public static void main(String[] args) {
// Load signature file
byte[] signaturteBin = File.readBytes("signatureBinary.txt");
Signature signature = Signature.fromDer(new ByteString(signaturteBin));
// Print signature
System.out.println(signature.toBase64());
}
}
```

[Stark Bank]: https://starkbank.com
Expand All @@ -96,4 +136,6 @@ System.out.println(signature.toBase64());
```

### Run all unit tests
gradle test
```shell
gradle test
```
2 changes: 1 addition & 1 deletion src/main/java/com/starkbank/ellipticcurve/Ecdsa.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.security.NoSuchAlgorithmException;


public final class Ecdsa {
public class Ecdsa {

public static Signature sign(String message, PrivateKey privateKey, MessageDigest hashfunc) {
byte[] hashMessage = hashfunc.digest(message.getBytes());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/starkbank/ellipticcurve/Math.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static Point multiply(Point p, BigInteger n, BigInteger N, BigInteger A,
* @param P Prime number in the module of the equation Y^2 = X^3 + A*X + B (mod P)
* @return Point that represents the sum of First and Second Point
*/

public static Point add(Point p, Point q, BigInteger A, BigInteger P) {
return fromJacobian(jacobianAdd(toJacobian(p), toJacobian(q), A, P), P);
}
Expand Down Expand Up @@ -66,6 +67,7 @@ public static BigInteger inv(BigInteger x, BigInteger n) {
* @return Point in Jacobian coordinates
*/
public static Point toJacobian(Point p) {

return new Point(p.x, p.y, BigInteger.ONE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ private final static byte[] getDecodabet( int options ) {


/** Defeats instantiation. */
private Base64(){}
public Base64(){}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
* Company: SFL LLC
* Created on 20-Jan-19
*
* @author Taron Petrosyan
*/

public class ByteString {
private byte[] bytes;

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/starkbank/ellipticcurve/utils/Der.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

import static com.starkbank.ellipticcurve.utils.BinaryAscii.*;

/**
* Created on 05-Jan-19
*
* @author Taron Petrosyan
*/

public class Der {

private Der() {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/starkbank/ellipticcurve/utils/File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.starkbank.ellipticcurve.utils;

import java.io.IOException;
import java.nio.file.*;

/*
*This class handles the fileinput types as filename string
*If using bytearray file as "signatureBinary.txt"
*Use the readByte method
**/
public class File {

public static String read(String fileName)
{
String content = "";
try
{
content = new String(Files.readAllBytes(Paths.get(fileName)));
}
catch (IOException e)
{
e.printStackTrace();
}
return content;
}

public static byte[] readBytes(String fileName)
{
byte[] content = null;
try
{
content = Files.readAllBytes(Paths.get(fileName));
}
catch (IOException e)
{
e.printStackTrace();
}

return content;
}

}

0 comments on commit 8e803f4

Please sign in to comment.