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

Commit cf21d55

Browse files
authored
Merge pull request #229 from exoego/node14-crypto
[crypto] Various updates for Node.js v14
2 parents 577b8e9 + 7a216c7 commit cf21d55

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

app/current/src/main/scala/io/scalajs/nodejs/crypto/Crypto.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ trait Crypto extends js.Object {
111111
* will display the available digest algorithms.
112112
* @param algorithm the given algorithm (e.g. 'sha256', 'sha512')
113113
*/
114+
@deprecated("Use CreateHashOptions instead.", "v0.12.0")
114115
def createHash(algorithm: String, options: TransformOptions): Hash = js.native
115116
def createHash(algorithm: String, options: CreateHashOptions = js.native): Hash = js.native
116117

@@ -152,6 +153,9 @@ trait Crypto extends js.Object {
152153
*/
153154
def createVerify(algorithm: String, options: WritableOptions = js.native): Verify = js.native
154155

156+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
157+
def diffieHellman(options: DiffieHellmanOptions): Buffer = js.native
158+
155159
def generateKeyPair(
156160
`type`: String,
157161
options: GenerateKeyPairOptions,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.scalajs.nodejs.crypto
2+
3+
import scala.scalajs.js
4+
5+
trait DiffieHellmanOptions {
6+
var privateKey: KeyObject
7+
var publicKey: KeyObject
8+
}
9+
10+
object DiffieHellmanOptions {
11+
def apply(
12+
privateKey: KeyObject,
13+
publicKey: KeyObject
14+
): DiffieHellmanOptions = {
15+
val obj = js.Dynamic.literal(
16+
"privateKey" -> privateKey,
17+
"publicKey" -> publicKey
18+
)
19+
obj.asInstanceOf[DiffieHellmanOptions]
20+
}
21+
}

app/current/src/main/scala/io/scalajs/nodejs/crypto/Hash.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.scalajs.nodejs.crypto
22

3+
import com.thoughtworks.enableIf
34
import io.scalajs.nodejs.buffer.Buffer
45
import io.scalajs.nodejs.stream.{Transform, TransformOptions}
56

@@ -17,6 +18,25 @@ import scala.scalajs.js
1718
@js.native
1819
sealed trait Hash extends Transform {
1920

21+
/**
22+
* Creates a new Hash object that contains a deep copy of the internal state of the current Hash object.
23+
* An error is thrown when an attempt is made to copy the Hash object after its hash.digest() method has been called.
24+
*
25+
* Added in: v13.1.0
26+
*/
27+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
28+
def copy(): Hash = js.native
29+
30+
/**
31+
* Creates a new Hash object that contains a deep copy of the internal state of the current Hash object.
32+
* 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.
33+
* An error is thrown when an attempt is made to copy the Hash object after its hash.digest() method has been called.
34+
*
35+
* Added in: v13.1.0
36+
*/
37+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
38+
def copy(options: TransformOptions): Hash = js.native
39+
2040
/**
2141
* Calculates the digest of all of the data passed to be hashed (using the hash.update() method). The encoding can
2242
* be 'hex', 'binary' or 'base64'. If encoding is provided a string will be returned; otherwise a Buffer is returned.

app/current/src/main/scala/io/scalajs/nodejs/crypto/KeyObject.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,28 @@ import scala.scalajs.js.typedarray.{DataView, TypedArray}
77
import scala.scalajs.js.|
88

99
@js.native
10+
// TODO: Can be exposed as newable class when Node.js v10 dropped
1011
sealed trait KeyObject extends js.Object {
1112
def export(options: KeyObjectExportOptions): Buffer | String = js.native
1213
def export(): Buffer = js.native
1314

15+
/**
16+
* For asymmetric keys, this property represents the type of the key.
17+
* This property is undefined for unrecognized KeyObject types and symmetric keys.
18+
*/
19+
val asymmetricKeyType: js.UndefOr[String] = js.native
20+
21+
/**
22+
* For secret keys, this property represents the size of the key in bytes.
23+
* This property is undefined for asymmetric keys.
24+
*/
1425
val symmetricKeySize: js.UndefOr[Int] = js.native
15-
val `type`: String = js.native
26+
27+
/**
28+
* Depending on the type of this KeyObject, this property is either 'secret' for secret (symmetric) keys,
29+
* 'public' for public (asymmetric) keys or 'private' for private (asymmetric) keys.
30+
*/
31+
val `type`: String = js.native
1632
}
1733

1834
class KeyObjectExportOptions(

core/src/main/scala/io/scalajs/nodejs/internal/CompilerSwitches.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ object CompilerSwitches {
1212

1313
final val isNodeJs12 = (c: whitebox.Context) => c.settings.exists(compare((major, _, _) => major == 12))
1414
final val gteNodeJs12 = (c: whitebox.Context) => c.settings.exists(compare((major, _, _) => major >= 12))
15+
16+
final val isNodeJs14 = (c: whitebox.Context) => c.settings.exists(compare((major, _, _) => major == 14))
17+
final val gteNodeJs14 = (c: whitebox.Context) => c.settings.exists(compare((major, _, _) => major >= 14))
1518
}

0 commit comments

Comments
 (0)