mirror of
https://github.com/fluencelabs/crypto
synced 2025-04-24 14:22:18 +00:00
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:
parent
b01dffd779
commit
9dfe3fd99c
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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))
|
||||
|
31
js/src/test/scala/fluence/crypto/JSHashSpec.scala
Normal file
31
js/src/test/scala/fluence/crypto/JSHashSpec.scala
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
||||
|
26
jvm/src/test/scala/fluence/crypto/JvmHashSpec.scala
Normal file
26
jvm/src/test/scala/fluence/crypto/JvmHashSpec.scala
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user