// ----------------------------- // ---- 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(nameof(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. UnmanagedBuffer newBuffer = UnmanagedBuffer.Allocate(capacity * sizeof(char)); if (_buffer != null) { UnmanagedBuffer.Copy(_buffer, newBuffer, _buffer.ByteLength); _buffer.Dispose(); } _buffer = newBuffer; }
internal 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) { UnmanagedBuffer.Copy(str._buffer, _buffer, (ulong)(str._decryptedLength * sizeof(char))); } }
private SecureString(SecureString str) { Debug.Assert(str._buffer != null, "Expected other SecureString's buffer to be non-null"); Debug.Assert(str._encrypted, "Expected to be used only on encrypted SecureStrings"); _buffer = UnmanagedBuffer.Allocate((int)str._buffer.ByteLength); Debug.Assert(_buffer != null); UnmanagedBuffer.Copy(str._buffer, _buffer, str._buffer.ByteLength); _decryptedLength = str._decryptedLength; _encrypted = str._encrypted; }
private void EnsureCapacity(int capacity) { if (capacity > MaxLength) { throw new ArgumentOutOfRangeException(nameof(capacity), SR.ArgumentOutOfRange_Capacity); } Debug.Assert(_buffer != null); if ((uint)capacity * sizeof(char) <= _buffer.ByteLength) { return; } UnmanagedBuffer oldBuffer = _buffer; UnmanagedBuffer newBuffer = UnmanagedBuffer.Allocate(GetAlignedByteSize(capacity)); UnmanagedBuffer.Copy(oldBuffer, newBuffer, (uint)_decryptedLength * sizeof(char)); _buffer = newBuffer; oldBuffer.Dispose(); }