crypto.createVerify()는 매개변수에 전달된 알고리즘을 사용하는 검증 객체를 생성하고 반환합니다. crypto.getHashes()를 사용하여 사용 가능한 모든 서명 알고리즘의 이름을 가져올 수 있습니다. 일부 경우에만 다이제스트 알고리즘 대신 'RHA-SHA256'과 같은 서명 알고리즘의 이름을 사용하여 Verify 인스턴스를 생성할 수 있습니다.
구문
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