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

Function test

test/parallel/test-crypto-async-sign-verify.js:12–66  ·  view source on GitHub ↗
(
  publicFixture,
  privateFixture,
  algorithm,
  deterministic,
  options
)

Source from the content-addressed store, hash-verified

10const fixtures = require('../common/fixtures');
11
12function test(
13 publicFixture,
14 privateFixture,
15 algorithm,
16 deterministic,
17 options
18) {
19 let publicPem = fixtures.readKey(publicFixture);
20 let privatePem = fixtures.readKey(privateFixture);
21 let privateKey = crypto.createPrivateKey(privatePem);
22 let publicKey = crypto.createPublicKey(publicPem);
23 const privateDer = {
24 key: privateKey.export({ format: 'der', type: 'pkcs8' }),
25 format: 'der',
26 type: 'pkcs8',
27 ...options
28 };
29 const publicDer = {
30 key: publicKey.export({ format: 'der', type: 'spki' }),
31 format: 'der',
32 type: 'spki',
33 ...options
34 };
35
36 if (options) {
37 publicPem = { ...options, key: publicPem };
38 privatePem = { ...options, key: privatePem };
39 privateKey = { ...options, key: privateKey };
40 publicKey = { ...options, key: publicKey };
41 }
42
43 const data = Buffer.from('Hello world');
44 const expected = crypto.sign(algorithm, data, privateKey);
45
46 for (const key of [privatePem, privateKey, privateDer]) {
47 crypto.sign(algorithm, data, key, common.mustSucceed((actual) => {
48 if (deterministic) {
49 assert.deepStrictEqual(actual, expected);
50 }
51
52 assert.strictEqual(
53 crypto.verify(algorithm, data, key, actual), true);
54 }));
55 }
56
57 const verifyInputs = [
58 publicPem, publicKey, publicDer, privatePem, privateKey, privateDer];
59 for (const key of verifyInputs) {
60 crypto.verify(algorithm, data, key, expected, common.mustSucceed(
61 (verified) => assert.strictEqual(verified, true)));
62
63 crypto.verify(algorithm, data, key, Buffer.from(''), common.mustSucceed(
64 (verified) => assert.strictEqual(verified, false)));
65 }
66}
67
68// RSA w/ default padding
69test('rsa_public.pem', 'rsa_private.pem', 'sha256', true);

Calls 3

exportMethod · 0.45
fromMethod · 0.45
verifyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…