Mô tả
Phương thức crypto.pbkdf2Sync(), còn gọi là Password-Based Key Derivation function 2, cung cấp triển khai đồng bộ (synchronous) của hàm dẫn xuất khóa. Một khóa được dẫn xuất bằng cách sử dụng Hmac digest của thuật toán xác định từ password, salt và iterations. Quá trình này tạo khóa theo quy trình đồng bộ.
Cú pháp
crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)
Tham số
password – Mật khẩu để lấy khóa độ dài byte yêu cầu. Các giá trị có thể: string, DataView, Buffer, v.v.
salt – Tương tự password để lấy khóa. Các giá trị có thể: string, DataView, Buffer, v.v.
iterations – Lấy khóa mong muốn độ dài byte yêu cầu. Chấp nhận giá trị number.
keylen – Độ dài byte yêu cầu của khóa. Kiểu number.
digest – Thuật toán Hmac được chỉ định bởi giá trị digest này. Giá trị mặc định: 'sha1'.
Ví dụ
Tạo file tên pbkdf2Sync.js và sao chép đoạn mã dưới. Sau khi tạo file, dùng lệnh sau để chạy:
node pbkdf2Sync.js
pbkdf2Sync.js
// crypto.pbkdf2() demo example // Importing the crypto module const crypto = require('crypto'); // Defining the pbkdf2 with the following options const pbkdfKey = crypto.pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512'); // Printing the derivedKey console.log("key is: ",pbkdfKey.toString('hex'));Đầu ra
C:\home\node>> node pbkdf2Sync.js key is: 3745e482c6e0ade35da10139e797157f4a5da669dad7d5da88ef87e47471cc47ed941c7ad618e8 27304f083f8707f12b7cfdd5f489b782f10cc269e3c08d59ae
Ví dụ khác
Xem thêm một ví dụ nữa.
// crypto.pbkdf2Sync () demo example // Importing the crypto module const crypto = require('crypto'); // Defining the pbkdf2Sync with the following options const pbkdfKey = crypto.pbkdf2Sync('secret', 'salt', 100, 32, 'sha1'); // Printing the derivedKey console.log("key is: ",pbkdfKey); console.log("key(in hex) is: ",pbkdfKey.toString('hex')); console.log("key(in base64) is: ",pbkdfKey.toString('base64'));Đầu ra
C:\home\node>> node pbkdf2Sync.js key is:key(in hex) is: b73635f7c0882e1fc3ba6e29b14af1274df84828b4d18fcc222eb574455f505d key(in base64) is: tzY198CILh/Dum4psUrxJ034SCi00Y/Mii61dEVfUF0=