ভূমিকা
crypto.createHmac() মেথড একটি Hmac অবজেক্ট তৈরি করে এবং রিটার্ন করে। এই Hmac পাস করা অ্যালগরিদম এবং কি ব্যবহার করে। অপশনাল অপশনগুলো স্ট্রিম বিহевиয়ার কন্ট্রোল করতে ব্যবহৃত হয়। ডিফাইন করা কি HMAC কি হিসেবে ক্রিপ্টোগ্রাফিক HMAC হ্যাশ জেনারেট করতে ব্যবহৃত হয়।
সিনট্যাক্স
crypto.createHmac(algorithm, key, [options])
প্যারামিটার
algorithm — Hmac অবজেক্ট জেনারেট করার অ্যালগরিদম। ইনপুট টাইপ: স্ট্রিং।
key — ক্রিপ্টোগ্রাফিক Hmac হ্যাশ জেনারেট করার Hmac কি।
options — স্ট্রিম বিহевиয়ার কন্ট্রোলের অপশনাল প্যারামিটার।
encoding — স্ট্রিং এনকোডিং।
উদাহরণ ১
createHmac.js ফাইল তৈরি করে node createHmac.js রান করুন।
// crypto.createHmac() demo example // Importing the crypto module const crypto = require('crypto'); // Defining the secret key const secret = 'TutorialsPoint'; // Initializing the createHmac method using secret const hmacValue = crypto.createHmac('sha256', secret) // Data to be encoded .update('Welcome to TutorialsPoint !') // Defining encoding type .digest('hex'); // Printing the output console.log("Hmac value Obtained is: ", hmacValue);আউটপুট
C:\home\node>> node createHmac.js Hmac value Obtained is: dd897f858bad70329fad82087110059f5cb920af2736d96277801f70bd57618e
উদাহরণ ২
// crypto.createHmac() demo example // Importing the crypto & fs module const crypto = require('crypto'); const fs = require('fs'); // Getting the current file path const filename = process.argv[1]; // Creting hmac for current path using secret const hmac = crypto.createHmac('sha256', "TutorialsPoint"); const input = fs.createReadStream(filename); input.on('readable', () => { // Reading single element produced by hmac stream. const val = input.read(); if (val) hmac.update(val); else { console.log(`${hmac.digest('hex')} ${filename}`); } });আউটপুট
C:\home\node>> node createHmac.js 0489ce5e4bd940c06253764e03927414f79269fe4f91b1c75184dc074fa86e22 /home/node/test/createHmac .js