MCPcopy Index your code
hub / github.com/nodejs/node / assert_goodCryptoKey

Function assert_goodCryptoKey

test/fixtures/wpt/WebCryptoAPI/util/helpers.js:91–174  ·  view source on GitHub ↗
(key, algorithm, extractable, usages, kind)

Source from the content-addressed store, hash-verified

89// Is key a CryptoKey object with correct algorithm, extractable, and usages?
90// Is it a secret, private, or public kind of key?
91function assert_goodCryptoKey(key, algorithm, extractable, usages, kind) {
92 if (typeof algorithm === "string") {
93 algorithm = { name: algorithm };
94 }
95
96 var correctUsages = [];
97
98 var registeredAlgorithmName;
99 registeredAlgorithmNames.forEach(function(name) {
100 if (name.toUpperCase() === algorithm.name.toUpperCase()) {
101 registeredAlgorithmName = name;
102 }
103 });
104
105 assert_equals(key.constructor, CryptoKey, "Is a CryptoKey");
106 assert_equals(key.type, kind, "Is a " + kind + " key");
107 assert_equals(key.extractable, extractable, "Extractability is correct");
108
109 assert_equals(key.algorithm.name, registeredAlgorithmName, "Correct algorithm name");
110 if (key.algorithm.name.toUpperCase() === "HMAC" && algorithm.length === undefined) {
111 switch (key.algorithm.hash.name.toUpperCase()) {
112 case 'SHA-1':
113 case 'SHA-256':
114 assert_equals(key.algorithm.length, 512, "Correct length");
115 break;
116 case 'SHA-384':
117 case 'SHA-512':
118 assert_equals(key.algorithm.length, 1024, "Correct length");
119 break;
120 default:
121 assert_unreached("Unrecognized hash");
122 }
123 } else if (key.algorithm.name.toUpperCase().startsWith("KMAC") && algorithm.length === undefined) {
124 switch (key.algorithm.name.toUpperCase()) {
125 case 'KMAC128':
126 assert_equals(key.algorithm.length, 128, "Correct length");
127 break;
128 case 'KMAC256':
129 assert_equals(key.algorithm.length, 256, "Correct length");
130 break;
131 }
132 } else {
133 assert_equals(key.algorithm.length, algorithm.length, "Correct length");
134 }
135 if (["HMAC", "RSASSA-PKCS1-v1_5", "RSA-PSS"].includes(registeredAlgorithmName)) {
136 assert_equals(key.algorithm.hash.name.toUpperCase(), algorithm.hash.toUpperCase(), "Correct hash function");
137 }
138
139 if (/^(?:Ed|X)(?:25519|448)$/.test(key.algorithm.name)) {
140 assert_false('namedCurve' in key.algorithm, "Does not have a namedCurve property");
141 }
142
143 // usages is expected to be provided for a key pair, but we are checking
144 // only a single key. The publicKey and privateKey portions of a key pair
145 // recognize only some of the usages appropriate for a key pair.
146 if (key.type === "public") {
147 ["encrypt", "verify", "wrapKey", "encapsulateBits", "encapsulateKey"].forEach(function(usage) {
148 if (usages.includes(usage)) {

Callers 12

testSuccessFunction · 0.85
testFormatFunction · 0.85
testFormatFunction · 0.85
testEcdhJwkAlgFunction · 0.85
testPkcs8PrivateOnlyFunction · 0.85
testFormatFunction · 0.85
testFormatFunction · 0.85
testFormatFunction · 0.85
testFormatFunction · 0.85
testWrappingFunction · 0.85

Calls 9

includesMethod · 0.80
forEachMethod · 0.65
assert_equalsFunction · 0.50
assert_unreachedFunction · 0.50
assert_falseFunction · 0.50
assert_not_equalsFunction · 0.50
assert_in_arrayFunction · 0.50
testMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…