Exemple #1
0
	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);
	}
Exemple #2
0
	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());
	}