Repository navigation
OnixLabs.Core.Text.Base64Codec
Represents a codec for encoding and decoding Base-64 values.
| Namespace | OnixLabs.Core.Text |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → Base64Codec
|
| Implements | IBaseCodec |
public sealed class Base64Codec : IBaseCodecBase64Codec encodes using Convert.ToBase64String and decodes using Convert.FromBase64String. Its default and only supported provider is Base64FormatProvider.Rfc4648; the provider is used to validate that the codec was invoked with a compatible format provider rather than to vary the 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-64 string. |
Decode(ReadOnlySpan<char>, IFormatProvider?) |
byte[] |
Decodes the specified Base-64 value into a byte array. |
TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string) |
bool |
Tries to encode the specified value into a Base-64 string. |
TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[]) |
bool |
Tries to decode the specified Base-64 value into a byte array. |
public string Encode(ReadOnlySpan<byte> value, IFormatProvider? provider = null);Encodes the specified value into a Base-64 string representation. See IBaseCodec.Encode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to encode into a Base-64 string representation. |
provider |
IFormatProvider? |
The Base64FormatProvider to use. When null or a CultureInfo, defaults to Base64FormatProvider.Rfc4648. |
Returns — A new Base-64 string representation encoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be encoded, or provider is not a Base64FormatProvider. |
public byte[] Decode(ReadOnlySpan<char> value, IFormatProvider? provider = null);Decodes the specified Base-64 representation into a byte array. See IBaseCodec.Decode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-64 value to decode into a byte array. |
provider |
IFormatProvider? |
The Base64FormatProvider to use. When null or a CultureInfo, defaults to Base64FormatProvider.Rfc4648. |
Returns — A new byte array decoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not a valid Base-64 string, or provider is not a Base64FormatProvider. |
public bool TryEncode(ReadOnlySpan<byte> value, IFormatProvider? provider, out string result);Tries to encode the specified value into a Base-64 string representation. See IBaseCodec.TryEncode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to encode into a Base-64 string representation. |
provider |
IFormatProvider? |
The Base64FormatProvider to use. |
result |
out string |
When this method returns, contains the Base-64 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-64 representation into a byte array. See IBaseCodec.TryDecode for the full contract.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-64 value to decode into a byte array. |
provider |
IFormatProvider? |
The Base64FormatProvider to use. |
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!");
Base64Codec codec = new();
string encoded = codec.Encode(data);
byte[] decoded = codec.Decode(encoded);
Console.WriteLine(encoded); // SGVsbG8sIFdvcmxkIQ==
Console.WriteLine(Encoding.UTF8.GetString(decoded)); // Hello, World!using System;
using OnixLabs.Core.Text;
byte[] data = [0xCA, 0xFE, 0xBA, 0xBE];
string encoded = IBaseCodec.GetString(data, Base64FormatProvider.Rfc4648);
if (IBaseCodec.TryGetBytes(encoded, Base64FormatProvider.Rfc4648, out byte[] decoded))
{
Console.WriteLine(decoded.Length); // 4
}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