SecureString.Unix.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. namespace System.Security
  9. {
  10. // SecureString attempts to provide a defense-in-depth solution.
  11. //
  12. // On Windows, this is done with several mechanisms:
  13. // 1. keeping the data in unmanaged memory so that copies of it aren't implicitly made by the GC moving it around
  14. // 2. zero'ing out that unmanaged memory so that the string is reliably removed from memory when done with it
  15. // 3. encrypting the data while it's not being used (it's unencrypted to manipulate and use it)
  16. //
  17. // On Unix, we do 1 and 2, but we don't do 3 as there's no CryptProtectData equivalent.
  18. public sealed partial class SecureString
  19. {
  20. private static int GetAlignedByteSize(int length)
  21. {
  22. return Math.Max(length, 1) * sizeof(char);
  23. }
  24. private void ProtectMemory()
  25. {
  26. _encrypted = true;
  27. }
  28. private void UnprotectMemory()
  29. {
  30. _encrypted = false;
  31. }
  32. }
  33. }