Geralt
  • Introduction
  • Random data
  • Constant time
  • Secure memory
  • Encoding
  • Padding
  • Hashing
  • Message authentication
  • Password hashing
  • Key derivation
  • Authenticated encryption
    • Stream and file encryption
    • AEGIS-128L
    • AEGIS-256
    • ChaCha20-Poly1305
    • XChaCha20-Poly1305
  • Key exchange
  • Digital signatures
  • Advanced
    • Validation
    • Concat
    • ChaCha20
    • XChaCha20
    • HChaCha20
    • Poly1305
    • Ed25519 to X25519
Powered by GitBook
On this page
  • Purpose
  • Usage
  • ToHex
  • FromHex
  • ToBase64
  • FromBase64
  • Constants
  • Notes

Encoding

Last updated 1 year ago

Purpose

It can be useful to convert bytes to strings. For example, to represent or to create shareable and public keys. Hex and Base64 encoding can be used to do this.

Usage

ToHex

Returns the hexadecimal string that represents the data.

Encodings.ToHex(ReadOnlySpan<byte> data)

Exceptions

data has a length of 0.

The data could not be converted to hex. This should never be thrown.

FromHex

Returns a byte array from a hexadecimal string. Common separator characters are ignored by default. ignoreChars can be null to disallow any non-hexadecimal characters.

Encodings.FromHex(string hex, string ignoreChars = ":- ")

Exceptions

hex is null.

hex has a length of 0.

Unable to parse the hex string.

ToBase64

Returns the Base64 string representing the data. Choose one variant (e.g. Base64URL for file names/URLs) and only ever use that variant.

Encodings.ToBase64(ReadOnlySpan<byte> data, Base64Variant variant = Base64Variant.Original)

Exceptions

data has a length of 0.

The data could not be converted to Base64. This should never be thrown.

FromBase64

Returns a byte array from a Base64 string. The variant must match the one used for encoding. ignoreChars can be null to disallow any non-Base64 characters.

Encodings.FromBase64(string base64, Base64Variant variant = Base64Variant.Original, string ignoreChars = " ")

Exceptions

base64 is null.

base64 has a length of 0.

Unable to parse the Base64 string.

Constants

public enum Base64Variant
{
    Original = 1,
    OriginalNoPadding = 3,
    Url = 5,
    UrlNoPadding = 7
}

Notes

Do NOT support multiple variants of Base64 in your application (e.g. Base64 with and without padding). This can lead to .

Unlike in the .NET library, these functions run in constant time to avoid potential . Furthermore, the Base64 implementation should be more resistant to .

Base64 has a better compression rate than hex. However, they tend to be used for different purposes. For instance, hex is often used for and , whereas Base64 is more commonly used for .

hashes
X25519
Ed25519
ArgumentOutOfRangeException
FormatException
ArgumentNullException
ArgumentOutOfRangeException
FormatException
ArgumentOutOfRangeException
FormatException
ArgumentNullException
ArgumentOutOfRangeException
FormatException
vulnerabilities
side-channel attacks
malleability attacks
test vectors
encoding hashes
encoding keys