本文整理汇总了C++中Hash::Wrap方法的典型用法代码示例。如果您正苦于以下问题:C++ Hash::Wrap方法的具体用法?C++ Hash::Wrap怎么用?C++ Hash::Wrap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Hash
的用法示例。
在下文中一共展示了Hash::Wrap方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Hash
static
NAN_METHOD(Copy) {
Hash *src = Nan::ObjectWrap::Unwrap<Hash>(info.This());
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>("bypass").ToLocalChecked() };
v8::Local<v8::FunctionTemplate> construct = Nan::New<v8::FunctionTemplate>(hash_constructor);
v8::Local<v8::Object> inst = construct->GetFunction()->NewInstance(argc, argv);
// Construction may fail with a JS exception, in which case we just need
// to return.
if(inst.IsEmpty()) {
return;
}
Hash *dest = new Hash();
dest->Wrap(inst);
dest->initialized_ = src->initialized_;
dest->any_blake2_update = src->any_blake2_update;
dest->any_blake2_final = src->any_blake2_final;
dest->outbytes = src->outbytes;
dest->state = src->state;
info.GetReturnValue().Set(inst);
}
示例2: ThrowError
static
NAN_METHOD(New) {
if(!info.IsConstructCall()) {
return Nan::ThrowError("Constructor must be called with new");
}
Hash *obj = new Hash();
obj->Wrap(info.This());
if(info.Length() < 1 || !info[0]->IsString()) {
return Nan::ThrowError(v8::Exception::TypeError(Nan::New<v8::String>("First argument must be a string with algorithm name").ToLocalChecked()));
}
std::string algo = std::string(*v8::String::Utf8Value(info[0]->ToString()));
const char *key_data = nullptr;
size_t key_length;
if(algo != "bypass" && info.Length() >= 2) {
if(!node::Buffer::HasInstance(info[1])) {
return Nan::ThrowError(v8::Exception::TypeError(Nan::New<v8::String>("If key argument is given, it must be a Buffer").ToLocalChecked()));
}
key_data = node::Buffer::Data(info[1]);
key_length = node::Buffer::Length(info[1]);
}
if(algo == "bypass") {
// Initialize nothing - .copy() will set up all the state
} else if(algo == "blake2b") {
if(!key_data) {
if(blake2b_init(reinterpret_cast<blake2b_state*>(&obj->state), BLAKE2B_OUTBYTES) != 0) {
return Nan::ThrowError("blake2b_init failure");
}
} else {
if(key_length > BLAKE2B_KEYBYTES) {
return Nan::ThrowError("Key must be 64 bytes or smaller");
}
if(blake2b_init_key(reinterpret_cast<blake2b_state*>(&obj->state), BLAKE2B_OUTBYTES, key_data, key_length) != 0) {
return Nan::ThrowError("blake2b_init_key failure");
}
}
obj->outbytes = 512 / 8;
obj->any_blake2_update = BLAKE_FN_CAST(blake2b_update);
obj->any_blake2_final = BLAKE_FN_CAST(blake2b_final);
obj->initialized_ = true;
} else if(algo == "blake2bp") {
if(!key_data) {
if(blake2bp_init(reinterpret_cast<blake2bp_state*>(&obj->state), BLAKE2B_OUTBYTES) != 0) {
return Nan::ThrowError("blake2bp_init failure");
}
} else {
if(key_length > BLAKE2B_KEYBYTES) {
return Nan::ThrowError("Key must be 64 bytes or smaller");
}
if(blake2bp_init_key(reinterpret_cast<blake2bp_state*>(&obj->state), BLAKE2B_OUTBYTES, key_data, key_length) != 0) {
return Nan::ThrowError("blake2bp_init_key failure");
}
}
obj->outbytes = 512 / 8;
obj->any_blake2_update = BLAKE_FN_CAST(blake2bp_update);
obj->any_blake2_final = BLAKE_FN_CAST(blake2bp_final);
obj->initialized_ = true;
} else if(algo == "blake2s") {
if(!key_data) {
if(blake2s_init(reinterpret_cast<blake2s_state*>(&obj->state), BLAKE2S_OUTBYTES) != 0) {
return Nan::ThrowError("blake2bs_init failure");
}
} else {
if(key_length > BLAKE2S_KEYBYTES) {
return Nan::ThrowError("Key must be 32 bytes or smaller");
}
if(blake2s_init_key(reinterpret_cast<blake2s_state*>(&obj->state), BLAKE2S_OUTBYTES, key_data, key_length) != 0) {
return Nan::ThrowError("blake2s_init_key failure");
}
}
obj->outbytes = 256 / 8;
obj->any_blake2_update = BLAKE_FN_CAST(blake2s_update);
obj->any_blake2_final = BLAKE_FN_CAST(blake2s_final);
obj->initialized_ = true;
} else if(algo == "blake2sp") {
if(!key_data) {
if(blake2sp_init(reinterpret_cast<blake2sp_state*>(&obj->state), BLAKE2S_OUTBYTES) != 0) {
return Nan::ThrowError("blake2sp_init failure");
}
} else {
if(key_length > BLAKE2S_KEYBYTES) {
return Nan::ThrowError("Key must be 32 bytes or smaller");
}
if(blake2sp_init_key(reinterpret_cast<blake2sp_state*>(&obj->state), BLAKE2S_OUTBYTES, key_data, key_length) != 0) {
return Nan::ThrowError("blake2sp_init_key failure");
}
}
obj->outbytes = 256 / 8;
obj->any_blake2_update = BLAKE_FN_CAST(blake2sp_update);
obj->any_blake2_final = BLAKE_FN_CAST(blake2sp_final);
obj->initialized_ = true;
} else {
return Nan::ThrowError("Algorithm must be blake2b, blake2s, blake2bp, or blake2sp");
}
info.GetReturnValue().Set(info.This());
}