({ name,
context,
publicKeyPem,
privateKeyPem,
signature,
data })
| 133 | } |
| 134 | |
| 135 | async function testSign({ name, |
| 136 | context, |
| 137 | publicKeyPem, |
| 138 | privateKeyPem, |
| 139 | signature, |
| 140 | data }) { |
| 141 | const [ |
| 142 | publicKey, |
| 143 | privateKey, |
| 144 | hmacKey, |
| 145 | rsaKeys, |
| 146 | ecKeys, |
| 147 | ] = await Promise.all([ |
| 148 | crypto.createPublicKey(publicKeyPem) |
| 149 | .toCryptoKey(name, false, ['verify']), |
| 150 | crypto.createPrivateKey(privateKeyPem) |
| 151 | .toCryptoKey(name, false, ['sign']), |
| 152 | subtle.generateKey( |
| 153 | { name: 'HMAC', hash: 'SHA-256' }, |
| 154 | false, |
| 155 | ['sign']), |
| 156 | subtle.generateKey( |
| 157 | { |
| 158 | name: 'RSA-PSS', |
| 159 | modulusLength: 1024, |
| 160 | publicExponent: new Uint8Array([1, 0, 1]), |
| 161 | hash: 'SHA-256', |
| 162 | }, |
| 163 | false, |
| 164 | ['sign']), |
| 165 | subtle.generateKey( |
| 166 | { |
| 167 | name: 'ECDSA', |
| 168 | namedCurve: 'P-256' |
| 169 | }, |
| 170 | false, |
| 171 | ['sign']), |
| 172 | ]); |
| 173 | |
| 174 | { |
| 175 | const sig = await subtle.sign({ name, context }, privateKey, data); |
| 176 | assert.strictEqual(sig.byteLength, signature.byteLength); |
| 177 | assert(await subtle.verify({ name, context }, publicKey, sig, data)); |
| 178 | } |
| 179 | |
| 180 | { |
| 181 | const copy = Buffer.from(data); |
| 182 | const p = subtle.sign({ name, context }, privateKey, copy); |
| 183 | copy[0] = 255 - copy[0]; |
| 184 | const sig = await p; |
| 185 | assert(await subtle.verify({ name, context }, publicKey, sig, data)); |
| 186 | } |
| 187 | |
| 188 | // Test failure when using wrong key |
| 189 | await assert.rejects( |
| 190 | subtle.sign({ name, context }, publicKey, data), { |
| 191 | message: /Unable to use this key to sign/ |
| 192 | }); |
no test coverage detected