-
Notifications
You must be signed in to change notification settings - Fork 161
Add WebCryptoApi tests #667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
tests-shared/src/main/scala/org/scalajs/dom/tests/shared/WebCryptoApiTests.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package org.scalajs.dom.tests.shared | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
import org.scalajs.dom | ||
import org.scalajs.dom.tests.shared.AsyncTesting._ | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.typedarray._ | ||
|
||
trait WebCryptoApiTests { | ||
|
||
@Test final def getRandomValuesWork: Unit = { | ||
dom.webcrypto.getRandomValues(Array.ofDim[Byte](8).toTypedArray) | ||
} | ||
|
||
@Test final def generateAESEncryptionKeyWorks: AsyncResult = async { | ||
generateAESEncryptionKey() | ||
} | ||
|
||
@Test final def aesEncryptionDecryptionTest: AsyncResult = async { | ||
val data = "data" | ||
for { | ||
key <- generateAESEncryptionKey() | ||
iv = generateAESInitializationVector() | ||
encrypted <- aesEncrypt(key, iv, data) | ||
decrypted <- aesDecrypt(key, iv, encrypted) | ||
} yield assertEquals(data, decrypted) | ||
} | ||
|
||
@Test final def aesKeyDerivationWorks: AsyncResult = async { | ||
val derivationAlgorithm = "PBKDF2" | ||
val pbdkf2 = new dom.Pbkdf2Params { | ||
val name = derivationAlgorithm | ||
val salt = "salt".getBytes.toTypedArray.buffer | ||
val iterations = 100.toDouble | ||
val hash = "SHA-512" | ||
} | ||
val aesCtr = new dom.AesDerivedKeyParams { | ||
val name = "AES-GCM" | ||
val length = 256 | ||
} | ||
|
||
for { | ||
pbkdf2Key <- dom.crypto.subtle | ||
.importKey( | ||
dom.KeyFormat.raw, | ||
"password".getBytes.toTypedArray.buffer, | ||
derivationAlgorithm, | ||
false, | ||
js.Array(dom.KeyUsage.deriveKey, dom.KeyUsage.deriveBits) | ||
) | ||
.toFuture | ||
|
||
aesKey <- dom.crypto.subtle | ||
.deriveKey( | ||
pbdkf2, | ||
pbkdf2Key, | ||
aesCtr, | ||
true, | ||
js.Array(dom.KeyUsage.encrypt, dom.KeyUsage.decrypt) | ||
) | ||
.toFuture | ||
.map(_.asInstanceOf[dom.CryptoKey]) | ||
} yield assertNotNull(aesKey) | ||
} | ||
|
||
private def generateAESEncryptionKey() = { | ||
dom.crypto.subtle | ||
.generateKey( | ||
new dom.AesKeyAlgorithm { | ||
val name = "AES-GCM" | ||
val length = 256 | ||
}, | ||
true, | ||
js.Array(dom.KeyUsage.encrypt, dom.KeyUsage.decrypt) | ||
) | ||
.toFuture | ||
.map(_.asInstanceOf[dom.CryptoKey]) | ||
} | ||
|
||
private def generateAESInitializationVector() = { | ||
dom.webcrypto.getRandomValues(Array.ofDim[Byte](12).toTypedArray) | ||
} | ||
|
||
private def aesEncrypt(key: dom.CryptoKey, iv0: dom.BufferSource, data: String) = { | ||
dom.crypto.subtle | ||
.encrypt( | ||
new dom.AesGcmParams { | ||
val name = "AES-GCM" | ||
val iv = iv0 | ||
val tagLength = 128 | ||
val additionalData = "".getBytes.toTypedArray.buffer | ||
}, | ||
key, | ||
data.getBytes().toTypedArray | ||
) | ||
.toFuture | ||
.map(_.asInstanceOf[ArrayBuffer]) | ||
} | ||
|
||
private def aesDecrypt(key: dom.CryptoKey, iv0: dom.BufferSource, encrypted: dom.BufferSource) = { | ||
dom.crypto.subtle | ||
.decrypt( | ||
new dom.AesGcmParams { | ||
val name = "AES-GCM" | ||
val iv = iv0 | ||
val tagLength = 128 | ||
val additionalData = "".getBytes.toTypedArray.buffer | ||
}, | ||
key, | ||
encrypted | ||
) | ||
.toFuture | ||
.map(_.asInstanceOf[ArrayBuffer]) | ||
.map { buffer => | ||
val arr = Array.ofDim[Byte](buffer.byteLength) | ||
TypedArrayBuffer.wrap(buffer).get(arr) | ||
new String(arr, "UTF-8") | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.