1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.crypto;
16
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.Set;
23
24 import javax.crypto.SecretKey;
25
26 import org.htmlunit.corejs.javascript.Scriptable;
27 import org.htmlunit.corejs.javascript.VarScope;
28 import org.htmlunit.javascript.HtmlUnitScriptable;
29 import org.htmlunit.javascript.JavaScriptEngine;
30 import org.htmlunit.javascript.configuration.JsxClass;
31 import org.htmlunit.javascript.configuration.JsxConstructor;
32 import org.htmlunit.javascript.configuration.JsxGetter;
33
34
35
36
37
38
39
40
41
42
43 @JsxClass
44 public class CryptoKey extends HtmlUnitScriptable {
45
46 private Key internalKey_;
47 private String type_;
48 private boolean isExtractable_;
49 private Scriptable algorithm_;
50 private Set<String> usages_;
51
52
53
54
55 @JsxConstructor
56 public void jsConstructor() {
57 throw JavaScriptEngine.typeErrorIllegalConstructor();
58 }
59
60
61
62
63
64
65
66
67
68
69
70 static CryptoKey create(final VarScope scope, final Key internalKey, final boolean isExtractable,
71 final Scriptable algorithm, final Collection<String> usages) {
72 if (internalKey == null) {
73 throw new NullPointerException("The provided key can't be null");
74 }
75
76 final CryptoKey key = new CryptoKey();
77 key.internalKey_ = internalKey;
78
79 if (internalKey instanceof PublicKey) {
80 key.type_ = "public";
81 }
82 else if (internalKey instanceof PrivateKey) {
83 key.type_ = "private";
84 }
85 else if (internalKey instanceof SecretKey) {
86 key.type_ = "secret";
87 }
88 else {
89 throw new IllegalStateException("Unsupported key type: " + internalKey.getClass());
90 }
91
92 key.isExtractable_ = isExtractable;
93 key.algorithm_ = algorithm;
94 key.usages_ = new LinkedHashSet<>(usages);
95
96 key.setParentScope(scope);
97 key.setPrototype(getWindow(key).getPrototype(CryptoKey.class));
98 return key;
99 }
100
101
102
103
104 public Key getInternalKey() {
105 return internalKey_;
106 }
107
108
109
110
111 @JsxGetter
112 public String getType() {
113 return type_;
114 }
115
116
117
118
119 @JsxGetter
120 public boolean getExtractable() {
121 return isExtractable_;
122 }
123
124
125
126
127 @JsxGetter
128 public Scriptable getAlgorithm() {
129 return algorithm_;
130 }
131
132
133
134
135 @JsxGetter
136 public Scriptable getUsages() {
137 return JavaScriptEngine.newArray(getParentScope(), usages_.toArray());
138 }
139
140
141
142
143 public Set<String> getUsagesInternal() {
144 return usages_;
145 }
146 }