The verify.update() method is an inbuilt method in verify class within the crypto module in NodeJs. This method verifies the content within the given data. When the input encoding parameter is not specified and it detects that the data is a string, the ‘utf8’ encoding is used. However, if the data is the type of Buffer, TypedArray or DataView, then any specified input encoding is ignored.
Syntax:
verify.update(data, inputEncoding)
Parameters: This method accepts the following parameters as mentioned above:
- data: This parameter defines the type of data like string, TypeArray, Buffer or DataView.
- inputEncoding: This parameter defines the encoding type of the input.
Returns: This method does not return any value.
Example 1: In this example, we will see how to use the update method for verifying the hash.
const crypto = require( 'crypto' );
const { privateKey, publicKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
});
const sign = crypto.createSign( 'SHA256' );
// Both strings should be same sign.update( 'GeeksforGeeks' );
sign.end(); const signature = sign.sign(privateKey); const verify = crypto.createVerify( 'SHA256' );
// Both strings should be same verify.update( 'GeeksforGeeks' );
verify.end(); console.log(verify.verify(publicKey, signature)); |
Output:
true
Example 2: Below the example, we are changing the content of the data then we will verify the content of the data by using verify.update() method.
const crypto = require( 'crypto' );
const { privateKey, publicKey } = crypto.generateKeyPairSync( 'rsa' , {
modulusLength: 2048,
});
const sign = crypto.createSign( 'SHA256' );
// Both strings should be same sign.update( 'GeeksforGeeks' );
sign.end(); const signature = sign.sign(privateKey); const verify = crypto.createVerify( 'SHA256' );
// Both strings should be same verify.update( 'GeeksforGeeks Not Same' )
verify.end(); console.log(verify.verify(publicKey, signature)); |
Output:
false
Reference: https://nodejs.org/api/crypto.html#verifyupdatedata-inputencoding