From 9dfe3fd99cbd5dcb7c3ebbb600f5bf7cec065be2 Mon Sep 17 00:00:00 2001 From: Dima Date: Wed, 14 Feb 2018 16:45:00 +0300 Subject: [PATCH] sha1 for js and tests (#42) * sha1 for js and tests * test for jdk hashers, add js hasher implementation * typo --- .../scala/fluence/crypto/facade/Hash.scala | 7 +++++ .../fluence/crypto/hash/JsCryptoHasher.scala | 13 ++++++-- .../scala/fluence/crypto/EcdsaJSSpec.scala | 4 +-- .../scala/fluence/crypto/JSHashSpec.scala | 31 +++++++++++++++++++ .../fluence/crypto/hash/JdkCryptoHasher.scala | 1 + .../scala/fluence/crypto/JvmHashSpec.scala | 26 ++++++++++++++++ 6 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 js/src/test/scala/fluence/crypto/JSHashSpec.scala create mode 100644 jvm/src/test/scala/fluence/crypto/JvmHashSpec.scala diff --git a/js/src/main/scala/fluence/crypto/facade/Hash.scala b/js/src/main/scala/fluence/crypto/facade/Hash.scala index ac15737..6a569a5 100644 --- a/js/src/main/scala/fluence/crypto/facade/Hash.scala +++ b/js/src/main/scala/fluence/crypto/facade/Hash.scala @@ -30,3 +30,10 @@ class SHA256() extends js.Object { def update(msg: js.Array[Byte]): Unit = js.native def digest(enc: String): String = js.native } + +@js.native +@JSImport("hash.js", "sha1") +class SHA1() extends js.Object { + def update(msg: js.Array[Byte]): Unit = js.native + def digest(enc: String): String = js.native +} diff --git a/js/src/main/scala/fluence/crypto/hash/JsCryptoHasher.scala b/js/src/main/scala/fluence/crypto/hash/JsCryptoHasher.scala index d6f5083..81e226d 100644 --- a/js/src/main/scala/fluence/crypto/hash/JsCryptoHasher.scala +++ b/js/src/main/scala/fluence/crypto/hash/JsCryptoHasher.scala @@ -17,7 +17,7 @@ package fluence.crypto.hash -import fluence.crypto.facade.SHA256 +import fluence.crypto.facade.{SHA1, SHA256} import scodec.bits.ByteVector import scala.scalajs.js.JSConverters._ @@ -25,13 +25,22 @@ import scala.scalajs.js.JSConverters._ object JsCryptoHasher { lazy val Sha256: CryptoHasher[Array[Byte], Array[Byte]] = new CryptoHasher[Array[Byte], Array[Byte]] { - override def hash(msg1: Array[Byte]): Array[Byte] = { val sha256 = new SHA256() sha256.update(msg1.toJSArray) ByteVector.fromValidHex(sha256.digest("hex")).toArray } + override def hash(msg1: Array[Byte], msg2: Array[Byte]*): Array[Byte] = { + hash(msg1 ++ msg2.flatten) + } + } + lazy val Sha1: CryptoHasher[Array[Byte], Array[Byte]] = new CryptoHasher[Array[Byte], Array[Byte]] { + override def hash(msg1: Array[Byte]): Array[Byte] = { + val sha1 = new SHA1() + sha1.update(msg1.toJSArray) + ByteVector.fromValidHex(sha1.digest("hex")).toArray + } override def hash(msg1: Array[Byte], msg2: Array[Byte]*): Array[Byte] = { hash(msg1 ++ msg2.flatten) } diff --git a/js/src/test/scala/fluence/crypto/EcdsaJSSpec.scala b/js/src/test/scala/fluence/crypto/EcdsaJSSpec.scala index 00ba4b4..e7dd93f 100644 --- a/js/src/test/scala/fluence/crypto/EcdsaJSSpec.scala +++ b/js/src/test/scala/fluence/crypto/EcdsaJSSpec.scala @@ -20,12 +20,12 @@ package fluence.crypto import cats.data.EitherT import cats.instances.try_._ import fluence.crypto.algorithm.{ CryptoErr, EcdsaJS } -import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec } +import org.scalatest.{ Matchers, WordSpec } import scodec.bits.ByteVector import scala.util.{ Random, Try } -class EcdsaJSSpec extends WordSpec with Matchers with BeforeAndAfterAll { +class EcdsaJSSpec extends WordSpec with Matchers { def rndBytes(size: Int) = Random.nextString(10).getBytes def rndByteVector(size: Int) = ByteVector(rndBytes(size)) diff --git a/js/src/test/scala/fluence/crypto/JSHashSpec.scala b/js/src/test/scala/fluence/crypto/JSHashSpec.scala new file mode 100644 index 0000000..6ef1d13 --- /dev/null +++ b/js/src/test/scala/fluence/crypto/JSHashSpec.scala @@ -0,0 +1,31 @@ +package fluence.crypto + +import fluence.crypto.facade.{ SHA1, SHA256 } +import org.scalatest.{ Matchers, WordSpec } + +import scala.scalajs.js.JSConverters._ + +class JSHashSpec extends WordSpec with Matchers { + "js hasher" should { + //test values get from third-party hash services + "work with sha256" in { + val str = "sha256Tester" + val sha256TesterHex = "513c17f8cf6ba96ce412cc2ae82f68821e9a2c6ae7a2fb1f5e46d08c387c8e65" + + val hasher = new SHA256() + hasher.update(str.getBytes().toJSArray) + val hex = hasher.digest("hex") + hex shouldBe sha256TesterHex + } + + "work with sha1" in { + val str = "sha1Tester" + val sha1TesterHex = "879db20eabcecea7d4736a8bae5bc64564b76b2f" + + val hasher = new SHA1() + hasher.update(str.getBytes().toJSArray) + val hex = hasher.digest("hex") + hex shouldBe sha1TesterHex + } + } +} diff --git a/jvm/src/main/scala/fluence/crypto/hash/JdkCryptoHasher.scala b/jvm/src/main/scala/fluence/crypto/hash/JdkCryptoHasher.scala index 00622e5..f20d1c1 100644 --- a/jvm/src/main/scala/fluence/crypto/hash/JdkCryptoHasher.scala +++ b/jvm/src/main/scala/fluence/crypto/hash/JdkCryptoHasher.scala @@ -40,6 +40,7 @@ class JdkCryptoHasher(algorithm: String) extends CryptoHasher[Array[Byte], Array object JdkCryptoHasher { lazy val Sha256 = apply("SHA-256") + lazy val Sha1 = apply("SHA-1") def apply(algorithm: String): JdkCryptoHasher = new JdkCryptoHasher(algorithm) diff --git a/jvm/src/test/scala/fluence/crypto/JvmHashSpec.scala b/jvm/src/test/scala/fluence/crypto/JvmHashSpec.scala new file mode 100644 index 0000000..b99304d --- /dev/null +++ b/jvm/src/test/scala/fluence/crypto/JvmHashSpec.scala @@ -0,0 +1,26 @@ +package fluence.crypto + +import fluence.crypto.hash.JdkCryptoHasher +import org.scalatest.{ Matchers, WordSpec } +import scodec.bits.ByteVector + +class JvmHashSpec extends WordSpec with Matchers { + "jvm hasher" should { + //test values get from third-party hash services + "work with sha256" in { + val str = "sha256Tester" + val sha256TesterHex = "513c17f8cf6ba96ce412cc2ae82f68821e9a2c6ae7a2fb1f5e46d08c387c8e65" + + val hasher = JdkCryptoHasher.Sha256 + ByteVector(hasher.hash(str.getBytes())).toHex shouldBe sha256TesterHex + } + + "work with sha1" in { + val str = "sha1Tester" + val sha1TesterHex = "879db20eabcecea7d4736a8bae5bc64564b76b2f" + + val hasher = JdkCryptoHasher.Sha1 + ByteVector(hasher.hash(str.getBytes())).toHex shouldBe sha1TesterHex + } + } +}