I am writing a stream that needs to be aligned to a particular size. What is the right way of appending null (or any value) without exposing sensitive areas of RAM outside Array.Copy?
For example, I need to “round up” byteArray.Length to the value of pad:
byte[] byteArray = Encoding.ASCII.GetBytes(preStream);
long pad = (minBlobEntrySize * databaseCount) - byteArray.Length;
Array.Copy(byteArray, 0, buffer, relativeOffset2, byteArray.Length);
And I know that the following is wrong since it may expose sensitive data:
Array.Copy(byteArray, 0, buffer, relativeOffset2, byteArray.Length + pad);
What is the correct way to handle this?
Looks like the
Array.Resizemethod should do what you want here.Note that you don’t need the
+ padin theCopymethod as the length will be the resized one.