1
我有C++代碼。我怎樣才能使用nodejs
模塊ffi
,ref
,ref-struct
,ref-array
NodeJS FFI - 創建結構數組
CK_BBOOL yes = CK_TRUE;
CK_BBOOL no = CK_FALSE;
// encryption/decryption sensitive key
CK_ATTRIBUTE key_template[] = {
{CKA_SENSITIVE, &yes, sizeof(CK_BBOOL)}, // key is sensitive: should not be readable
{CKA_ENCRYPT, &yes, sizeof(CK_BBOOL)} , // key can encrypt data
{CKA_DECRYPT, &yes, sizeof(CK_BBOOL)} , // key can decrypt data
};
CK_OBJECT_HANDLE key; // key handle for the new key
CK_MECHANISM gen_mec = {CKM_DES_KEY_GEN, NULL_PTR, 0}; // DES key
C_GenerateKey(session, &gen_mec, key_template, 3, &key); // generates the key
if (rv != CKR_OK) {
printf("Something went wrong while generating the key: %lu\n",rv);
exit(1);
} else
printf("Key generated!\n");
// now key 'points' to the freshly generated key
有關詳細信息這是一個PKCS11功能重複這樣的代碼。
我的JavaScript代碼是
var Templates = RefArray(CKI.CK_ATTRIBUTE);
var valueLen = new Buffer(4);
valueLen.writeUInt32LE(32, 0);
valueLen.type = CKI.CK_ULONG; //ulong
var bTrue = new Buffer(1);
bTrue.writeUInt8(1, 0);
bTrue.type = CKI.CK_BYTE; //uchar
var template0 = new CKI.CK_ATTRIBUTE({type: CKI.CKA_SENSITIVE, pValue: bTrue.ref(), ulValueLen: 1});
var template1 = new CKI.CK_ATTRIBUTE({type: CKI.CKA_VALUE_LEN, pValue: valueLen.ref(), ulValueLen: 4});
var template2 = new CKI.CK_ATTRIBUTE({type: CKI.CKA_ENCRYPT, pValue: bTrue.ref(), ulValueLen: 1});
var template3 = new CKI.CK_ATTRIBUTE({type: CKI.CKA_DECRYPT, pValue: bTrue.ref(), ulValueLen: 1});
var templates = new Templates(4);
templates[0] = template0;
templates[1] = template2;
templates[2] = template3;
templates[3] = template1;
var $hObject = Ref.alloc(CKI.CK_ULONG);
Debug('C_GenerateKey');
var res = this.cki.C_GenerateKey(this.handle, mech.ref(), templates.ref(), 4, $hObject);
Utils.check_cki_res(res, 'C_GenerateKey');
乳寧爲CKK_AES_KEY_GEN
機制這個代碼C_GenerateKey
返回錯誤 Error on Cryptoki function C_GenerateKey. Error is TemplateIncomplete(208)