Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

[crypto] Various updates for Node.js v14 #229

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
}
}
20 changes: 20 additions & 0 deletions app/current/src/main/scala/io/scalajs/nodejs/crypto/Hash.scala
Original file line number Diff line number Diff line change
@@ -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}

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}