// ----------------------------- // ---- PAL layer ends here ---- // ----------------------------- private void EnsureCapacity(int capacity) { // Make sure the requested capacity doesn't exceed SecureString's defined limit if (capacity > MaxLength) { throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_Capacity); } // If we already have enough space allocated, we're done if (_buffer != null && (capacity * sizeof(char)) <= (int)_buffer.ByteLength) { return; } // We need more space, so allocate a new buffer, copy all our data into it, // and then swap the new for the old. ProtectedBuffer newBuffer = ProtectedBuffer.Allocate(capacity * sizeof(char)); if (_buffer != null) { using (_buffer.Unprotect()) ProtectedBuffer.Copy(_buffer, newBuffer, _buffer.ByteLength); newBuffer.Protect(); _buffer.Dispose(); } _buffer = newBuffer; }
[System.Security.SecurityCritical] // auto-generated internal unsafe SecureString(SecureString str) { // Allocate enough space to store the provided string EnsureCapacity(str._decryptedLength); _decryptedLength = str._decryptedLength; // Copy the string into the newly allocated space if (_decryptedLength > 0) { using (str._buffer.Unprotect()) ProtectedBuffer.Copy(str._buffer, _buffer, (ulong)(str._decryptedLength * sizeof(char))); } // Protect the buffer _buffer.Protect(); }