Skip to content

Commit 2e79577

Browse files
duonglaiquangrbri
authored andcommitted
SubtleCrypto: add CryptoKey
1 parent e7d3984 commit 2e79577

1 file changed

Lines changed: 108 additions & 1 deletion

File tree

src/main/java/org/htmlunit/javascript/host/crypto/CryptoKey.java

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,131 @@
1414
*/
1515
package org.htmlunit.javascript.host.crypto;
1616

17+
import java.security.Key;
18+
import java.security.PrivateKey;
19+
import java.security.PublicKey;
20+
import java.util.Collection;
21+
import java.util.LinkedHashSet;
22+
import java.util.Objects;
23+
import java.util.Set;
24+
25+
import javax.crypto.SecretKey;
26+
27+
import org.htmlunit.corejs.javascript.Scriptable;
1728
import org.htmlunit.javascript.HtmlUnitScriptable;
29+
import org.htmlunit.javascript.JavaScriptEngine;
1830
import org.htmlunit.javascript.configuration.JsxClass;
1931
import org.htmlunit.javascript.configuration.JsxConstructor;
32+
import org.htmlunit.javascript.configuration.JsxGetter;
33+
import org.htmlunit.javascript.host.Window;
2034

2135
/**
2236
* A JavaScript object for {@code CryptoKey}.
2337
*
38+
* @see <a href="https://w3c.github.io/webcrypto/#dfn-CryptoKey">CryptoKey</a>
39+
*
2440
* @author Ahmed Ashour
2541
* @author Ronald Brill
42+
* @author Lai Quang Duong
2643
*/
2744
@JsxClass
2845
public class CryptoKey extends HtmlUnitScriptable {
2946

47+
private Key internalKey_;
48+
private String type_;
49+
private boolean isExtractable_;
50+
private Scriptable algorithm_;
51+
private Set<String> usages_;
52+
3053
/**
3154
* JavaScript constructor.
3255
*/
3356
@JsxConstructor
3457
public void jsConstructor() {
35-
// nothing to do
58+
throw JavaScriptEngine.typeErrorIllegalConstructor();
59+
}
60+
61+
/**
62+
* Creates a properly scoped CryptoKey from the given parameters.
63+
*
64+
* @param scope the JS scope
65+
* @param internalKey the Java key (SecretKey, PublicKey, or PrivateKey)
66+
* @param isExtractable whether the key can be exported
67+
* @param algorithm the JS algorithm descriptor object
68+
* @param usages the permitted key usages
69+
* @return the new CryptoKey
70+
*/
71+
static CryptoKey create(final Scriptable scope, final Key internalKey, final boolean isExtractable,
72+
final Scriptable algorithm, final Collection<String> usages) {
73+
final CryptoKey key = new CryptoKey();
74+
key.internalKey_ = Objects.requireNonNull(internalKey);
75+
76+
if (internalKey instanceof PublicKey) {
77+
key.type_ = "public";
78+
}
79+
else if (internalKey instanceof PrivateKey) {
80+
key.type_ = "private";
81+
}
82+
else if (internalKey instanceof SecretKey) {
83+
key.type_ = "secret";
84+
}
85+
else {
86+
throw new IllegalStateException("Unsupported key type: " + internalKey.getClass());
87+
}
88+
89+
key.isExtractable_ = isExtractable;
90+
key.algorithm_ = algorithm;
91+
key.usages_ = new LinkedHashSet<>(usages);
92+
93+
final Window window = getWindow(Objects.requireNonNull(scope));
94+
key.setParentScope(window);
95+
key.setPrototype(window.getPrototype(CryptoKey.class));
96+
return key;
97+
}
98+
99+
/**
100+
* @return the Java key (opaque {@code [[handle]]} internal slot)
101+
*/
102+
public Key getInternalKey() {
103+
return internalKey_;
104+
}
105+
106+
/**
107+
* @return the key type: "public", "private", or "secret"
108+
*/
109+
@JsxGetter
110+
public String getType() {
111+
return type_;
112+
}
113+
114+
/**
115+
* @return whether the key material may be exported
116+
*/
117+
@JsxGetter
118+
public boolean getExtractable() {
119+
return isExtractable_;
120+
}
121+
122+
/**
123+
* @return the algorithm descriptor object
124+
*/
125+
@JsxGetter
126+
public Scriptable getAlgorithm() {
127+
return algorithm_;
128+
}
129+
130+
/**
131+
* @return the permitted key usages as a JS array
132+
*/
133+
@JsxGetter
134+
public Scriptable getUsages() {
135+
return JavaScriptEngine.newArray(this, usages_.toArray());
136+
}
137+
138+
/**
139+
* @return the permitted key usages as a Java set (for internal use)
140+
*/
141+
public Set<String> getUsagesInternal() {
142+
return usages_;
36143
}
37144
}

0 commit comments

Comments
 (0)