Skip to content

SafeString

undergroundwires edited this page Feb 15, 2020 · 16 revisions

SafeString : Protect your strings

SafeString represents an encrypted string that's not leaked in the memory. It has more advantages than similar classes because of the security design of the application. It's modifiable and can work with different encodings.

Example usage:

 var safeString = new SafeString();
 await safeString.AppendAsync('t');
 await safeString.AppendAsync('e');
 await safeString.AppendAsync("st");
 //safeString will hold encrypted "test" text

SafeString uses SafeBytes as its internal data storage. That makes SafeString safe to both modify and compare with other string and SafeString instances.

SecureString SafeString
Supports multiple encodings
Safely character insert
Safely character remove
Safely equals
Safely retrieve
Unlimited characters

Safe to modify

SecureString is only char appendable and you need to reveal the sensitive information in order to be able to modify it. But SafeString can be safely modified by inserting chars/strings/bytes, deleting, replacing and more.

Safe to compare

SecureString's are not comparable with each other. But the security design and architecture of SafeString makes it safe to compare with another sensitive information in a secure context. The equality checks provides protection against timing attacks.

Retrieve the protected data

SecureString is not retrievable, but SafeString is. You can:

  • Reveal char by char using RevealDecryptedCharAsync without any performance trade-off.

  • Reveal the string as bytes using RevealDecryptedBytesAsync.

  • Get the inner encrypted ISafeBytes instance per character using GetAsSafeBytes.

  • Or you can get the whole string as disposable string in memory using RevealDecryptedStringAsync. Example:

          using(var secret = await safeString.RevealDecryptedStringAsync())
          {
              // Use secret.String here.  While in the 'using' block, the string is accessible
              // but pinned in memory.  When the 'using' block terminates, the string is zeroed
              // out for security, and garbage collected as usual.
          }

And do not forget, it might not be a good idea to reveal information in the memory even though it appears in milliseconds.

Supported encodings

  • Ascii : Standard ASCII.
  • Utf16LittleEndian : UTF-16 with little endian byte order
  • Utf16BigEndian : UTF-16 with big endian byte order

See also

Clone this wiki locally