-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbf378a
commit ea3efbc
Showing
5 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/org/arkecosystem/crypto/signature/ECDSASigner.java
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,11 @@ | ||
package org.arkecosystem.crypto.signature; | ||
|
||
import org.bitcoinj.core.ECKey; | ||
import org.bitcoinj.core.Sha256Hash; | ||
|
||
public class ECDSASigner implements Signer { | ||
@Override | ||
public byte[] sign(byte[] message, ECKey privateKey) { | ||
return privateKey.sign(Sha256Hash.wrap(message)).encodeToDER(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/arkecosystem/crypto/signature/ECDSAVerifier.java
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,15 @@ | ||
package org.arkecosystem.crypto.signature; | ||
|
||
import org.bitcoinj.core.ECKey; | ||
import org.bitcoinj.core.SignatureDecodeException; | ||
|
||
public class ECDSAVerifier implements Verifier { | ||
@Override | ||
public boolean verify(byte[] hash, ECKey key, byte[] signature) { | ||
try { | ||
return ECKey.verify(hash, signature, key.getPubKey()); | ||
} catch (SignatureDecodeException e) { | ||
return false; | ||
} | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
src/test/java/org/arkecosystem/crypto/signature/ECDSASignerTest.java
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,31 @@ | ||
package org.arkecosystem.crypto.signature; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.arkecosystem.crypto.Schnorr; | ||
import org.arkecosystem.crypto.identities.PrivateKey; | ||
import org.bitcoinj.core.ECKey; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ECDSASignerTest { | ||
|
||
private Signer signer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
signer = new ECDSASigner(); | ||
} | ||
|
||
@Test | ||
void sign() { | ||
String message = "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"; | ||
ECKey privateKey = PrivateKey.fromPassphrase("some passphrase"); | ||
String signature = | ||
"304402207856D22D9C1E146492117B61D83F0A8D2E046C5A75F471172689A0A0C26907C6022035A1368F38EC63BC9B7BEEC7BCE49BD2C1AE28179EE83EBE7FD9C17824E2514B"; | ||
|
||
assertEquals( | ||
signature, | ||
Schnorr.bytesToHex(signer.sign(Schnorr.hexStringToByteArray(message), privateKey))); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/org/arkecosystem/crypto/signature/ECDSAVerifierTest.java
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,44 @@ | ||
package org.arkecosystem.crypto.signature; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.arkecosystem.crypto.Schnorr; | ||
import org.arkecosystem.crypto.identities.PrivateKey; | ||
import org.bitcoinj.core.ECKey; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ECDSAVerifierTest { | ||
|
||
private Verifier verifier; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
verifier = new ECDSAVerifier(); | ||
} | ||
|
||
@Test | ||
void verify() { | ||
byte[] message = | ||
Schnorr.hexStringToByteArray( | ||
"243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"); | ||
ECKey privateKey = PrivateKey.fromPassphrase("some passphrase"); | ||
String signature = | ||
"304402207856D22D9C1E146492117B61D83F0A8D2E046C5A75F471172689A0A0C26907C6022035A1368F38EC63BC9B7BEEC7BCE49BD2C1AE28179EE83EBE7FD9C17824E2514B"; | ||
|
||
assertTrue(verifier.verify(message, privateKey, Schnorr.hexStringToByteArray(signature))); | ||
} | ||
|
||
@Test | ||
void verifyInvalid() { | ||
byte[] message = | ||
Schnorr.hexStringToByteArray( | ||
"243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"); | ||
ECKey privateKey = PrivateKey.fromPassphrase("some passphrase"); | ||
String signature = | ||
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"; | ||
|
||
assertFalse(verifier.verify(message, privateKey, Schnorr.hexStringToByteArray(signature))); | ||
} | ||
} |