-
Notifications
You must be signed in to change notification settings - Fork 5
SafeString
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" textSafeString uses SafeBytes as its internal data storage. That makes SafeString safe to both modify and compare with other string and SafeString instances.
SafeString vs System.Security.SecureString
| SecureString | SafeString | |
|---|---|---|
| Supports multiple encodings | ✖ | ✔ |
| Safely character insert | ✖ | ✔ |
| Safely character remove | ✖ | ✔ |
| Safely equals | ✖ | ✔ |
| Safely retrieve | ✖ | ✔ |
| Unlimited characters | ✖ | ✔ |
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.
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.
SecureString is not retrievable, but SafeString is. You can:
-
Reveal char by char using
RevealDecryptedCharAsyncwithout any performance trade-off. -
Reveal the string as bytes using
RevealDecryptedBytesAsync. -
Get the inner encrypted
ISafeBytesinstance per character usingGetAsSafeBytes. -
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.
- Ascii : Standard ASCII.
- Utf16LittleEndian : UTF-16 with little endian byte order
- Utf16BigEndian : UTF-16 with big endian byte order
- SafeBytes (example usages)