---
title: "AlphaCode"
description: "Namespace: Lacuna.Pki"
canonical: "https://docs.lacunasoftware.com/api/Lacuna.Pki/AlphaCode"
locale: "pt-BR"
product: "PKI SDK"
last_updated: "2026-06-02"
---

# AlphaCode

**Namespace:** `Lacuna.Pki`  
**Assembly:** `Lacuna.Pki`  
_Class_

This class generates alphanumeric codes that are easy for humans to read. The codes are intended to be used as "document recovery codes"
on printer-friendly versions of signed files, with codes that are (1) easy to read (2) easy to type back with low risk of mistaking similar
characters such as "O" and "0" and (3) have a relatively high entropy for the size of the code (high number of possible codes relative to the
size of the code, allowing the developer to choose a relatively small code size).

```csharp
public static class AlphaCode
```

**Hierarquia:** [`object`](https://learn.microsoft.com/dotnet/api/system.object) → `AlphaCode`

## Comentários

To improve readability, the codes contain only uppercase letters and do not include characters like "O", "0", "1" and "I", while
still maintaining a relatively high entropy per character. There are 32 possible characters, so every character adds 5 bits to the overall
entropy (25% better than using hexadecimal) which results in smaller codes for the same intended entropy. For instance, to generate a code
with 80 bits of entropy (2^80 possible codes), an hexadecimal code have to be 20 characters, while a code generated by this class needs
only 16 characters.

## Propriedades

### `DefaultSize`

The default size for codes (used by the method Generate()). Each character contributes 5 bits to the overall entropy
of the generated codes. This value is initially set to 16, which means 80 bits of entropy (2^80 possible codes).

```csharp
public static int DefaultSize { get; set; }
```

**Retorno**

[`int`](https://learn.microsoft.com/dotnet/api/system.int32)

---

## Métodos

### `Format(string, int)`

Formats an alphanumeric code in groups, for instance a 16-character code into 4 groups is formatted as XXXX-XXXX-XXXX-XXXX. The number of
groups should be an exact divisor of the code size, otherwise the code is returned unformatted. For instance, for a code of size 20, you
might pass nGroups = 4 for XXXXX-XXXXX-XXXXX-XXXX or nGroups = 5 for XXXX-XXXX-XXXX-XXXX-XXXX.

```csharp
public static string Format(string code, int nGroups)
```

**Parâmetros**

| Nome | Tipo | Descrição |
|---|---|---|
| `code` | [`string`](https://learn.microsoft.com/dotnet/api/system.string) | The unformatted alphanumeric code |
| `nGroups` | [`int`](https://learn.microsoft.com/dotnet/api/system.int32) | The number of groups |

**Retorno**

[`string`](https://learn.microsoft.com/dotnet/api/system.string) — The alphanumeric code formatted in blocks, or the unformatted code if the code size is not divisible by the number of groups

---

### `Format(string)`

Formats an alphanumeric code in groups of characters separated by hyphens, for instance a 16-character code is formatted as XXXX-XXXX-XXXX-XXXX.
A guess of the number of groups is made based on the code size. If you want to specify the number of groups, use the overload Format(string, int) instead.

```csharp
public static string Format(string code)
```

**Parâmetros**

| Nome | Tipo | Descrição |
|---|---|---|
| `code` | [`string`](https://learn.microsoft.com/dotnet/api/system.string) | The unformatted alphanumeric code |

**Retorno**

[`string`](https://learn.microsoft.com/dotnet/api/system.string) — The alphanumeric code formatted in groups, or the unformatted code if no good guess of the number of groups can be made based on the code size

---

### `Generate()`

Generates an alphanumeric code with the default size (see property DefaultSize). The returned code is unformatted, and
is meant to be stored on your database indexing the document or related entity. To show the code to users, you should
format the code with one of the Format() methods.

```csharp
public static string Generate()
```

**Retorno**

[`string`](https://learn.microsoft.com/dotnet/api/system.string) — The alphanumeric code with the default size (unformatted)

---

### `Generate(int)`

Generates an alphanumeric code with the given size. Each character contributes 5 bits to the overall entropy of the generated
codes, so for instance a code with size 16 has 80 bits of entropy (2^80 possible codes). The returned code is unformatted, and
is meant to be stored on your database indexing the document or related entity. To show the code to users, you should
format the code with one of the Format() methods.

```csharp
public static string Generate(int codeSize)
```

**Parâmetros**

| Nome | Tipo | Descrição |
|---|---|---|
| `codeSize` | [`int`](https://learn.microsoft.com/dotnet/api/system.int32) | Size of the code to be generated |

**Retorno**

[`string`](https://learn.microsoft.com/dotnet/api/system.string) — The alphanumeric code with the given size (unformatted)

---

### `Parse(string)`

Parses a given alphanumeric code, removing hyphens or other punctuations. This method is intended to be called to parse a code typed
by the user, before using the code to perform a lookup on the database.

```csharp
public static string Parse(string formattedCode)
```

**Parâmetros**

| Nome | Tipo | Descrição |
|---|---|---|
| `formattedCode` | [`string`](https://learn.microsoft.com/dotnet/api/system.string) | The formatted alphanumeric code, e.g. XXXX-XXXX-XXXX-XXXX |

**Retorno**

[`string`](https://learn.microsoft.com/dotnet/api/system.string) — The unformatted alphanumeric code, e.g.: XXXXXXXXXXXXXXXX

---

## Membros herdados

[`ToString()`](https://learn.microsoft.com/dotnet/api/system.object.tostring), [`Equals(object)`](<https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)>), [`Equals(object, object)`](<https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)>), [`ReferenceEquals(object, object)`](https://learn.microsoft.com/dotnet/api/system.object.referenceequals), [`GetHashCode()`](https://learn.microsoft.com/dotnet/api/system.object.gethashcode), [`GetType()`](https://learn.microsoft.com/dotnet/api/system.object.gettype), [`MemberwiseClone()`](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone)
