Repository navigation
OnixLabs.Core.Text.Base32Codec
Represents a codec for encoding and decoding Base-32 values.
| Namespace | OnixLabs.Core.Text |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → Base32Codec
|
| Implements | IBaseCodec |
public sealed class Base32Codec : IBaseCodecBase32Codec implements the standard 5-bit group Base-32 algorithm. The alphabet and padding behaviour are taken from the supplied Base32FormatProvider; its default provider is Base32FormatProvider.Rfc4648.
When the selected provider uses padding (see Base32FormatProvider.IsPadded), encoded output is padded with = characters to a multiple of eight characters, and decoding rejects input whose length is not a multiple of eight (TryDecode returns false). Decoding also fails if the input contains a character that is not part of the provider's alphabet.
An empty input is treated as a successful no-op, producing an empty string (encoding) or an empty byte[] (decoding). Supplying a format provider of an incompatible type causes the Try* operations to return false and the Encode/Decode operations to throw a FormatException.
| Method | Returns | Description |
|---|---|---|
Encode(ReadOnlySpan<byte>, IFormatProvider?) |
string |
Encodes the specified value into a Base-32 string. |
Decode(ReadOnlySpan<char>, IFormatProvider?) |
byte[] |
Decodes the specified Base-32 value into a byte array. |
TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string) |
bool |
Tries to encode the specified value into a Base-32 string. |
TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[]) |
bool |
Tries to decode the specified Base-32 value into a byte array. |
public string Encode(ReadOnlySpan<byte> value, IFormatProvider? provider = null);Encodes the specified value into a Base-32 string representation. See IBaseCodec.Encode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to encode into a Base-32 string representation. |
provider |
IFormatProvider? |
The Base32FormatProvider that supplies the alphabet and padding behaviour. When null or a CultureInfo, defaults to Base32FormatProvider.Rfc4648. |
Returns — A new Base-32 string representation encoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be encoded, or provider is not a Base32FormatProvider. |
public byte[] Decode(ReadOnlySpan<char> value, IFormatProvider? provider = null);Decodes the specified Base-32 representation into a byte array. See IBaseCodec.Decode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-32 value to decode into a byte array. |
provider |
IFormatProvider? |
The Base32FormatProvider that supplies the alphabet and padding behaviour. When null or a CultureInfo, defaults to Base32FormatProvider.Rfc4648. |
Returns — A new byte array decoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be decoded, its length is not a multiple of eight when using a padded provider, it contains characters not in the provider's alphabet, or provider is not a Base32FormatProvider. |
public bool TryEncode(ReadOnlySpan<byte> value, IFormatProvider? provider, out string result);Tries to encode the specified value into a Base-32 string representation. See IBaseCodec.TryEncode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to encode into a Base-32 string representation. |
provider |
IFormatProvider? |
The Base32FormatProvider that supplies the alphabet and padding behaviour. |
result |
out string |
When this method returns, contains the Base-32 string encoded from value, or an empty string if the value could not be encoded. |
Returns — true if the value was encoded successfully; otherwise, false.
public bool TryDecode(ReadOnlySpan<char> value, IFormatProvider? provider, out byte[] result);Tries to decode the specified Base-32 representation into a byte array. See IBaseCodec.TryDecode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-32 value to decode into a byte array. |
provider |
IFormatProvider? |
The Base32FormatProvider that supplies the alphabet and padding behaviour. |
result |
out byte[] |
When this method returns, contains the byte array decoded from value, or an empty byte array if the value could not be decoded. |
Returns — true if the value was decoded successfully; otherwise, false.
using System;
using System.Text;
using OnixLabs.Core.Text;
byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
Base32Codec codec = new();
string rfc4648 = codec.Encode(data, Base32FormatProvider.Rfc4648);
string padded = codec.Encode(data, Base32FormatProvider.PaddedRfc4648);
string zbase = codec.Encode(data, Base32FormatProvider.ZBase32);
Console.WriteLine(rfc4648); // JBSWY3DPEB3W64TMMQQQ
Console.WriteLine(padded); // JBSWY3DPEB3W64TMMQQQ====
Console.WriteLine(zbase); // jb1sa5dxrb5ycodiiqqqusing OnixLabs.Core.Text;
byte[] data = [0xDE, 0xAD, 0xBE, 0xEF];
Base32Codec codec = new();
string encoded = codec.Encode(data, Base32FormatProvider.PaddedRfc4648);
byte[] decoded = codec.Decode(encoded, Base32FormatProvider.PaddedRfc4648);
// decoded == [0xDE, 0xAD, 0xBE, 0xEF]ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions