Node.js Verify Reference

Node.js

Verify Object

Verify ক্লাস Node.js এর ক্রিপ্টো মডিউলের অংশ।

এটি ক্রিপ্টোগ্রাফিক ডিজিটাল স্বাক্ষর যাচাই করার একটি উপায় প্রদান করে।

যাচাই ইভেন্টগুলি crypto.createVerify() পদ্ধতি ব্যবহার করে তৈরি করা হয়েছে।

একটি পরিচিত প্রেরকের দ্বারা একটি বার্তা স্বাক্ষরিত এবং ট্রানজিটে পরিবর্তন করা হয়নি তা যাচাই করতে সাইন ক্লাসের সাথে Verify ব্যবহার করা হয়।

Import Crypto Module

// Import the crypto module
const crypto = require('crypto');

// Create a Verify object
const verify = crypto.createVerify('RSA-SHA256');

Verify Methods

পদ্ধতি ব্যাখ্যা
verify.update(data[, inputEncoding]) প্রদত্ত ডেটা দিয়ে যাচাই বিষয়বস্তু আপডেট করে। যদি ইনপুট এনকোডিং প্রদান করা হয়, ডেটা নির্দিষ্ট এনকোডিং ব্যবহার করে একটি স্ট্রিং; অন্যথায়, ডেটা একটি বাফার, টাইপডঅ্যারে বা ডেটাভিউ। এই পদ্ধতিটি নতুন ডেটা সহ একাধিকবার কল করা যেতে পারে।
verify.verify(object, signature[, signatureEncoding]) প্রদত্ত বস্তু এবং স্বাক্ষর ব্যবহার করে সরবরাহকৃত ডেটা যাচাই করে। অবজেক্ট হল একটি স্ট্রিং যাতে একটি PEM-এনকোডেড পাবলিক কী, 'পাবলিক' টাইপের একটি কীঅবজেক্ট, বা একটি X.509 শংসাপত্র। স্বাক্ষর এনকোডিং প্রদান করা হলে, স্বাক্ষর একটি নির্দিষ্ট এনকোডিং ব্যবহার করে একটি স্ট্রিং; অন্যথায় এটি একটি বাফার, TypedArray, বা DataView। স্বাক্ষর বৈধ হলে সত্য দেখায়, অন্যথায় মিথ্যা।

Basic Verify Example

নিম্নলিখিত উদাহরণটি ব্যাখ্যা করে কিভাবে একটি বার্তার ডিজিটাল স্বাক্ষর যাচাই করতে হয়:

const crypto = require('crypto');
const fs = require('fs');

// Load the message, signature, and public key
// In a real application, these would typically come from files or network
// For this example, we'll try to load from the files created in the Sign example
let message, signature, publicKey;

try {
  message = fs.readFileSync('message.txt', 'utf8');
  signature = fs.readFileSync('signature.hex', 'utf8');
  publicKey = fs.readFileSync('public_key.pem', 'utf8');
} catch (error) {
  // If files don't exist, create example data
  const { privateKey, publicKey: newPublicKey } = crypto.generateKeyPairSync('rsa', {
    modulusLength: 2048,
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem'
    }
  });
  
  message = 'This is a message to be verified';
  publicKey = newPublicKey;
  
  // Create a signature for the example
  const sign = crypto.createSign('SHA256');
  sign.update(message);
  signature = sign.sign(privateKey, 'hex');
}

// Create a Verify object
const verify = crypto.createVerify('SHA256');

// Update with the message
verify.update(message);

// Verify the signature with the public key
const isValid = verify.verify(publicKey, signature, 'hex');

console.log('Message:', message);
console.log('Signature:', signature);
console.log('Is signature valid?', isValid);

Verifying with Different Algorithms

যাচাই ক্লাস বিভিন্ন স্বাক্ষর অ্যালগরিদম সমর্থন করে:

const crypto = require('crypto');

// Generate key pairs for different algorithms
function generateRSAKeyPair() {
  return crypto.generateKeyPairSync('rsa', {
    modulusLength: 2048,
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem'
    }
  });
}

function generateECKeyPair() {
  return crypto.generateKeyPairSync('ec', {
    namedCurve: 'prime256v1',
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'sec1',
      format: 'pem'
    }
  });
}

// Generate different key pairs
const rsaKeys = generateRSAKeyPair();
const ecKeys = generateECKeyPair();

// Message to sign and verify
const message = 'Message to verify with different algorithms';

// Function to sign and verify with a specific algorithm
function testSignatureVerification(algorithm, privateKey, publicKey, message) {
  try {
    // Sign the message
    const sign = crypto.createSign(algorithm);
    sign.update(message);
    const signature = sign.sign(privateKey, 'hex');
    
    // Verify the signature
    const verify = crypto.createVerify(algorithm);
    verify.update(message);
    const isValid = verify.verify(publicKey, signature, 'hex');
    
    // Try to verify with a tampered message
    const tamperedVerify = crypto.createVerify(algorithm);
    tamperedVerify.update(message + ' (tampered)');
    const isTamperedValid = tamperedVerify.verify(publicKey, signature, 'hex');
    
    return {
      algorithm,
      isValid,
      isTamperedValid
    };
  } catch (error) {
    return {
      algorithm,
      error: error.message
    };
  }
}

// Test various signature algorithms
console.log(`Message: "${message}"`);
console.log('-----------------------------------------------');

// RSA signatures with different hash algorithms
console.log('RSA Signatures:');
['SHA256', 'SHA384', 'SHA512'].forEach(hash => {
  console.log(testSignatureVerification(hash, rsaKeys.privateKey, rsaKeys.publicKey, message));
});

console.log('-----------------------------------------------');

// ECDSA signatures
console.log('ECDSA Signatures:');
['SHA256', 'SHA384'].forEach(hash => {
  console.log(testSignatureVerification(hash, ecKeys.privateKey, ecKeys.publicKey, message));
});

Verifying with Multiple Updates

আপনি একাধিক তথ্যের টুকরো দিয়ে একটি যাচাই অবজেক্ট আপডেট করতে পারেন:

const crypto = require('crypto');

// Generate a keypair
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem'
  }
});

// Create a signature with multiple updates
const sign = crypto.createSign('SHA256');
sign.update('First part of the message. ');
sign.update('Second part of the message. ');
sign.update('Third part of the message.');
const signature = sign.sign(privateKey, 'hex');

console.log('Signature created with multiple updates');

// Create a Verify object
const verify = crypto.createVerify('SHA256');

// Verify with multiple updates matching the original
verify.update('First part of the message. ');
verify.update('Second part of the message. ');
verify.update('Third part of the message.');
const isValidMultiple = verify.verify(publicKey, signature, 'hex');

console.log('Verification with matching multiple updates:', isValidMultiple);

// Verify with a single update containing the same data
const verifySingle = crypto.createVerify('SHA256');
verifySingle.update('First part of the message. Second part of the message. Third part of the message.');
const isValidSingle = verifySingle.verify(publicKey, signature, 'hex');

console.log('Verification with single update of same data:', isValidSingle);

// Try to verify with different updates
const verifyDifferent = crypto.createVerify('SHA256');
verifyDifferent.update('First part of the message. ');
verifyDifferent.update('Modified second part. ');
verifyDifferent.update('Third part of the message.');
const isValidDifferent = verifyDifferent.verify(publicKey, signature, 'hex');

console.log('Verification with different updates:', isValidDifferent);

Verifying File Signatures

এই উদাহরণটি একটি ফাইলের জন্য একটি ডিজিটাল স্বাক্ষর যাচাই করার চিত্র তুলে ধরে:

const crypto = require('crypto');
const fs = require('fs');

// Function to verify a file's signature
function verifyFile(filePath, signaturePath, publicKey, algorithm = 'SHA256') {
  return new Promise((resolve, reject) => {
    try {
      // Read the signature
      const signature = fs.readFileSync(signaturePath, 'utf8');
      
      // Create Verify object
      const verify = crypto.createVerify(algorithm);
      
      // Create read stream for the file
      const readStream = fs.createReadStream(filePath);
      
      // Handle stream events
      readStream.on('data', (data) => {
        verify.update(data);
      });
      
      readStream.on('end', () => {
        // Verify the signature
        const isValid = verify.verify(publicKey, signature, 'hex');
        resolve(isValid);
      });
      
      readStream.on('error', (error) => {
        reject(error);
      });
    } catch (error) {
      reject(error);
    }
  });
}

// For this example, create a file, sign it, and verify it
const filePath = 'example_to_verify.txt';
const signaturePath = `${filePath}.sig`;
const publicKeyPath = 'verify_public_key.pem';

// Create a test environment if files don't exist
if (!fs.existsSync(filePath) || !fs.existsSync(signaturePath) || !fs.existsSync(publicKeyPath)) {
  console.log('Creating test environment...');
  
  // Generate a keypair
  const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
    modulusLength: 2048,
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem'
    }
  });
  
  // Save the public key
  fs.writeFileSync(publicKeyPath, publicKey);
  
  // Create a test file
  fs.writeFileSync(filePath, 'This is a test file for signature verification.\n'.repeat(100));
  
  // Sign the file
  const sign = crypto.createSign('SHA256');
  const fileContent = fs.readFileSync(filePath);
  sign.update(fileContent);
  const signature = sign.sign(privateKey, 'hex');
  
  // Save the signature
  fs.writeFileSync(signaturePath, signature);
  
  console.log('Test environment created');
}

// Load the public key
const publicKey = fs.readFileSync(publicKeyPath, 'utf8');

// Verify the file signature
verifyFile(filePath, signaturePath, publicKey)
  .then(isValid => {
    console.log(`File: ${filePath}`);
    console.log(`Signature: ${signaturePath}`);
    console.log(`Verification result: ${isValid ? 'Valid signature' : 'Invalid signature'}`);
    
    // Demonstrate a tampered file
    if (isValid) {
      const tamperedFilePath = `${filePath}.tampered`;
      fs.copyFileSync(filePath, tamperedFilePath);
      
      // Make a small change to the file
      const content = fs.readFileSync(tamperedFilePath, 'utf8');
      fs.writeFileSync(tamperedFilePath, content.replace('verification', 'TAMPERED'));
      
      // Verify the tampered file with the original signature
      return verifyFile(tamperedFilePath, signaturePath, publicKey)
        .then(isTamperedValid => {
          console.log(`\nTampered file: ${tamperedFilePath}`);
          console.log(`Verification result: ${isTamperedValid ? 'Valid signature (unexpected!)' : 'Invalid signature (expected)'}`);
        });
    }
  })
  .catch(error => {
    console.error('Error verifying file:', error.message);
  });

Verifying with Different Key Types

যাচাই ক্লাস বিভিন্ন ধরনের পাবলিক কীগুলির সাথে কাজ করতে পারে:

const crypto = require('crypto');
const fs = require('fs');

// Message to sign and verify
const message = 'Message to verify with different key formats';

// Function to sign and verify with different key formats
function verifyWithKeyFormat(publicKey, keyFormat, algorithm = 'SHA256') {
  try {
    // Generate keypair for test
    const { privateKey, publicKey: generatedPublicKey } = crypto.generateKeyPairSync('rsa', {
      modulusLength: 2048,
      publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
      },
      privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem'
      }
    });
    
    // Sign the message with private key
    const sign = crypto.createSign(algorithm);
    sign.update(message);
    const signature = sign.sign(privateKey, 'hex');
    
    // Verify with the provided public key format
    const verify = crypto.createVerify(algorithm);
    verify.update(message);
    return {
      format: keyFormat,
      isValid: verify.verify(publicKey, signature, 'hex')
    };
  } catch (error) {
    return {
      format: keyFormat,
      error: error.message
    };
  }
}

// Generate an RSA key pair
const { privateKey, publicKey: pemPublicKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem'
  }
});

// Sign the message for verification tests
const sign = crypto.createSign('SHA256');
sign.update(message);
const signature = sign.sign(privateKey, 'hex');

// Function to verify with different key formats
function testVerifyWithKey(publicKey, keyFormat) {
  try {
    const verify = crypto.createVerify('SHA256');
    verify.update(message);
    return {
      format: keyFormat,
      isValid: verify.verify(publicKey, signature, 'hex')
    };
  } catch (error) {
    return {
      format: keyFormat,
      error: error.message
    };
  }
}

console.log(`Message: "${message}"`);
console.log('Signature:', signature.substring(0, 32) + '...');

// 1. Verify with PEM-encoded public key (string)
console.log('\n1. PEM-encoded public key (string):');
console.log(testVerifyWithKey(pemPublicKey, 'PEM string'));

// 2. Verify with PEM-encoded public key (buffer)
console.log('\n2. PEM-encoded public key (buffer):');
console.log(testVerifyWithKey(Buffer.from(pemPublicKey), 'PEM buffer'));

// 3. Verify with KeyObject
console.log('\n3. KeyObject:');
const keyObject = crypto.createPublicKey(pemPublicKey);
console.log(testVerifyWithKey(keyObject, 'KeyObject'));

// 4. Try to verify with X.509 certificate
console.log('\n4. X.509 Certificate (simulated):');
console.log({
  format: 'X.509 Certificate',
  note: 'In a real scenario, you would load an X.509 certificate containing the public key'
});

// 5. Try to verify with JWK (requires conversion)
console.log('\n5. JWK (requires conversion):');
console.log({
  format: 'JWK',
  note: 'JWK needs to be converted to PEM or KeyObject first'
});

Verifying with Advanced Options

নির্দিষ্ট OpenSSL বিকল্পের সাথে স্বাক্ষর যাচাই করে:

const crypto = require('crypto');

// Generate RSA key pair
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem'
  }
});

// Message to sign
const message = 'Message to verify with different options';

// Function to sign with specific options
function signWithOptions(algorithm, message, privateKey, options = {}) {
  // Create private key with options
  const keyWithOptions = {
    key: privateKey,
    ...options
  };
  
  // Sign the message
  const sign = crypto.createSign(algorithm);
  sign.update(message);
  return sign.sign(keyWithOptions, 'hex');
}

// Function to verify with specific options
function verifyWithOptions(algorithm, message, publicKey, signature, options = {}) {
  try {
    // Create public key with options
    const keyWithOptions = {
      key: publicKey,
      ...options
    };
    
    // Verify the signature
    const verify = crypto.createVerify(algorithm);
    verify.update(message);
    return verify.verify(keyWithOptions, signature, 'hex');
  } catch (error) {
    return `Error: ${error.message}`;
  }
}

console.log(`Message: "${message}"`);

// 1. Sign and verify with standard PKCS#1 v1.5 padding (default)
const sig1 = signWithOptions('SHA256', message, privateKey);
console.log('\n1. Standard PKCS#1 v1.5 padding:');
console.log('Signature:', sig1.substring(0, 32) + '...');
console.log('Verification result:', verifyWithOptions('SHA256', message, publicKey, sig1));

// 2. Sign and verify with PSS padding
const pssOptions = {
  padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
  saltLength: 32
};
const sig2 = signWithOptions('SHA256', message, privateKey, pssOptions);
console.log('\n2. PSS padding:');
console.log('Signature:', sig2.substring(0, 32) + '...');
console.log('Verification result (matching options):',
  verifyWithOptions('SHA256', message, publicKey, sig2, pssOptions));
console.log('Verification result (default options):',
  verifyWithOptions('SHA256', message, publicKey, sig2));

// 3. Verify with PSS padding and different salt lengths
console.log('\n3. PSS padding with different salt lengths:');
[20, 32, 48].forEach(saltLength => {
  const sigSalt = signWithOptions('SHA256', message, privateKey, {
    padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
    saltLength
  });
  
  console.log(`Salt length ${saltLength}:`);
  
  // Try to verify with correct salt length
  console.log(`  - Verify with correct salt length (${saltLength}):`,
    verifyWithOptions('SHA256', message, publicKey, sigSalt, {
      padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
      saltLength
    }));
  
  // Try to verify with wrong salt length
  const wrongSaltLength = saltLength + 10;
  console.log(`  - Verify with wrong salt length (${wrongSaltLength}):`,
    verifyWithOptions('SHA256', message, publicKey, sigSalt, {
      padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
      saltLength: wrongSaltLength
    }));
});

Certificate-Based Verification

X.509 :

const crypto = require('crypto');
const fs = require('fs');

// Function to simulate a certificate-based verification
function demonstrateCertificateVerification() {
  console.log('Certificate-Based Verification Demonstration');
  console.log('-------------------------------------------');
  
  console.log('In a real application, you would:');
  console.log('1. Obtain an X.509 certificate containing the signer\'s public key');
  console.log('2. Verify the certificate\'s trust chain');
  console.log('3. Extract the public key from the certificate');
  console.log('4. Use that public key to verify the signature');
  
  console.log('\nSimplified example:');
  
  // Generate a key pair
  const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
    modulusLength: 2048,
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem'
    }
  });
  
  // In a real app, you'd have a certificate with the public key
  const mockCertificate = `-----BEGIN CERTIFICATE-----
(This would be a real X.509 certificate containing the public key)
-----END CERTIFICATE-----`;
  
  // Message to sign
  const message = 'Message signed with a certificate-backed key';
  
  // Sign the message
  const sign = crypto.createSign('SHA256');
  sign.update(message);
  const signature = sign.sign(privateKey, 'hex');
  
  console.log(`Message: "${message}"`);
  console.log(`Signature: ${signature.substring(0, 32)}...`);
  
  console.log('\nVerification steps:');
  console.log('1. Extract public key from certificate (simulated)');
  
  // In a real scenario, you'd extract the public key from the certificate
  // For this example, we'll use our generated public key directly
  console.log('2. Verify the signature using the extracted public key');
  
  const verify = crypto.createVerify('SHA256');
  verify.update(message);
  const isValid = verify.verify(publicKey, signature, 'hex');
  
  console.log(`Verification result: ${isValid ? 'Valid signature' : 'Invalid signature'}`);
}

// Run the demonstration
demonstrateCertificateVerification();

Security Best Practices

ডিজিটাল স্বাক্ষর যাচাই করার সময় এই নিরাপত্তার সর্বোত্তম অনুশীলনগুলি বিবেচনা করুন:

ট্রাস্ট ব্যবস্থাপনা:প্রমাণীকরণের জন্য ব্যবহৃত পাবলিক কী-এর উৎস যাচাই করুন। একটি বিশ্বস্ত উত্স থেকে না আসা পর্যন্ত একটি পাবলিক কী বিশ্বাস করবেন না৷
শংসাপত্র যাচাইকরণ:শংসাপত্র ব্যবহার করার সময়, সম্পূর্ণ শংসাপত্র চেইন পরীক্ষা করুন এবং শংসাপত্র প্রত্যাহার স্থিতি পরীক্ষা করুন।
মিলিত স্বাক্ষর অ্যালগরিদম:নিশ্চিত করুন যে বৈধকরণ অ্যালগরিদম স্বাক্ষর অ্যালগরিদমের সাথে মেলে, স্বাক্ষর অ্যালগরিদম, প্যাডিং বা লবণের দৈর্ঘ্যের মতো যেকোনো বিকল্প সহ।
ইনপুট বৈধতা:ইনজেকশন আক্রমণ প্রতিরোধ করার জন্য এটি যাচাই করার আগে কোনো তথ্য যাচাই এবং স্যানিটাইজ করুন।
ব্যর্থ নিরাপদ:সর্বদা ডিফল্ট স্বাক্ষর প্রত্যাখ্যান যে কোনো কারণে বৈধতা ব্যর্থ হয়.
যাচাইকরণ কোড সহজ রাখুন:জটিলতা বৈধকরণ বাইপাস দুর্বলতার ঝুঁকি বাড়ায়।
সময় আক্রমণ বিবেচনা করুন:কিছু বাস্তবায়নে স্বাক্ষর যাচাইকরণ সময় আক্রমণের জন্য ঝুঁকিপূর্ণ হতে পারে।

Common Use Cases for Signature Verification

Software Updates

ইনস্টলেশনের আগে আপডেটের সত্যতা যাচাই করা হচ্ছে

Document Verification

ডিজিটালি স্বাক্ষরিত নথিগুলি পরিবর্তন করা হয়নি তা নিশ্চিত করা

API Authentication

API অনুরোধের পরিচয় যাচাই করা হচ্ছে

JWT Validation

JSON ওয়েব টোকেন স্বাক্ষর যাচাই করা হচ্ছে

Certificate Chain Validation

সার্টিফিকেট চেইনে স্বাক্ষর যাচাই করা হচ্ছে

Secure Communication

সুরক্ষিত প্রোটোকলগুলিতে বার্তাগুলির প্রমাণীকরণ

অনুশীলন করুন

Node.js SHA256 Verify .

crypto.createVerify('SHA256', publicKey)
✗ ভুল! "crypto.createVerify()" পদ্ধতিতে সর্বজনীন কী প্রয়োজন হয় না, এটি শুধুমাত্র যাচাইকরণের সময় প্রয়োজন হয়
crypto.createVerify('SHA256')
✓ ঠিক আছে! "crypto.createVerify('SHA256')" হল সঠিক পদ্ধতি যা Node.js-এ SHA256 অ্যালগরিদম দিয়ে যাচাই ইভেন্ট তৈরি করতে ব্যবহৃত হয়
crypto.makeVerify('SHA256')
✗ ভুল! "crypto.makeVerify()" Node.js-এ একটি বৈধ পদ্ধতি নয়
crypto.newVerify('SHA256')
✗ ভুল! "crypto.newVerify()" Node.js-এ একটি বৈধ পদ্ধতি নয়