From 64f12eb6093192f2868b3ce4cd7b398d5ff68129 Mon Sep 17 00:00:00 2001 From: Dima Date: Mon, 15 Jul 2019 14:34:54 +0300 Subject: [PATCH] Fix hash for ed25119 (#12) * no hasher for default ed25519 * support interop buffers --- build.sbt | 2 +- .../fluence/crypto/CryptoJsHelpers.scala | 2 +- .../scala/fluence/crypto/eddsa/Ed25519.scala | 5 ++-- .../scala/fluence/crypto/facade/Buffer.scala | 27 +++++++++++++++++++ 4 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 hashsign/js/src/main/scala/fluence/crypto/facade/Buffer.scala diff --git a/build.sbt b/build.sbt index 90b3711..6353931 100644 --- a/build.sbt +++ b/build.sbt @@ -14,7 +14,7 @@ val scalaV = scalaVersion := "2.12.8" val commons = Seq( scalaV, - version := "0.0.10", + version := "0.0.15", fork in Test := true, parallelExecution in Test := false, organization := "one.fluence", diff --git a/hashsign/js/src/main/scala/fluence/crypto/CryptoJsHelpers.scala b/hashsign/js/src/main/scala/fluence/crypto/CryptoJsHelpers.scala index 3341580..b0792e4 100644 --- a/hashsign/js/src/main/scala/fluence/crypto/CryptoJsHelpers.scala +++ b/hashsign/js/src/main/scala/fluence/crypto/CryptoJsHelpers.scala @@ -24,6 +24,6 @@ import scala.language.higherKinds object CryptoJsHelpers { implicit class ByteVectorOp(bv: ByteVector) { - def toJsBuffer: Buffer = Buffer.from(bv.toHex, "hex") + def toJsBuffer: Buffer = new Buffer(ByteVector(bv.toArray).toHex, "hex") } } diff --git a/hashsign/js/src/main/scala/fluence/crypto/eddsa/Ed25519.scala b/hashsign/js/src/main/scala/fluence/crypto/eddsa/Ed25519.scala index f157b4d..61739c8 100644 --- a/hashsign/js/src/main/scala/fluence/crypto/eddsa/Ed25519.scala +++ b/hashsign/js/src/main/scala/fluence/crypto/eddsa/Ed25519.scala @@ -22,9 +22,8 @@ import cats.data.EitherT import fluence.crypto.CryptoError.nonFatalHandling import fluence.crypto.facade.ed25519.Supercop import fluence.crypto.hash.JsCryptoHasher -import fluence.crypto.{Crypto, CryptoError, KeyPair, CryptoJsHelpers} +import fluence.crypto.{Crypto, CryptoError, CryptoJsHelpers, KeyPair} import fluence.crypto.signature.{SignAlgo, Signature, SignatureChecker, Signer} -import io.scalajs.nodejs.buffer.Buffer import scodec.bits.ByteVector import scala.language.higherKinds @@ -38,7 +37,7 @@ class Ed25519(hasher: Option[Crypto.Hasher[Array[Byte], Array[Byte]]]) { hash ← JsCryptoHasher.hash(message, hasher) sign ← nonFatalHandling { Supercop.sign( - Buffer.from(ByteVector(hash).toHex, "hex"), + ByteVector(hash).toJsBuffer, keyPair.publicKey.value.toJsBuffer, keyPair.secretKey.value.toJsBuffer ) diff --git a/hashsign/js/src/main/scala/fluence/crypto/facade/Buffer.scala b/hashsign/js/src/main/scala/fluence/crypto/facade/Buffer.scala new file mode 100644 index 0000000..a813b41 --- /dev/null +++ b/hashsign/js/src/main/scala/fluence/crypto/facade/Buffer.scala @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2017 Fluence Labs Limited + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package fluence.crypto.facade + +import scala.scalajs.js +import scala.scalajs.js.annotation.JSGlobal + +@js.native +@JSGlobal("Buffer") +class Buffer(arr: js.Array[Byte]) extends js.Object { + def toString(enc: String): String = js.native +}