我有 C++ 代码。我怎样才能使用nodejs
模块重复这样的代码ffi
,,,,ref
ref-struct
ref-array
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)