crypto.createVerify() จะสร้างและส่งคืนวัตถุตรวจสอบที่ใช้อัลกอริทึมที่ส่งผ่านในพารามิเตอร์ คุณสามารถใช้ crypto.getHashes() เพื่อรับชื่อของอัลกอริธึมการลงนามที่มีอยู่ทั้งหมด คุณสามารถสร้างการยืนยันอินสแตนซ์ได้โดยใช้ชื่อของอัลกอริทึมลายเซ็น เช่น 'RHA-SHA256' ในบางกรณีเท่านั้น แทนที่จะเป็นอัลกอริทึมไดเจสต์
ไวยากรณ์
crypto.createVerify(algorithm, [options])
พารามิเตอร์
พารามิเตอร์ข้างต้นอธิบายไว้ด้านล่าง −
-
อัลกอริทึม – ใช้อินพุตสำหรับชื่ออัลกอริทึมที่จะใช้ในขณะที่สร้างวัตถุ/อินสแตนซ์ตรวจสอบ
-
ตัวเลือก – นี่เป็นพารามิเตอร์ทางเลือกที่สามารถใช้ควบคุมพฤติกรรมการสตรีมได้
ตัวอย่าง
สร้างไฟล์ที่มีชื่อ – createVerify.js และคัดลอกข้อมูลโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ให้ใช้คำสั่งต่อไปนี้เพื่อเรียกใช้โค้ดนี้ดังแสดงในตัวอย่างด้านล่าง −
node createVerify.js
createVerify.js
// Node.js program to demonstrate the use of createVerify() method
// Importing the crypto module
const crypto = require('crypto');
// Creating verify object with the input algorithm
const verify = crypto.createVerify('SHA256');
// Returning the verify object
console.log(verify); ผลลัพธ์
C:\home\node>> node createVerify.js
Verify {
_handle: {},
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree:
{ next: null,
entry: null,
finish: [Function: bound onCorkedFinish] } },
writable: true,
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined } ตัวอย่าง
ลองดูอีกตัวอย่างหนึ่ง
// Node.js program to demonstrate the use of createVerify() method
// Importing the crypto module
const crypto = require('crypto');
// Creating the verify object from SHA256 algo
const verify = crypto.createVerify('SHA256');
// Writing the below data to be signed and verified
verify.write('TutorialPoint');
// Ending the method
verify.end();
// Beginning public key execution
const l1 = "-----BEGIN PUBLIC KEY-----\n"
// Encrypted data
const l2 = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXIvPbzLjaPLd8jgiv1TL/X8PXpJNgDkGRj9U9Lcx1
yKURpQFVavcMkfWyO8r7JlZNMax0JKfLZUM1IePRjHlFw=="
// Finishing public key execution
const l3 = "\n-----END PUBLIC KEY-----"
// concatenating all public keys
const publicKey = l1 + l2 + l3
// Signature that will be verified
const signature = "MEYCIQCPfWhpzxMqu3gZWflBm5V0aetgb2/S+SGyGcElaOjgdgIhALaD4lbxVwa8HUUBFOLz+CGvI
ioDkf9oihSnXHCqh8yV";
// Prints true if signature is verified else false
console.log(verify.verify(publicKey, signature)); ผลลัพธ์
C:\home\node>> node createVerify.js false