cast to void[]

Alberto reda at zioale.it
Mon Feb 12 02:13:20 PST 2007


I have some problem casting a struct to void[]:

const ushort RSA_ENC_LEN =256;

struct RSAEncryptedData {
  ubyte encoding[RSA_ENC_LEN];
}

struct Signature {
  ubyte sig[RSA_ENC_LEN];
}

class RSA {
  ...
  Signature sign(void[] block, int type=Hash.SHA512);
  bool verifySig(void[] block, Signature sig, int type=Hash.SHA512);
  ...
}

int main() {
	ubyte[32] sessionkey;
	int ret;
	
	aes128 aes = new aes128(Bits256);
	aes.setRandomKey();
	sessionkey[0..$] = aes.getKey();

	RSA  rsatest = new RSA();
	RSAEncryptedData buf = rsatest.encryptWithPublicKey(sessionkey);
	
	// #1
	Signature sig = rsatest.sign(cast(void[])buf.encoding, Hash.SHA512);
	ret = rsatest.verifySig(cast(void[])buf.encoding, sig, Hash.SHA512);
	assert(ret == true);
	
	// #2
	void[] a;
	a.length = RSAEncryptedData.sizeof;
	memcpy(cast(void *)a, cast(void *)&buf, a.length);
	Signature sig2 = rsatest.sign(a, Hash.SHA512);
	ret = rsatest.verifySig(a, sig2, Hash.SHA512);	
	assert(ret == true);
	
	// #3
	Signature sig3 =
rsatest.sign(cast(void[])((&buf)[0..RSAEncryptedData.sizeof]), Hash.SHA512);
	ret =
rsatest.verifySig(cast(void[])((&buf)[0..RSAEncryptedData.sizeof]),
sig3, Hash.SHA512);
	assert(ret == true);

	return 0;
}

I got an Assertion failure on the 3° assert, where I have tried to cast
buf directly to void[].
In general, if I have something like this:

struct pippo {
   ushort a;
   ushort b;
   ubyte[170] c;
   uint d;
}

void do_something(void []);

What is the right way to pass pippo to do_something() ? something like #2 ?


More information about the Digitalmars-d-learn mailing list