เมธอด crypto.generateKeyPair() ใช้สำหรับสร้างคู่กุญแจแบบไม่สมมาตร (asymmetric key pair) ของประเภทที่ระบุ ประเภทที่รองรับ ได้แก่ RSA, DSA, EC, Ed25519, Ed448, X25519, X448 และ DH ฟังก์ชันนี้จะทำงานเหมือนกับการเรียก keyObject.export บนผลลัพธ์เมื่อระบุ publicKeyEncoding หรือ privateKeyEncoding มิฉะนั้นจะคืนค่าส่วนที่เกี่ยวข้องของ keyObject โดยตรง
ไวยากรณ์
crypto.generateKeyPair(type, options, callback)
พารามิเตอร์
พารามิเตอร์ข้างต้นอธิบายดังนี้:
type – ระบุประเภทสตริงที่ต้องการสร้างกุญแจ ประเภทที่รองรับคือ RSA, DSA, EC, Ed25519, Ed448, X25519, X448 และ DH
options – สามารถระบุพารามิเตอร์ต่อไปนี้ได้:
modulusLength – ขนาดกุญแจในบิตสำหรับประเภท RSA และ DSA
publicExponent – ค่าตัวยกกำลังสาธารณะสำหรับอัลกอริทึม RSA ค่าเริ่มต้นคือ
0x10001divisorLength – ขนาดของ q ในบิต
namedCurve – ชื่อของคอนกรวย (curve) ที่จะใช้
prime – พารามิเตอร์เฉพาะสำหรับประเภทเช่น DH
primeLength – ความยาวเฉพาะในบิต
generator – ค่าเจเนอเรเตอร์ที่กำหนดเอง ค่าเริ่มต้นคือ
2groupName – ชื่อกลุ่ม Diffie-Hellman สำหรับอัลกอริทึม DH
publicKeyEncoding – ค่าสตริงสำหรับการเข้ารหัสกุญแจสาธารณะ
privateKeyEncoding – ค่าสตริงสำหรับการเข้ารหัสกุญแจส่วนตัว
callback – ฟังก์ชันเรียกกลับที่มีพารามิเตอร์ดังนี้:
err – ข้อผิดพลาดที่เกิดขึ้น (ถ้ามี)
publicKey – ค่าของกุญแจสาธารณะ
privateKey – ค่าของกุญแจส่วนตัว
ตัวอย่าง
สร้างไฟล์ชื่อ generateKeyPair.js และคัดลอกโค้ดด้านล่าง หลังจากสร้างไฟล์แล้ว ใช้คำสั่งต่อไปนี้เพื่อรันโค้ด:
node generateKeyPair.js
generateKeyPair.js
// โปรแกรม Node.js สาธิตการทำงานของเมธอด crypto.generateKeyPair()
// นำเข้า generateKeyPair จากโมดูล crypto
const { generateKeyPair } = require('crypto');
// เรียกใช้เมธอด generateKeyPair() ด้วยพารามิเตอร์ด้านล่าง
generateKeyPair('rsa', {
modulusLength: 530, // ตัวเลือก
publicExponent: 0x10101,
publicKeyEncoding: {
type: 'pkcs1',
format: 'der'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'der',
cipher: 'aes-192-cbc',
passphrase: 'Welcome to TutorialsPoint!'
}
}, (err, publicKey, privateKey) => { // ฟังก์ชันเรียกกลับ
if(!err) {
// แสดงคู่กุญแจแบบไม่สมมาตร
console.log("Public Key is: ", publicKey);
console.log();
console.log("Private Key is: ", privateKey);
} else {
// แสดงข้อผิดพลาดถ้ามี
console.log("Error is: ", err);
}
});
ผลลัพธ์
C:\home\node>> node generateKeyPair.js Public Key is: <Buffer 30 4a 02 43 03 43 66 5a e1 25 0d d8 c4 fe e3 a0 77 ea e4 e7 20 78 8c eb a7 a4 f6 85 f0 45 fc 7b 46 d7 3e 5e e3 8b a1 16 e5 59 e8 79 69 65 54 00 6c 89 ... > Private Key is: <Buffer 30 82 01 cd 30 57 06 09 2a 86 48 86 f7 0d 01 05 0d 30 4a 30 29 06 09 2a 86 48 86 f7 0d 01 05 0c 30 1c 04 08 1e a2 b4 ea ce 50 0e 80 02 02 08 00 30 0c ... >
ตัวอย่างเพิ่มเติม
ลองดูตัวอย่างอีกหนึ่งกรณี:
// โปรแกรม Node.js สาธิตการทำงานของเมธอด crypto.generateKeyPair()
// นำเข้า generateKeyPair จากโมดูล crypto
const { generateKeyPair } = require('crypto');
// เรียกใช้เมธอด generateKeyPair() ด้วยพารามิเตอร์ด้านล่าง
generateKeyPair('ec', {
namedCurve: 'secp256k1', // ตัวเลือก
publicKeyEncoding: {
type: 'spki',
format: 'der'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'der'
}
}, (err, publicKey, privateKey) => { // ฟังก์ชันเรียกกลับ
if(!err) {
// แสดงคู่กุญแจแบบไม่สมมาตร
console.log("Public Key is: ", publicKey);
console.log("Public Key in hex is: ", publicKey.toString('hex'));
console.log();
console.log("Private Key is: ", privateKey);
console.log("Private Key in hex is: ", privateKey.toString('hex'));
} else {
// แสดงข้อผิดพลาดถ้ามี
console.log("Error is: ", err);
}
});
ผลลัพธ์
C:\home\node>> node generateKeyPair.js Public Key is: <Buffer 30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04 00 0a 03 42 00 04 d1 d0 0b 7e f7 e3 3e cf d8 08 2a 20 a8 5e 52 be ca 56 29 42 c3 6b 9f d3 15 7c ... > Public Key in hex is: 3056301006072a8648ce3d020106052b8104000a03420004d1d00b7ef7e33ecfd8082a20a85e52beca562942c36b9fd3157cf98b03b41ecc4b4e13b4cd5cd35814383ee76afabaf794faf6f31bc8c9f7007748f74767677c Private Key is: <Buffer 30 81 84 02 01 00 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04 00 0a 04 6d 30 6b 02 01 01 04 20 05 01 a1 d5 80 f7 65 56 6f a3 3a 05 d3 6d ec 82 ad ... > Private Key in hex is: 308184020100301006072a8648ce3d020106052b8104000a046d306b02010104200501a1d580f765566fa33a05d36dec82ad590ff8697d182ddca6b881acfbadd1a14403420004d1d00b7ef7e33ecfd8082a20a85e52beca562942c36b9fd3157cf98b03b41ecc4b4e13b4cd5cd35814383ee76afabaf794faf6f31bc8c9f7007748f74767677c