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

Commit 2c40b0d

Browse files
author
exoego
committed
Overhaul crypto module
1 parent 4bf18c9 commit 2c40b0d

21 files changed

+700
-139
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
2525
| [child_process](https://nodejs.org/api/child_process.html) | :heavy_check_mark: |
2626
| [cluster](https://nodejs.org/api/cluster.html) | :heavy_check_mark: |
2727
| [console](https://nodejs.org/api/console.html) | :heavy_check_mark: |
28-
| [crypto](https://nodejs.org/api/crypto.html) | |
28+
| [crypto](https://nodejs.org/api/crypto.html) | :heavy_check_mark: |
2929
| [dgram](https://nodejs.org/api/dgram.html) | |
3030
| [dns](https://nodejs.org/api/dns.html) | |
3131
| [events](https://nodejs.org/api/events.html) | |

app/current/src/main/scala/io/scalajs/nodejs/buffer/Buffer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ class Buffer protected () extends Uint8Array( /* dummy to trick constructor */ -
529529
* @return a string according to the specified character encoding in encoding.
530530
* @example {{{ buf.toString([encoding[, start[, end]]]) }}}
531531
*/
532-
def toString(encoding: String = js.native, start: Int = js.native, end: Int = js.native): String = js.native
532+
def toString(encoding: String, start: Int = js.native, end: Int = js.native): String = js.native
533533

534534
/**
535535
* Re-encodes the given Buffer instance from one character encoding to another. Returns a new Buffer instance.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.scalajs.nodejs.crypto
2+
3+
import com.thoughtworks.enableIf
4+
import io.scalajs.nodejs.buffer.Buffer
5+
6+
import scala.scalajs.js
7+
import scala.scalajs.js.annotation.JSImport
8+
import scala.scalajs.js.|
9+
10+
@js.native
11+
@JSImport("crypto", "Certificate")
12+
class Certificate extends js.Object {
13+
def exportChallenge(spkac: String | BufferLike): Buffer = js.native
14+
def exportPublicKey(spkac: String | BufferLike, encoding: String = js.native): Buffer = js.native
15+
def verifySpkac(spkac: BufferLike): Boolean = js.native
16+
}
17+
18+
@js.native
19+
@JSImport("crypto", "Certificate")
20+
object Certificate extends js.Object {
21+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
22+
def exportChallenge(spkac: String | BufferLike): Buffer = js.native
23+
24+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
25+
def exportPublicKey(spkac: String | BufferLike, encoding: String = js.native): Buffer = js.native
26+
27+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
28+
def verifySpkac(spkac: BufferLike): Boolean = js.native
29+
}

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

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

33
import io.scalajs.nodejs.buffer.Buffer
4-
import io.scalajs.nodejs.stream.IDuplex
4+
import io.scalajs.nodejs.stream.{Transform, TransformOptions}
55

66
import scala.scalajs.js
77

@@ -16,7 +16,7 @@ import scala.scalajs.js
1616
* are not to be created directly using the new keyword.
1717
*/
1818
@js.native
19-
trait Cipher extends IDuplex {
19+
sealed trait Cipher extends Transform {
2020

2121
/**
2222
* Returns any remaining enciphered contents. If output_encoding parameter is one of 'binary', 'base64' or 'hex',
@@ -26,7 +26,7 @@ trait Cipher extends IDuplex {
2626
* Attempts to call cipher.final() more than once will result in an error being thrown.
2727
* @example cipher.final([output_encoding])
2828
*/
29-
def `final`(output_encoding: String): String = js.native
29+
def `final`(outputEncoding: String): String = js.native
3030

3131
/**
3232
* Returns any remaining enciphered contents. If output_encoding parameter is one of 'binary', 'base64' or 'hex',
@@ -43,7 +43,7 @@ trait Cipher extends IDuplex {
4343
* the value used for the additional authenticated data (AAD) input parameter.
4444
* @example cipher.setAAD(buffer)
4545
*/
46-
def setAAD(buffer: Buffer): Unit = js.native
46+
def setAAD(buffer: Buffer, options: SetAADOptions = js.native): Cipher = js.native
4747

4848
/**
4949
* When using an authenticated encryption mode (only GCM is currently supported), the cipher.getAuthTag() method
@@ -52,7 +52,7 @@ trait Cipher extends IDuplex {
5252
* The cipher.getAuthTag() method should only be called after encryption has been completed using the cipher.final() method.
5353
* @example cipher.getAuthTag()
5454
*/
55-
def getAuthTag(): js.Any = js.native
55+
def getAuthTag(): Buffer = js.native
5656

5757
/**
5858
* When using block encryption algorithms, the Cipher class will automatically add padding to the input data to the
@@ -65,7 +65,7 @@ trait Cipher extends IDuplex {
6565
* The cipher.setAutoPadding() method must be called before cipher.final().
6666
* @example cipher.setAutoPadding(auto_padding=true)
6767
*/
68-
def setAutoPadding(auto_padding: Boolean = js.native): Unit = js.native
68+
def setAutoPadding(auto_padding: Boolean = js.native): Cipher = js.native
6969

7070
/**
7171
* Updates the cipher with data. If the input_encoding argument is given, it's value must be one of 'utf8', 'ascii',
@@ -80,21 +80,13 @@ trait Cipher extends IDuplex {
8080
* cipher.update() after cipher.final() will result in an error being thrown.
8181
* @example cipher.update(data[, input_encoding][, output_encoding])
8282
*/
83-
def update(data: String, input_encoding: String, output_encoding: String = js.native): String = js.native
84-
85-
/**
86-
* Updates the cipher with data. If the input_encoding argument is given, it's value must be one of 'utf8', 'ascii',
87-
* or 'binary' and the data argument is a string using the specified encoding. If the input_encoding argument is not
88-
* given, data must be a Buffer. If data is a Buffer then input_encoding is ignored.
89-
*
90-
* The output_encoding specifies the output format of the enciphered data, and can be 'binary', 'base64' or 'hex'.
91-
* If the output_encoding is specified, a string using the specified encoding is returned. If no output_encoding is
92-
* provided, a Buffer is returned.
93-
*
94-
* The cipher.update() method can be called multiple times with new data until cipher.final() is called. Calling
95-
* cipher.update() after cipher.final() will result in an error being thrown.
96-
* @example cipher.update(data[, input_encoding][, output_encoding])
97-
*/
98-
def update(data: Buffer): Buffer = js.native
83+
def update(data: String, inputEncoding: String, outputEncoding: String): String = js.native
84+
def update(data: String, inputEncoding: String): Buffer = js.native
85+
def update(data: BufferLike): Buffer = js.native
9986

10087
}
88+
89+
class SetAADOptions(override val transform: js.UndefOr[js.Function] = js.undefined,
90+
override val flush: js.UndefOr[js.Function] = js.undefined,
91+
val plaintextLength: js.UndefOr[Int] = js.undefined)
92+
extends TransformOptions(transform, flush) {}

0 commit comments

Comments
 (0)