sha1 for js and tests (#42)

* sha1 for js and tests

* test for jdk hashers, add js hasher implementation

* typo
This commit is contained in:
Dima 2018-02-14 16:45:00 +03:00 committed by GitHub
parent b01dffd779
commit 9dfe3fd99c
6 changed files with 78 additions and 4 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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))

View File

@ -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
}
}
}

View File

@ -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)

View File

@ -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
}
}
}