Interface ITextEncoding
Interface for abstracting different means of encoding and decoding byte collections.
public interface ITextEncoding
Methods
Decode(ReadOnlySpan<char>, Span<byte>)
Decode the string into data
.
void Decode(ReadOnlySpan<char> encoded, Span<byte> data)
Parameters
encoded
ReadOnlySpan<char>Encoded text.
data
Span<byte>A System.Span<T> to decode the data to.
Remarks
When decoding sensitive data that should not be in an immutable object, this method gives you the ability to decode directly to a char System.Span<T>.
The point of this method is to allow you to decode data to a char collection that you own. Therefore, you must allocate this collection before calling this method.
For Base16 and ModHex, it is one character per four bits of information, so two characters per byte.
For Base32, it is more complicated. Each character hold five bits of data. Plus, you must account for padding at the end of the encoded data. There is a method to calculate the space needed called GetDecodedSize(ReadOnlySpan<char>) that will tell you how many bytes you need.
Decode(string)
Decode the string into a byte array.
byte[] Decode(string encoded)
Parameters
encoded
stringA string encoded with data to be decoded.
Returns
- byte[]
A byte collection resulting from decoding
encoded
.
Encode(ReadOnlySpan<byte>)
Encode the byte collection into a string representation.
string Encode(ReadOnlySpan<byte> data)
Parameters
data
ReadOnlySpan<byte>The byte collection to encode.
Returns
- string
A string representation of
data
.
Encode(ReadOnlySpan<byte>, Span<char>)
Encode the byte collection into encoded
.
void Encode(ReadOnlySpan<byte> data, Span<char> encoded)
Parameters
data
ReadOnlySpan<byte>The data to be encoded.
encoded
Span<char>A System.Span<T> to encode the data to.
Remarks
When encoding sensitive data that should not be in an immutable object, this method gives you the ability to encode directly to memory.
The point of this method is to allow you to encode data to memory that you own. Therefore, you must allocate this memory before calling this method. The amount of memory required varies depending on the encoding.
For Base16 and ModHex, it is one character per four bits of information, so two characters per byte.
For Base32, it is more complicated. Each character hold five bits of data. Plus, you must account for padding at the end of the encoded data. There is a method to calculate the space needed called GetEncodedSize(int) that will tell you how many bytes you need.