Good day guys
I'm inserting an array into another array with the Array.copy function. The destination array is fixed. Its a byte[157]. The first 64 elements and the last 77 elements stay the same only the middle 16 elements change. So I say:
<br />
Array.copy(source,0,dest,64,16);<br />
Isn't there a faster way of doing this? I know about memoryStreams but the memoryStream.toArray() eats up performance and it creates a copy of the array which uses memory I don't have. I'm developing on a smart card. Maybe the full code will help. This is a Hmac function used for tls.
private void ComputeMyHMac_hash(ref HashAlgorithm alg, ref byte[] secret, byte[] seed, ref byte[] output,int size)
{
#region pads
#region inpad
byte[] inpad = { 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
};
#endregion
#region outpad
byte[] outpad = { 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
};
#endregion
for (int i = 0; i < secret.Length; i++)
{
inpad[i] ^= secret[i];
outpad[i] ^= secret[i];
}
#endregion
secret = null;
byte[] buf = new byte[size];
byte[] buf2 = new byte[size];
byte[] worker = new byte[64+size];
Array.Copy(inpad, worker, inpad.Length);
byte[] worker2 = new byte[64 + seed.Length];
Array.Copy(inpad,worker2,inpad.Length);
Array.Copy(seed, 0, worker2, 64, seed.Length);
byte[] tmp = new byte[size];
byte[] worker3 = new byte[64 + size];
Array.Copy(outpad, worker3, outpad.Length);
outpad = null;
MemoryStream outStr = new MemoryStream(0);
int iterations = (output.Length + size - 1) / size;
for (int i = 0; i < iterations; i++)
{
if (i == 0)
{
DoFinal(ref buf, worker2, ref alg, ref tmp, ref worker3);
worker2 = new byte[64 + size + seed.Length];
Array.Copy(inpad, worker2, inpad.Length);
inpad = null;
Array.Copy(seed, 0, worker2, 64 +size, seed.Length);
}
else
{
Array.Copy(seed, 0, worker, 64, seed.Length);
DoFinal(ref buf, worker, ref alg, ref tmp, ref worker3);
}
seed = buf;
Array.Copy(seed, 0, worker2, 64, seed.Length);
DoFinal(ref buf2, worker2, ref alg, ref tmp, ref worker3);
outStr.Write(buf2, 0, buf2.Length);
}
output = outStr.ToArray();
outStr.Close();
}
private void DoFinal(ref byte[] buf, byte[] worker, ref HashAlgorithm alg, ref byte[] tmp, ref byte[] worker3)
{
tmp = alg.ComputeHash(worker);
Array.Copy(tmp, 0, worker3, 64, tmp.Length);
buf = alg.ComputeHash(worker3);
}
The hash algorithm can be MD5 or Sha1, The size is the size of the hash algorithm either 16 or 20.
Hopefully someone can help. Any advice would be appreciated.
Thanks
Btw: Where can I find the editing tags to make the post look good? To put the code in a nice code block that the indentation is correct?