Ejemplo n.º 1
0
Blob RippleAddress::accountPrivateSign (Blob const& message) const
{
    if (vchData.size() == 33  &&  vchData[0] == 0xED)
    {
        uint8_t const*      secretKey = &vchData[1];
        ed25519_public_key  publicKey;
        Blob                signature (sizeof (ed25519_signature));

        ed25519_publickey (secretKey, publicKey);

        ed25519_sign (
            message.data(), message.size(), secretKey, publicKey,
            &signature[0]);

        assert (isCanonicalEd25519Signature (signature.data()));

        return signature;
    }

    Blob result = ECDSASign(
        sha512Half(makeSlice(message)), getAccountPrivate());
    bool const ok = !result.empty();

    CondLog (!ok, lsWARNING, RippleAddress)
            << "accountPrivateSign: Signing failed.";

    return result;
}
Ejemplo n.º 2
0
bool
PublicKey::checkSignature(const Blob& data, const Blob& signature) const {
    if (!pk)
        return false;
    const gnutls_datum_t sig {(uint8_t*)signature.data(), (unsigned)signature.size()};
    const gnutls_datum_t dat {(uint8_t*)data.data(), (unsigned)data.size()};
    int rc = gnutls_pubkey_verify_data2(pk, GNUTLS_SIGN_RSA_SHA512, 0, &dat, &sig);
    return rc >= 0;
}
Ejemplo n.º 3
0
Blob CngKey::Export(CngKeyBlobFormat format) const {
	EVP_PKEY* key = ToKey(_self);
	Blob r;
	int size;
	byte *buf;
	switch (format) {
	case CngKeyBlobFormat::OSslEccPrivateBlob:
		SslCheck((size = ::i2d_PrivateKey(key, 0)) >= 0);
		r.Size = size;
		SslCheck(::i2d_PrivateKey(key, &(buf = r.data())) == size);
		break;
	case CngKeyBlobFormat::OSslEccPrivateBignum:
		{
			const BIGNUM *bn = ::EC_KEY_get0_private_key(key->pkey.ec);
			SslCheck(bn);
			r.Size = BN_num_bytes(bn);
			if (::BN_bn2bin(bn, r.data()) != r.Size)
				Throw(E_FAIL);
		}
		break;
	case CngKeyBlobFormat::OSslEccPublicBlob:
//		SslCheck(size = ::i2o_ECPublicKey(key, 0));
		SslCheck((size = ::i2d_PublicKey(key, 0)) >= 0);
		r.Size = size;
//		SslCheck(::i2o_ECPublicKey(key, &(buf = r.data())) == size);
		SslCheck(::i2d_PublicKey(key, &(buf = r.data())) == size);
		break;
	case CngKeyBlobFormat::OSslEccPublicCompressedBlob:
		{
			CngKey tmp = _self;
			key = ToKey(tmp);
			::EC_KEY_set_conv_form(key->pkey.ec, POINT_CONVERSION_COMPRESSED);
			r = tmp.Export(CngKeyBlobFormat::OSslEccPublicBlob);
		}
		break;
	case CngKeyBlobFormat::OSslEccPublicUncompressedBlob:
		{
			CngKey tmp = _self;
			key = ToKey(tmp);
			::EC_KEY_set_conv_form(key->pkey.ec, POINT_CONVERSION_UNCOMPRESSED);
			r = tmp.Export(CngKeyBlobFormat::OSslEccPublicBlob);
		}
		break;
	case CngKeyBlobFormat::OSslEccPrivateCompressedBlob:
		{
			CngKey tmp = _self;
			key = ToKey(tmp);
			::EC_KEY_set_conv_form(key->pkey.ec, POINT_CONVERSION_COMPRESSED);
			r = tmp.Export(CngKeyBlobFormat::OSslEccPrivateBlob);
		}
		break;
	default:
		Throw(E_INVALIDARG);
	}
	return r;
}
Ejemplo n.º 4
0
void Blob::SetData(Blob &source) {
  if (data_)
    data_ = nullptr;
  shape_ = source.shape();
  count_ = num() * channels() * height() * width();
  data_ = source.data();
}
Ejemplo n.º 5
0
Mat* MyImage::getOpenCVMat(){
  if(mat == NULL)  {
    try {
      Mat orig;
      getMagickImage();
      magick.magick("BGR");
      Blob blb ;
      magick.write(&blb);
      mat = new Mat();
      orig = Mat(magick.size().height(),
  	       magick.size().width(), 
  	       CV_8UC3, 
  	       (void *) blb.data());
      cvtColor(orig, *mat, CV_RGB2HSV);
      magick.magick("RGB");
    }
    catch (Magick::Exception &e)
    {
      cout << "Caught a magick exception: " << e.what() << endl;
      throw e;
    }
  }
  return mat;

}
Ejemplo n.º 6
0
Blob::Blob(const Blob &source) {
  if (data_)
    data_ = nullptr;
  shape_ = source.shape();
  count_ = source.count();
  data_ = source.data();
}
Ejemplo n.º 7
0
 void PreparedStatement::setBlob(int column, const Blob &value)
 {
     locker l(d->mutex);
     if (!d->prepared) {
         return;
     }
     sqlite3_bind_blob(d->stmt, column, value.data(), value.size(), SQLITE_STATIC);
 }
Ejemplo n.º 8
0
    void setBlob(MYSQL_BIND& bind, unsigned long& length, const Blob& data)
    {
      length = data.size();
      reserve(bind, length);
      memcpy(static_cast<char*>(bind.buffer), data.data(), length);

      bind.buffer_type = MYSQL_TYPE_VAR_STRING;
      bind.is_null = 0;
      bind.length = &length;
    }
Ejemplo n.º 9
0
bool RippleAddress::verifySignature(uint256 const& hash, Blob const& vchSig) const
{
    if (vchData.size() != crypto_sign_PUBLICKEYBYTES
        || vchSig.size () != crypto_sign_BYTES)
        throw std::runtime_error("bad inputs to verifySignature");

    bool verified = crypto_sign_verify_detached(vchSig.data(),
                 hash.data(), hash.bytes, vchData.data()) == 0;
    bool canonical = signatureIsCanonical (vchSig);
    return verified && canonical;
}
Ejemplo n.º 10
0
static
uint160 Hash160 (Blob const& vch)
{
    uint256 hash1;
    SHA256 (vch.data (), vch.size (), hash1.data ());

    uint160 hash2;
    RIPEMD160 (hash1.data (), hash1.size (), hash2.data ());

    return hash2;
}
Ejemplo n.º 11
0
Blob
PrivateKey::sign(const Blob& data) const
{
    if (!key)
        throw CryptoException("Can't sign data: no private key set !");
    gnutls_datum_t sig;
    const gnutls_datum_t dat {(unsigned char*)data.data(), (unsigned)data.size()};
    if (gnutls_privkey_sign_data(key, GNUTLS_DIG_SHA512, 0, &dat, &sig) != GNUTLS_E_SUCCESS)
        throw CryptoException("Can't sign data !");
    Blob ret(sig.data, sig.data+sig.size);
    gnutls_free(sig.data);
    return ret;
}
Ejemplo n.º 12
0
 void prepare(svector<iovec>& v, void* ptr, const int sender=-1) {
     Task<Image> * task = (Task<Image> *) ptr;
     Blob* blob = task->getBlob();
     task->get()->magick("BMP");
     task->get()->write(blob);
     struct iovec iov={const_cast<void*>(blob->data()),blob->length()};
     v.push_back(iov);
     setCallbackArg(ptr);
     const char * imagename = task->name().c_str();
     struct iovec iov2={const_cast<char*>(imagename),strlen(imagename)+1};
     v.push_back(iov2);
     setCallbackArg(nullptr);
 }    
Ejemplo n.º 13
0
void
PublicKey::unpack(Blob::const_iterator& begin, Blob::const_iterator& end)
{
    Blob tmp = deserialize<Blob>(begin, end);
    if (pk)
        gnutls_pubkey_deinit(pk);
    gnutls_pubkey_init(&pk);
    const gnutls_datum_t dat {(uint8_t*)tmp.data(), (unsigned)tmp.size()};
    int err = gnutls_pubkey_import(pk, &dat, GNUTLS_X509_FMT_PEM);
    if (err != GNUTLS_E_SUCCESS)
        err = gnutls_pubkey_import(pk, &dat, GNUTLS_X509_FMT_DER);
    if (err != GNUTLS_E_SUCCESS)
        throw CryptoException(std::string("Could not read public key: ") + gnutls_strerror(err));
}
Ejemplo n.º 14
0
bool Base58::decodeWithCheck (const char* psz, Blob& vchRet, Alphabet const& alphabet)
{
    if (!decode (psz, vchRet, alphabet))
        return false;

    auto size = vchRet.size ();

    if (size < 4)
    {
        vchRet.clear ();
        return false;
    }

    uint256 hash = SHA256Hash (vchRet.data (), vchRet.data () + size - 4);

    if (memcmp (&hash, &vchRet.end ()[-4], 4) != 0)
    {
        vchRet.clear ();
        return false;
    }

    vchRet.resize (size - 4);
    return true;
}
Ejemplo n.º 15
0
	// create from a base58 encoded seed
	bool PaysharesPrivateKey::fromString(std::string& base58seed)
	{
		Blob vchTemp;
		Base58::decodeWithCheck(base58seed.c_str(), vchTemp, Base58::getRippleAlphabet());

		if (vchTemp.empty() &&
			vchTemp.size() == crypto_sign_SEEDBYTES + 1 &&
			vchTemp[0] == RippleAddress::VER_SEED)
		{
			return(false);
		}

		mSeed.resize(crypto_sign_SEEDBYTES);
		memcpy(&mSeed[0], vchTemp.data() + 1, crypto_sign_SEEDBYTES);
		mPair.setSeed(mSeed);
		return(true);
	}
Ejemplo n.º 16
0
NodeObject::Ptr DecodedBlob::createObject ()
{
    bassert (m_success);

    NodeObject::Ptr object;

    if (m_success)
    {
        Blob data (m_dataBytes);

        memcpy (data.data (), m_objectData, m_dataBytes);

        object = NodeObject::createObject (
            m_objectType, m_ledgerIndex, data, uint256::fromVoid (m_key));
    }

    return object;
}
Ejemplo n.º 17
0
static char* read_file(const char *fname) {
    Blob blob;
    size_t data_size;
    char *dib;
    try {
        Image image(fname);
        preprocess_image(image);

        // Write to BLOB in BMP format
        image.write(&blob, "DIB");
    } catch(Exception &error_) {
        cerr << error_.what() << "\n";
        return NULL;
    }
    data_size = blob.length();
    dib = new char[data_size];
    memcpy(dib, blob.data(), data_size);
    return dib;
}
Ejemplo n.º 18
0
void displayer_png::display(const buffer & b, ostream & out)
{
	Geometry size(b.get_width(), b.get_height());
	Color background(0, 0, 0, 0);
	Image image(size, background);

	for (unsigned int h=0; h<b.get_height(); h++)
	{
		for (unsigned int w=0; w<b.get_width(); w++)
		{
			unsigned int c = (unsigned int)(MaxRGB * b(h,w));
			Color color(c, c, c);
			image.pixelColor(w, h, color);
		}
	}
	Blob blob;
	image.magick("PNG");
	image.write(&blob);
	out.write((const char *)blob.data(), (long unsigned int)blob.length());
}
Ejemplo n.º 19
0
Blob Scrypt(const ConstBuf& password, const ConstBuf& salt, int n, int r, int p, size_t dkLen) {
	HmacPseudoRandomFunction<SHA256> prf;
	const size_t mfLen = r*128;
	Blob bb = PBKDF2(prf, password, salt, 1, p*mfLen);
	AlignedMem am((n+1)*r*128, 128);
	uint32_t *v = (uint32_t*)am.get();
	SalsaBlockPtr tmp = (SalsaBlockPtr)alloca(r*128);
	for (int i=0; i<p; ++i) {
		SalsaBlockPtr x = (SalsaBlockPtr)(bb.data()+i*mfLen);
		for (int i=0; i<n; ++i) {
			memcpy(&v[i * 32 * r], x, 2*r * sizeof(x[0]));
			VectorMix(Salsa20Core, x, tmp, r, 8);
		}
		for (int i=0; i<n; ++i) {
			int j = 32*r * (x[2 * r - 1][0] & (n - 1));
			VectorXor(x[0], &v[j], 2*r*16);
			VectorMix(Salsa20Core, x, tmp, r, 8);
		}
	}
	return PBKDF2(prf, password, bb, 1, dkLen);
}
Ejemplo n.º 20
0
bool RippleAddress::accountPublicVerify (
    Blob const& message, Blob const& vucSig, ECDSA fullyCanonical) const
{
    if (vchData.size() == 33  &&  vchData[0] == 0xED)
    {
        if (vucSig.size() != 64)
        {
            return false;
        }

        uint8_t const* publicKey = &vchData[1];
        uint8_t const* signature = &vucSig[0];

        return !ed25519_sign_open (message.data(), message.size(),
                                   publicKey, signature)
                && isCanonicalEd25519Signature (signature);
    }

    return verifySignature (getAccountPublic(),
        sha512Half(makeSlice(message)), vucSig,
            fullyCanonical);
}
Ejemplo n.º 21
0
PrivateKey::PrivateKey(const Blob& import)
{
    if (gnutls_global_init() != GNUTLS_E_SUCCESS)
        throw CryptoException("Can't initialize GnuTLS.");
    int err = gnutls_x509_privkey_init(&x509_key);
    if (err != GNUTLS_E_SUCCESS)
        throw CryptoException("Can't initialize private key !");

    const gnutls_datum_t dt {(uint8_t*)import.data(), static_cast<unsigned>(import.size())};
    err = gnutls_x509_privkey_import2(x509_key, &dt, GNUTLS_X509_FMT_PEM, nullptr, GNUTLS_PKCS_PLAIN);
    if (err != GNUTLS_E_SUCCESS)
        err = gnutls_x509_privkey_import2(x509_key, &dt, GNUTLS_X509_FMT_DER, nullptr, GNUTLS_PKCS_PLAIN);
    if (err != GNUTLS_E_SUCCESS) {
        gnutls_x509_privkey_deinit(x509_key);
        throw CryptoException("Can't load private key !");
    }

    gnutls_privkey_init(&key);
    if (gnutls_privkey_import_x509(key, x509_key, GNUTLS_PRIVKEY_IMPORT_COPY) != GNUTLS_E_SUCCESS) {
        throw CryptoException("Can't load generic private key !");
    }
}
Ejemplo n.º 22
0
void SparseMatrixServerTable<T>::UpdateAddState(int worker_id,
  Blob keys_blob) {
  size_t keys_size = keys_blob.size<integer_t>();
  integer_t* keys = reinterpret_cast<integer_t*>(keys_blob.data());
  // add all values
  if (keys_size == 1 && keys[0] == -1) {
    for (auto id = 0; id < workers_nums_; ++id) {
      if (id == worker_id) continue;
      for (auto local_row_id = 0; local_row_id < this->my_num_row_; ++local_row_id) {
        // if other worker doesn't update the row, we can marked it as the updated.
        up_to_date_[id][local_row_id] = false;
      }
    }
  } else {
    for (auto id = 0; id < workers_nums_; ++id) {
      if (id == worker_id) continue;
      for (auto i = 0; i < keys_size; ++i) {
        // if other worker doesn't update the row, we can marked it as the updated.
        auto local_row_id = GetPhysicalRow(keys[i]);
        up_to_date_[id][local_row_id] = false;
      }
    }
  }
}
Ejemplo n.º 23
0
void STObject::setFieldVL (SField const& field, Blob const& v)
{
    setFieldUsingSetValue <STBlob>
            (field, Buffer(v.data (), v.size ()));
}
void Blob<Dtype>::ShareData(const Blob& other) {
  CHECK_EQ(count_, other.count());
  data_ = other.data(); // 赋值操作共享other中的资源,并停止对原有资源的共享
}
Ejemplo n.º 25
0
const Field & operator=(int v) { 
        _value.data(NULL);
        _modified = true;
        return *this;
    }
Ejemplo n.º 26
0
	//	change the shared_ptr object and will recycle the memory if need
	void shareData(const Blob& blob) {
		CHECK_EQ(count(), blob.count());
		data_ = blob.data(); 
	}
Ejemplo n.º 27
0
Archivo: blob.cpp Proyecto: MaoXu/jade
void Blob<Dtype>::ShareData(const Blob& other) {
  CHECK_EQ(count_, other.count());
  data_ = other.data();
}
Ejemplo n.º 28
0
msgpack::unpacked
unpackMsg(Blob b) {
    return msgpack::unpack((const char*)b.data(), b.size());
}
Ejemplo n.º 29
0
 void ClusterImpl::addBlob(const Blob& blob)
 {
   log_debug1("addBlob(ptr, " << blob.size() << ')');
   data.insert(data.end(), blob.data(), blob.end());
   offsets.push_back(data.size());
 }
Ejemplo n.º 30
0
int main(int argc, char** argv) {

	int rank, size;

	int radius = atoi(argv[2]);
	float weight = atof(argv[3]);
	MPI_Init (&argc, &argv);

	MPI_Comm_rank (MPI_COMM_WORLD, &rank);
	MPI_Comm_size (MPI_COMM_WORLD, &size);

	InitializeMagick(argv[0]);

	Image image;

	try {
		// Read a file into image object
		if(rank==0){
			image.read( argv[1] );

			int nx = image.columns();
			int ny = image.rows();
			Image im0(image);
			im0.crop(Geometry(nx/size, ny, 0, 0));

			for(int ip=1;ip<size;ip++){
				Image im(image);

				im.crop(Geometry(nx/size, ny, (nx/size)*ip, 0));

				Blob blob;
				im.write(&blob);
				int l = blob.length();
				MPI_Send(&l, 1, MPI_INT, ip, 0, MPI_COMM_WORLD);
				MPI_Send((void*)blob.data(), blob.length(), MPI_CHAR, ip, 0, MPI_COMM_WORLD);
			}

			im0.blur(radius, weight);
			image.draw(DrawableCompositeImage(0,0,im0));

			for(int ip=1;ip<size;ip++){
				int l;
				MPI_Recv(&l, 1, MPI_INT, ip, ip, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
				void* d = malloc(l);
				MPI_Recv(d, l, MPI_CHAR, ip, ip, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
				Blob blob_(d, l);
				Image im(blob_);
				image.draw(DrawableCompositeImage((nx/size)*ip,0,im));
				free(d);
			}
		}
		if(rank>0){

			int l;
			MPI_Recv(&l, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
			void* d = malloc(l);
			MPI_Recv(d, l, MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

			Blob blob(d, l);
			Image im(blob);
			im.blur(radius, weight);
			im.write(&blob);
			MPI_Send(&l, 1, MPI_INT, 0, rank, MPI_COMM_WORLD);
			MPI_Send((void*)blob.data(), blob.length(), MPI_CHAR, 0, rank, MPI_COMM_WORLD);
		}


		if(rank==0){
			image.write( argv[2] );
		}
	}
	catch( Exception &error_ )
	{
		cout << "Caught exception: " << error_.what() << endl;

	}


	MPI_Finalize();

	return 0;
}