Class ShortCode
Generates, normalizes and formats short codes that users read on one screen and type on another, such as device link codes or setup codes: 8 characters
from Crockford's base 32 (about 10^12 codes), displayed as K7PX-M3QR.
Namespace: Singulink.Net.Http.Api.Service.SignIn
Assembly: Singulink.Net.Http.Api.Service.dll
Syntax
public static class ShortCode
Remarks
The alphabet has the digits and the letters without I, L, O and U, so codes avoid characters that are easily confused. Typing is forgiving: case does not matter, O is read as 0, I and L are read as 1, and spaces and dashes are ignored.
Short codes can be guessed far more easily than random keys: whatever a code unlocks is exposed for as long as the code is valid, to as many wrong attempts as are allowed. There are few enough codes that a plain hash of one can be reversed by hashing every code, while a keyed hash (see ComputeHash(string, ReadOnlySpan<byte>)) cannot be without its key.
Fields
| Name | Description |
|---|---|
| Length | The number of characters in a normalized code. |
| MinHashKeyLength | The minimum length of the key of ComputeHash(string, ReadOnlySpan<byte>), in bytes. |
Methods
| Name | Description |
|---|---|
| ComputeHash(string, ReadOnlySpan<byte>) | Computes the keyed hash (HMAC-SHA256) of a code, as 43 base64url characters. The code is normalized first, so typed input gives the same hash as the generated code. |
| Format(string) | Formats a code for display, for example |
| Generate() | Generates a new random code in normalized form (Length characters, without the dash). |
| Normalize(string) | Normalizes a code typed by a user, or returns null if the input cannot be a code. The input is case-insensitive, O is read as 0, I and L are read as 1, and spaces and dashes are ignored. |