Class Base32
Class for encoding and decoding bytes into Base32.
public class Base32 : ITextEncoding
- Inheritance
-
objectBase32
- Implements
Remarks
See RFC4648 for details (https://datatracker.ietf.org/doc/html/rfc4648) on base-32.
Methods
Decode(ReadOnlySpan<char>, Span<byte>)
Decode the string into data
.
public 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.
public byte[] Decode(string encoded)
Parameters
encoded
stringA string encoded with data to be decoded.
Returns
- byte[]
A byte collection resulting from decoding
encoded
.
DecodeText(ReadOnlySpan<char>, Span<byte>)
Decode the string into data
.
public static void DecodeText(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.
DecodeText(string)
Decode the string into a byte array.
public static byte[] DecodeText(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.
public 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
.
public 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.
EncodeBytes(ReadOnlySpan<byte>)
Encode the byte collection into a string representation.
public static string EncodeBytes(ReadOnlySpan<byte> data)
Parameters
data
ReadOnlySpan<byte>The byte collection to encode.
Returns
- string
A string representation of
data
.
EncodeBytes(ReadOnlySpan<byte>, Span<char>)
Encode the byte collection into encoded
.
public static void EncodeBytes(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.
GetDecodedSize(ReadOnlySpan<char>)
Get the number of bytes in data represented by base-32 encoded text.
public static int GetDecodedSize(ReadOnlySpan<char> encoded)
Parameters
encoded
ReadOnlySpan<char>The text to be decoded.
Returns
- int
An int representing the number of bytes.
Remarks
The other encoding classes don't have the Get*Size methods. However, base-32 encoding represents five bits per character, and padding has to be accounted for, so it's a complex enough of an operation to merit a helper.
GetEncodedSize(int)
Gets the number of characters needed to encode the data.
public static int GetEncodedSize(int lengthInBytes)
Parameters
lengthInBytes
intThe length of the data to encode.
Returns
- int
The number of characters needed.
Remarks
The other encoding classes don't have the Get*Size methods. However, base-32 encoding represents five bits per character, and padding has to be accounted for, so it's a complex enough of an operation to merit a helper.