diff --git a/app/current/src/main/scala/io/scalajs/nodejs/crypto/Crypto.scala b/app/current/src/main/scala/io/scalajs/nodejs/crypto/Crypto.scala index 0c3f0ec02..5c7ca0217 100644 --- a/app/current/src/main/scala/io/scalajs/nodejs/crypto/Crypto.scala +++ b/app/current/src/main/scala/io/scalajs/nodejs/crypto/Crypto.scala @@ -111,6 +111,7 @@ trait Crypto extends js.Object { * will display the available digest algorithms. * @param algorithm the given algorithm (e.g. 'sha256', 'sha512') */ + @deprecated("Use CreateHashOptions instead.", "v0.12.0") def createHash(algorithm: String, options: TransformOptions): Hash = js.native def createHash(algorithm: String, options: CreateHashOptions = js.native): Hash = js.native @@ -152,6 +153,9 @@ trait Crypto extends js.Object { */ def createVerify(algorithm: String, options: WritableOptions = js.native): Verify = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) + def diffieHellman(options: DiffieHellmanOptions): Buffer = js.native + def generateKeyPair( `type`: String, options: GenerateKeyPairOptions, diff --git a/app/current/src/main/scala/io/scalajs/nodejs/crypto/DiffieHellmanOptions.scala b/app/current/src/main/scala/io/scalajs/nodejs/crypto/DiffieHellmanOptions.scala new file mode 100644 index 000000000..a07248844 --- /dev/null +++ b/app/current/src/main/scala/io/scalajs/nodejs/crypto/DiffieHellmanOptions.scala @@ -0,0 +1,21 @@ +package io.scalajs.nodejs.crypto + +import scala.scalajs.js + +trait DiffieHellmanOptions { + var privateKey: KeyObject + var publicKey: KeyObject +} + +object DiffieHellmanOptions { + def apply( + privateKey: KeyObject, + publicKey: KeyObject + ): DiffieHellmanOptions = { + val obj = js.Dynamic.literal( + "privateKey" -> privateKey, + "publicKey" -> publicKey + ) + obj.asInstanceOf[DiffieHellmanOptions] + } +} diff --git a/app/current/src/main/scala/io/scalajs/nodejs/crypto/Hash.scala b/app/current/src/main/scala/io/scalajs/nodejs/crypto/Hash.scala index 1e8edd817..f70b06bd4 100644 --- a/app/current/src/main/scala/io/scalajs/nodejs/crypto/Hash.scala +++ b/app/current/src/main/scala/io/scalajs/nodejs/crypto/Hash.scala @@ -1,5 +1,6 @@ package io.scalajs.nodejs.crypto +import com.thoughtworks.enableIf import io.scalajs.nodejs.buffer.Buffer import io.scalajs.nodejs.stream.{Transform, TransformOptions} @@ -17,6 +18,25 @@ import scala.scalajs.js @js.native sealed trait Hash extends Transform { + /** + * Creates a new Hash object that contains a deep copy of the internal state of the current Hash object. + * An error is thrown when an attempt is made to copy the Hash object after its hash.digest() method has been called. + * + * Added in: v13.1.0 + */ + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) + def copy(): Hash = js.native + + /** + * Creates a new Hash object that contains a deep copy of the internal state of the current Hash object. + * The optional options argument controls stream behavior. For XOF hash functions such as 'shake256', the outputLength option can be used to specify the desired output length in bytes. + * An error is thrown when an attempt is made to copy the Hash object after its hash.digest() method has been called. + * + * Added in: v13.1.0 + */ + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) + def copy(options: TransformOptions): Hash = js.native + /** * Calculates the digest of all of the data passed to be hashed (using the hash.update() method). The encoding can * be 'hex', 'binary' or 'base64'. If encoding is provided a string will be returned; otherwise a Buffer is returned. diff --git a/app/current/src/main/scala/io/scalajs/nodejs/crypto/KeyObject.scala b/app/current/src/main/scala/io/scalajs/nodejs/crypto/KeyObject.scala index 044156d6d..cebea9336 100644 --- a/app/current/src/main/scala/io/scalajs/nodejs/crypto/KeyObject.scala +++ b/app/current/src/main/scala/io/scalajs/nodejs/crypto/KeyObject.scala @@ -7,12 +7,28 @@ import scala.scalajs.js.typedarray.{DataView, TypedArray} import scala.scalajs.js.| @js.native +// TODO: Can be exposed as newable class when Node.js v10 dropped sealed trait KeyObject extends js.Object { def export(options: KeyObjectExportOptions): Buffer | String = js.native def export(): Buffer = js.native + /** + * For asymmetric keys, this property represents the type of the key. + * This property is undefined for unrecognized KeyObject types and symmetric keys. + */ + val asymmetricKeyType: js.UndefOr[String] = js.native + + /** + * For secret keys, this property represents the size of the key in bytes. + * This property is undefined for asymmetric keys. + */ val symmetricKeySize: js.UndefOr[Int] = js.native - val `type`: String = js.native + + /** + * Depending on the type of this KeyObject, this property is either 'secret' for secret (symmetric) keys, + * 'public' for public (asymmetric) keys or 'private' for private (asymmetric) keys. + */ + val `type`: String = js.native } class KeyObjectExportOptions( diff --git a/core/src/main/scala/io/scalajs/nodejs/internal/CompilerSwitches.scala b/core/src/main/scala/io/scalajs/nodejs/internal/CompilerSwitches.scala index e389ae2e3..a5a8ebc2b 100644 --- a/core/src/main/scala/io/scalajs/nodejs/internal/CompilerSwitches.scala +++ b/core/src/main/scala/io/scalajs/nodejs/internal/CompilerSwitches.scala @@ -12,4 +12,7 @@ object CompilerSwitches { final val isNodeJs12 = (c: whitebox.Context) => c.settings.exists(compare((major, _, _) => major == 12)) final val gteNodeJs12 = (c: whitebox.Context) => c.settings.exists(compare((major, _, _) => major >= 12)) + + final val isNodeJs14 = (c: whitebox.Context) => c.settings.exists(compare((major, _, _) => major == 14)) + final val gteNodeJs14 = (c: whitebox.Context) => c.settings.exists(compare((major, _, _) => major >= 14)) }