How to use the createVerify function from crypto

Find comprehensive JavaScript crypto.createVerify code examples handpicked from public code repositorys.

crypto.createVerify is a Node.js built-in module method that creates a Verify object used to verify the cryptographic signature of data.

163
164
165
166
167
168
169
170
171
172
function verify(input, key, method, type, signature) {
  if(type === "hmac") {
    return (signature === sign(input, key, method, type));
  }
  else if(type == "sign") {
    return crypto.createVerify(method)
                 .update(input)
                 .verify(key, base64urlUnescape(signature), 'base64');
  }
  else {
fork icon147
star icon0
watch icon31

+ 7 other calls in file

132
133
134
135
136
137
138
139
140
141
    let ansiPublicKey = Buffer.concat([
        Buffer.from([0x04]), x, y
    ])

    let pemPublicKey  = rawPublicKeyToPEM(ansiPublicKey, crv);
    return crypto.createVerify(hashAlg)
        .update(messageBuffer)
        .verify(pemPublicKey, signatureBuffer);

// Verify RSA
fork icon3
star icon1
watch icon2

+ 7 other calls in file

How does crypto.createVerify work?

crypto.createVerify is a method in Node.js's crypto module that creates a Verify object that can be used to verify the cryptographic signature of a piece of data using a public key. It takes an algorithm parameter and returns a Verify object with methods for updating the signature with data and verifying the signature against the public key.

102
103
104
105
106
107
108
109
110
111
  message,
  signature,
  inputEncoding,
  signatureEncoding
) {
  const verifyFunc = crypto.createVerify(algorithm);
  verifyFunc.update(message, inputEncoding);
  verifyFunc.end();
  return verifyFunc.verify(publicKey, signature, signatureEncoding);
}
fork icon2
star icon21
watch icon5

+ 3 other calls in file

47
48
49
50
51
52
53
54
55
56
57
58
// 验签
const publicKeyStr = fs.readFileSync(path.join(__dirname, './keys/pub.key'), inputCharset);
console.log('publicKeyStr = ' + publicKeyStr);
console.log('');


const verifyRsa2Result = crypto.createVerify(signAlgorithm).update(waitForSignStr, inputCharset).verify(fillPublicKeyMarker(publicKeyStr), sign, 'base64');
console.log('verifyRsa2Result = ' + verifyRsa2Result);
console.log('');


// kad 登录链接
fork icon0
star icon1
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const crypto = require("crypto");

const message = "hello world";
const privateKey = "..."; // The private key used to sign the message
const publicKey = "..."; // The public key used to verify the signature
const signature = "..."; // The digital signature created by signing the message with the private key

// Create a verifier object using the public key
const verifier = crypto.createVerify("SHA256");
verifier.update(message);

// Verify the signature using the verifier and the public key
const isSignatureValid = verifier.verify(publicKey, signature, "base64");

console.log(`Is the signature valid? ${isSignatureValid}`);

In this example, we create a verifier object using the crypto.createVerify method, passing the hashing algorithm as an argument. We then update the verifier object with the message to be verified using the update method. Finally, we verify the signature using the verify method, passing in the public key, the signature, and the encoding format as arguments. The verify method returns true if the signature is valid, false otherwise.

124
125
126
127
128
129
130
131
132
133
134


const verifySignature = (message, signature, testMode) => {
    try {
        const key = trimPemKey(testMode ? csobConfig.TEST_PRODUCTION_PUB : csobConfig.PRODUCTION_PUB);
        const publicKeyToVerify = crypto.createPublicKey(key);
        const verify = crypto.createVerify('RSA-SHA256');
        verify.update(message);
        return verify.verify(publicKeyToVerify, signature, 'base64');
    } catch (error) {
        console.log(error)
fork icon0
star icon0
watch icon1

216
217
218
219
220
221
222
223
224
    comarision_array.push(`${header_compare}: ${ header[header_compare]}`)
  }

}

const verifier = crypto.createVerify("sha256");
const comparethis = comarision_array.join('\n')
verifier.update(comparethis)
const result = verifier.verify(publicKey, signature, 'base64');
fork icon0
star icon0
watch icon1

+ 4 other calls in file

97
98
99
100
101
102
103
104
105
106
let s1stream = crypto.createSign('SHA1');
s1stream.end('Test123');
s1stream = s1stream.sign(keyPem, 'base64');
assert.strictEqual(s1, s1stream, `${s1} should equal ${s1stream}`);

const verified = crypto.createVerify('SHA1')
                       .update('Test')
                       .update('123')
                       .verify(certPem, s1, 'base64');
assert.strictEqual(verified, true);
fork icon0
star icon0
watch icon0

+ 29 other calls in file

156
157
158
159
160
161
162
163
164
165
function createKeyVerifier(bits) {
  return function verify(thing, signature, publicKey) {
    checkIsPublicKey(publicKey);
    thing = normalizeInput(thing);
    signature = toBase64(signature);
    var verifier = crypto.createVerify('RSA-SHA' + bits);
    verifier.update(thing);
    return verifier.verify(publicKey, signature, 'base64');
  }
}
fork icon0
star icon0
watch icon0

+ 3 other calls in file

79
80
81
82
83
84
85
86
87
88
		)
	)
}

let result = Buffer.concat(res)
let verified = crypto.createVerify("RSA-SHA256").update(result).verify(pubKey, sign)

console.log("Verified signature =>", verified)

const root = await loadProto("./src/game/genshin/proto/schema.proto")
fork icon0
star icon0
watch icon0

430
431
432
433
434
435
436
437
438
439
// Test signing and verifying
{
  const s1 = crypto.createSign('SHA1')
                 .update('Test123')
                 .sign(keyPem, 'base64');
  const s1Verified = crypto.createVerify('SHA1')
                         .update('Test')
                         .update('123')
                         .verify(certPem, s1, 'base64');
  assert.strictEqual(s1Verified, true);
fork icon0
star icon0
watch icon0

+ 11 other calls in file

155
156
157
158
159
160
161
162
163
164
	    'signing or verification'));

var v, nm, err;
try {
	nm = hashAlgo.toUpperCase();
	v = crypto.createVerify(nm);
} catch (e) {
	err = e;
}
if (v === undefined || (err instanceof Error &&
fork icon0
star icon0
watch icon0

+ 3 other calls in file

249
250
251
252
253
254
255
256
257
258
259
260
  }
}


// Test RSA key signing/verification
let rsaSign = crypto.createSign('SHA1');
let rsaVerify = crypto.createVerify('SHA1');
assert.ok(rsaSign);
assert.ok(rsaVerify);


const expectedSignature = fixtures.readKey(
fork icon0
star icon0
watch icon0

+ 53 other calls in file

70
71
72
73
74
75
76
77
78
79
  common.mustSucceed((verified) => assert.strictEqual(verified, false)),
);

// stream
assert.strictEqual(
  crypto.createVerify('sha256')
    .update(data)
    .verify(key, signature),
  false,
);
fork icon0
star icon0
watch icon0

31
32
33
34
35
36
37
38
39
40
    title: req.body.title,
    description: req.body.description,
    signature: signature
})

let verifier = crypto.createVerify(signing_algorithm)
verifier.update(req.body.title)
verifier.update(req.body.description)
verifier.end()
console.log(verifier.verify(public_key, signature, format_string))
fork icon0
star icon0
watch icon0

213
214
215
216
217
218
219
220
221

Example: Using `Verify` objects as streams:

```js
const crypto = require('crypto');
const verify = crypto.createVerify('rsa-sha256');

verify.write('some data to sign');
verify.end();
fork icon0
star icon0
watch icon1

+ 3 other calls in file