Ixnas.AltchaNet 1.2.0

dotnet add package Ixnas.AltchaNet --version 1.2.0
                    
NuGet\Install-Package Ixnas.AltchaNet -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ixnas.AltchaNet" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ixnas.AltchaNet" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Ixnas.AltchaNet" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ixnas.AltchaNet --version 1.2.0
                    
#r "nuget: Ixnas.AltchaNet, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ixnas.AltchaNet@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ixnas.AltchaNet&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Ixnas.AltchaNet&version=1.2.0
                    
Install as a Cake Tool

Altcha.NET

Build status Nuget version

C# implementation of the ALTCHA challenge.

Features

  • Compatible with the latest ALTCHA client-side widget
  • Independent of ASP.NET (Core)
  • Generates and validates self-hosted challenges
  • Solves remotely hosted challenges, enabling machine-to-machine ALTCHA
  • Replay attack prevention by denying previously verified challenges
  • Expiring challenges

Contents

Installation

This library is available on NuGet, so you can add it to your project as follows:

dotnet add package Ixnas.AltchaNet

Using self-hosted challenges

Set up

First make sure you've set up the front-end widget to use your challenge endpoint.

The entrypoint of this library contains a method for building and configuring a service. This service can be used to create ALTCHA challenges and validate their responses. The most basic configuration looks like this:

var altchaService = Altcha.CreateService(new AltchaSha256Configuration
{
    Key = AltchaKey.FromBytes(key),
    StoreFactory = storeFactory,
});

Here is a description of the different configuration options.

Property Description
StoreFactory (Required) Configures a store factory to use for previously verified ALTCHA responses. Used to prevent replay attacks.
Key (Required) Configures the SHA-256 algorithm for hashing and signing. Must be 64 bytes long. Currently the only supported algorithm.
Complexity (Optional) Overrides the default complexity to tweak the amount of computational effort a client has to put in. See ALTCHA's documentation for more information (default counter range 50,000 - 100,000 with a cost of 1).
Expiry (Optional) Overrides the default time it takes for a challenge to expire (default 120 seconds).
Key

The library requires a key to sign and verify ALTCHA challenges. You can use a random number generator from .NET to create one for you:

var key = new byte[64];
using (var rng = RandomNumberGenerator.Create())
{
    rng.GetBytes(key);
}
Store

The library requires a store implementation to store previously verified challenge responses. You can use anything persistent, like a database or a file. As long as it implements the IAltchaChallengeStore interface, it will work.

You can use expiryUtc to periodically remove expired challenges from your store.

As an example, the bundled in-memory store looks similar to this:

public class InMemoryStore : IAltchaChallengeStore
{
    private class StoredChallenge
    {
        public string Challenge { get; set; }
        public DateTimeOffset ExpiryUtc { get; set; }
    }

    private readonly List<StoredChallenge> _stored = new List<StoredChallenge>();

    public Task Store(string challenge, DateTimeOffset expiryUtc)
    {
        var challengeToStore = new StoredChallenge
        {
            Challenge = challenge,
            ExpiryUtc = expiryUtc
        };
        _stored.Add(challengeToStore);
        return Task.CompletedTask;
    }

    public Task<bool> Exists(string challenge)
    {
        _stored.RemoveAll(storedChallenge => storedChallenge.ExpiryUtc <= DateTimeOffset.UtcNow);
        var exists = _stored.Exists(storedChallenge => storedChallenge.Challenge == challenge);
        return Task.FromResult(exists);
    }
}

If you're using a short-lived object to access your database (like a request-scoped Entity Framework DbContext), it is recommended to provide a factory function for the store instead of an instance.

Usage

Generating a challenge

To generate a challenge:

var challenge = altchaService.Generate();

The challenge object can be serialized to JSON for the client to use. Read ALTCHA's documentation on how to use such a JSON object.

It's possible to override configuration options by passing an anonymous function to change the configuration. This can be useful when implementing a dynamic complexity strategy, for example.

// .NET 8 or newer
var challenge = altchaService.Generate(configuration => configuration with
{
    Complexity = configuration.Complexity with 
    {
        Counter = new AltchaComplexityCounterRange(200000, 300000),
    },
    Expiry = AltchaExpiry.FromSeconds(300),
});

// .NET Framework or .NET Standard
var challenge = altchaService.Generate(configuration => 
{
    configuration.Complexity = new AltchaDeterministicComplexity()
    {
        Counter = new AltchaComplexityCounterRange(200000, 300000),
        Cost = configuration.Complexity.Cost,
    };
    configuration.Expiry = AltchaExpiry.FromSeconds(300);
    return configuration;
})

The updated configuration will be used for this single call only.

Validating a response

To validate a response:

var validationResult = await altchaService.Validate(altcha, cancellationToken);
if (!validationResult.IsValid)
{
    _logger.LogInformation(validationResult.ValidationError.Message);
    /* ... */
}

The altcha parameter can either be a base64-encoded JSON string (like the raw value of the altcha field in a submitted form), or an already decoded and deserialized AltchaResponse object.

The cancellationToken parameter can be passed if the service was set up with a IAltchaCancellableChallengeStore. The cancellation token can cancel queries and updates to the store implementation.

Solving challenges

Set up

The entrypoint of this library contains a method for creating solver instances. The most basic configuration looks like this:

var altchaSolver = Altcha.CreateSolver();

// Or

var altchaSolver = Altcha.CreateSolver(new AltchaSolverConfiguration()
{
    IgnoreExpiry = true,
});

Here is a description of the different configuration options.

Method Description
IgnoreExpiry() (Optional) Disables checking for expiry before attempting to solve a challenge.
Build() Returns a new configured solver instance.

Usage

To solve a challenge, first make sure you have a deserialized AltchaChallange object to solve. Then you can solve the challenge as follows:

var solverResult = altchaSolver.Solve(challenge);

if (!solverResult.Success)
{
    _logger.LogInformation(solverResult.Error.Message);
    /* ... */
}

var formWithAltcha = new
{
    SomeFormField = "some text",
    Altcha = solverResult.Altcha
};

This example attaches the solution from solverResult.Altcha to a form object as the "altcha" field.

Example

The AspNetCoreExample project contains a few examples for generating, solving and validating challenges.

Contributing

Bug reports, fixes, ideas and suggestions are always welcome! Feel free to reach out by creating new issues, and I'll try to respond as soon as I can.

License

See LICENSE.txt

See LICENSE-ALTCHA.txt for ALTCHA's original license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Ixnas.AltchaNet:

Package Downloads
Sitepoint.AltchaCaptcha

Internal Sitepoint library providing integration with the self-hosted ALTCHA proof-of-work CAPTCHA service. Includes the client-side ALTCHA widget (static assets) and a typed service layer for requesting challenges and verifying proofs against the private backend API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 3,631 5/31/2026
1.1.1 48,857 12/29/2025
1.1.0 67,015 3/22/2025
1.0.0 2,281 3/18/2025
0.4.1 22,751 11/4/2024
0.4.0 229 11/3/2024
0.3.0 34,536 5/21/2024
0.2.1 228 5/13/2024
0.2.0 231 5/11/2024
0.1.3 260 5/6/2024
0.1.2 265 5/5/2024
0.1.1 242 4/26/2024
0.1.0 512 4/26/2024