mirror of
https://github.com/fluencelabs/crypto
synced 2025-05-27 22:11:18 +00:00
KademliaMVar.client and Kademlia.findNode (#49)
* KademliaMVar.client and Kademlia.findNode * scalariform formatting & tiny bugfix * sbt ops for travis? * constant b64seed in Contact * merge master * compilation bugfix * possible fix * review fixes
This commit is contained in:
parent
927c872631
commit
88d640dbf6
@ -19,6 +19,7 @@ package fluence.crypto.algorithm
|
|||||||
|
|
||||||
import cats.data.EitherT
|
import cats.data.EitherT
|
||||||
import cats.{ Monad, MonadError }
|
import cats.{ Monad, MonadError }
|
||||||
|
import fluence.crypto.SignAlgo
|
||||||
import fluence.crypto.facade.EC
|
import fluence.crypto.facade.EC
|
||||||
import fluence.crypto.hash.{ CryptoHasher, JsCryptoHasher }
|
import fluence.crypto.hash.{ CryptoHasher, JsCryptoHasher }
|
||||||
import fluence.crypto.keypair.KeyPair
|
import fluence.crypto.keypair.KeyPair
|
||||||
@ -82,5 +83,7 @@ class EcdsaJS(ec: EC, hasher: Option[CryptoHasher[Array[Byte], Array[Byte]]]) ex
|
|||||||
}
|
}
|
||||||
|
|
||||||
object EcdsaJS {
|
object EcdsaJS {
|
||||||
def ecdsa_secp256k1_sha256 = new EcdsaJS(new EC("secp256k1"), Some(JsCryptoHasher.Sha256))
|
val ecdsa_secp256k1_sha256 = new EcdsaJS(new EC("secp256k1"), Some(JsCryptoHasher.Sha256))
|
||||||
|
|
||||||
|
val signAlgo = new SignAlgo("ecdsa/secp256k1/sha256/js", ecdsa_secp256k1_sha256)
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ class EcdsaJSSpec extends WordSpec with Matchers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
"correctly work with signer and checker" in {
|
"correctly work with signer and checker" in {
|
||||||
val algo = new SignAlgo(EcdsaJS.ecdsa_secp256k1_sha256)
|
val algo = EcdsaJS.signAlgo
|
||||||
val keys = algo.generateKeyPair().extract
|
val keys = algo.generateKeyPair().extract
|
||||||
val signer = algo.signer(keys)
|
val signer = algo.signer(keys)
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ class EcdsaJSSpec extends WordSpec with Matchers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
"throw an errors on invalid data" in {
|
"throw an errors on invalid data" in {
|
||||||
val algo = new SignAlgo(EcdsaJS.ecdsa_secp256k1_sha256)
|
val algo = EcdsaJS.signAlgo
|
||||||
val keys = algo.generateKeyPair().extract
|
val keys = algo.generateKeyPair().extract
|
||||||
val signer = algo.signer(keys)
|
val signer = algo.signer(keys)
|
||||||
val data = rndByteVector(10)
|
val data = rndByteVector(10)
|
||||||
|
@ -23,6 +23,7 @@ import java.security.interfaces.ECPrivateKey
|
|||||||
|
|
||||||
import cats.data.EitherT
|
import cats.data.EitherT
|
||||||
import cats.Monad
|
import cats.Monad
|
||||||
|
import fluence.crypto.SignAlgo
|
||||||
import fluence.crypto.hash.{ CryptoHasher, JdkCryptoHasher }
|
import fluence.crypto.hash.{ CryptoHasher, JdkCryptoHasher }
|
||||||
import fluence.crypto.keypair.KeyPair
|
import fluence.crypto.keypair.KeyPair
|
||||||
import org.bouncycastle.jce.ECNamedCurveTable
|
import org.bouncycastle.jce.ECNamedCurveTable
|
||||||
@ -128,5 +129,7 @@ object Ecdsa {
|
|||||||
* `NONEwithECDSA with sha-256 hasher` Preferably the size of the key is greater than or equal to the digest algorithm
|
* `NONEwithECDSA with sha-256 hasher` Preferably the size of the key is greater than or equal to the digest algorithm
|
||||||
* don't use `SHA256WithECDSA` because of non-compatibility with javascript libraries
|
* don't use `SHA256WithECDSA` because of non-compatibility with javascript libraries
|
||||||
*/
|
*/
|
||||||
def ecdsa_secp256k1_sha256 = new Ecdsa("secp256k1", "NONEwithECDSA", Some(JdkCryptoHasher.Sha256))
|
val ecdsa_secp256k1_sha256 = new Ecdsa("secp256k1", "NONEwithECDSA", Some(JdkCryptoHasher.Sha256))
|
||||||
|
|
||||||
|
val signAlgo = new SignAlgo("ecdsa_secp256k1_sha256", ecdsa_secp256k1_sha256)
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ class SignatureSpec extends WordSpec with Matchers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
"correctly work with signer and checker" in {
|
"correctly work with signer and checker" in {
|
||||||
val algo = new SignAlgo(Ecdsa.ecdsa_secp256k1_sha256)
|
val algo = Ecdsa.signAlgo
|
||||||
val keys = algo.generateKeyPair().extract
|
val keys = algo.generateKeyPair().extract
|
||||||
val signer = algo.signer(keys)
|
val signer = algo.signer(keys)
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ class SignatureSpec extends WordSpec with Matchers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
"throw an errors on invalid data" in {
|
"throw an errors on invalid data" in {
|
||||||
val algo = new SignAlgo(Ecdsa.ecdsa_secp256k1_sha256)
|
val algo = Ecdsa.signAlgo
|
||||||
val keys = algo.generateKeyPair().extract
|
val keys = algo.generateKeyPair().extract
|
||||||
val signer = algo.signer(keys)
|
val signer = algo.signer(keys)
|
||||||
val data = rndByteVector(10)
|
val data = rndByteVector(10)
|
||||||
@ -84,7 +84,7 @@ class SignatureSpec extends WordSpec with Matchers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
"store and read key from file" in {
|
"store and read key from file" in {
|
||||||
val algo = new SignAlgo(Ecdsa.ecdsa_secp256k1_sha256)
|
val algo = Ecdsa.signAlgo
|
||||||
val keys = algo.generateKeyPair().extract
|
val keys = algo.generateKeyPair().extract
|
||||||
|
|
||||||
val keyFile = File.createTempFile("test", "")
|
val keyFile = File.createTempFile("test", "")
|
||||||
|
@ -28,9 +28,10 @@ import scala.language.higherKinds
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for generation keys, signers and checkers
|
* Class for generation keys, signers and checkers
|
||||||
|
* @param name Algo name for debugging
|
||||||
* @param algo implementation of sign alghoritms, e.g. ECDSA
|
* @param algo implementation of sign alghoritms, e.g. ECDSA
|
||||||
*/
|
*/
|
||||||
class SignAlgo(algo: KeyGenerator with SignatureFunctions) {
|
class SignAlgo(name: String, algo: KeyGenerator with SignatureFunctions) {
|
||||||
|
|
||||||
def generateKeyPair[F[_] : Monad](seed: Option[ByteVector] = None): EitherT[F, CryptoErr, KeyPair] =
|
def generateKeyPair[F[_] : Monad](seed: Option[ByteVector] = None): EitherT[F, CryptoErr, KeyPair] =
|
||||||
algo.generateKeyPair(seed.map(_.toArray))
|
algo.generateKeyPair(seed.map(_.toArray))
|
||||||
@ -38,13 +39,19 @@ class SignAlgo(algo: KeyGenerator with SignatureFunctions) {
|
|||||||
def signer(kp: KeyPair): Signer = new Signer {
|
def signer(kp: KeyPair): Signer = new Signer {
|
||||||
override def sign[F[_] : Monad](plain: ByteVector): EitherT[F, CryptoErr, Signature] = algo.sign(kp, plain)
|
override def sign[F[_] : Monad](plain: ByteVector): EitherT[F, CryptoErr, Signature] = algo.sign(kp, plain)
|
||||||
override def publicKey: KeyPair.Public = kp.publicKey
|
override def publicKey: KeyPair.Public = kp.publicKey
|
||||||
|
|
||||||
|
override def toString: String = s"Signer($name, ${kp.publicKey})"
|
||||||
}
|
}
|
||||||
|
|
||||||
def checker: SignatureChecker = new SignatureChecker {
|
def checker: SignatureChecker = new SignatureChecker {
|
||||||
override def check[F[_] : Monad](signature: Signature, plain: ByteVector): EitherT[F, CryptoErr, Unit] = algo.verify(signature, plain)
|
override def check[F[_] : Monad](signature: Signature, plain: ByteVector): EitherT[F, CryptoErr, Unit] = algo.verify(signature, plain)
|
||||||
|
|
||||||
|
override def toString: String = s"SignatureChecker($name)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override def toString: String = s"SignAlgo($name)"
|
||||||
}
|
}
|
||||||
|
|
||||||
object SignAlgo {
|
object SignAlgo {
|
||||||
val dumb = new SignAlgo(new DumbSign())
|
val dumb = new SignAlgo("dumb", new DumbSign())
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,9 @@ import scodec.bits.ByteVector
|
|||||||
|
|
||||||
import scala.language.higherKinds
|
import scala.language.higherKinds
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps public and private key, along with signing algorithm, to produce signatures
|
||||||
|
*/
|
||||||
trait Signer {
|
trait Signer {
|
||||||
def publicKey: KeyPair.Public
|
def publicKey: KeyPair.Public
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user