---
title: "Sign a prehashed message in C#"
url: https://recursiveai.org/onecrypto/cookbook/sign-prehashed-message-in-two-phases/
description: "Sign an existing SHA-256 digest with RSA-PSS, then verify the returned signature with OneCrypto’s two-phase signing API."
version: "0.4.0-preview.2"
---

# Sign a prehashed message in C#

**OneCrypto** is a free, open-source cryptography library for .NET from Recursive AI. MIT or Apache-2.0, preview, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCrypto.Keys) · [GitHub](https://github.com/recursive-ai-oss/onecrypto-csharp) · [Docs](https://recursiveai.org/onecrypto/docs/) · [Download example (.zip)](https://recursiveai.org/onecrypto/cookbook/sign-prehashed-message-in-two-phases/sign-prehashed-message-in-two-phases.zip)

Use `PrepareDigest` and RSA-PSS to sign a SHA-256 hash without hashing it again.

```shell
dotnet add package RecursiveAI.OneCrypto.Keys --prerelease
```

`RecursiveAI.OneCrypto` provides `RecursiveAI.OneCrypto.Policy` and is installed automatically with `RecursiveAI.OneCrypto.Keys`.

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using System.Security.Cryptography;
using RecursiveAI.OneCrypto.Keys;
using RecursiveAI.OneCrypto.Policy;

using var rsa = RSA.Create(2048);
using var key = PrivateKey.FromRsa(rsa);
var spec = SignatureSpec.RsaPss(
    HashAlgorithmName.SHA256);
byte[] digest = SHA256.HashData(
    "Approve draft"u8);
var policy = CryptoPolicy.Standard;
var now = DateTimeOffset.UtcNow;
var prepared = PreparedSignature
    .PrepareDigest(key.PublicKey, spec,
        digest, policy, now);

var request = PreparedSignature.Decode(
    prepared.Encode());
byte[] raw = key.SignDigest(
    request.ToSign.Span, request.Spec);
byte[] signature = prepared.Complete(raw);
Console.WriteLine(
    $"Verified: {signature.Length} bytes");
```

The example hashes `Approve draft`, encodes the signing request, and decodes it before signing. It simulates the handoff in one process with a fresh test key; nothing is sent over the network. `Complete` checks the raw signature against the public key and digest in the original request. If verification fails, it throws `InvalidSignatureException`.

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onecrypto/cookbook/sign-prehashed-message-in-two-phases/sign-prehashed-message-in-two-phases.zip) and `dotnet run`.

The local 2048-bit RSA example prints:

```text
Verified: 256 bytes
```

## Variations

**Keep signing and completion in the same process.** Replace the code after `PrepareDigest` with:

```csharp
byte[] signature = prepared.SignWith(key);
Console.WriteLine(
    $"Verified: {signature.Length} bytes");
```

## Related recipes

- [Prepare and complete a detached signature](https://recursiveai.org/onecrypto/cookbook/prepare-and-complete-detached-signature/)
- [Sign and verify with Ed25519](https://recursiveai.org/onecrypto/cookbook/ed25519-sign-verify/)

## The short answer

Use `PrepareDigest` with the public key, the already computed digest, and a `SignatureSpec` that names its hash algorithm. Give the signer the prepared `ToSign` bytes and spec, call `SignDigest`, and verify the returned bytes with `Complete`. If you already have the digest from an upstream step, use it in place of the example’s `SHA256.HashData` call. Do not pass the digest to `Sign`, which takes message bytes rather than an already computed digest.

## Why is a hash function used in digital signatures?

For the RSA-PSS flow above, SHA-256 reduces the input to a 32-byte digest. The process that reads the document computes that value; the process holding the key can sign without needing the original file. The digest must match the algorithm in the spec: the length alone does not tell the signer which hash function produced it. For example, do not label an arbitrary 32-byte value as a SHA-256 digest. The signature scheme and its hash must agree across preparation, signing, and verification.

## How do you sign and verify an RSA digest in C#?

Keep the original prepared state with the process that requested the signature. The key holder can use the encoded request to recover the spec and digest, then return only the raw signature. The requesting process calls `Complete` on the state it kept, not on replacement state supplied with the reply. Verification checks that the signature belongs to the expected public key and digest; it does not establish who owns that key or who approved the digest. Get the public key from a source your application already trusts, and make the decision to authorize a signing request separately.

`PrepareDigest` is for the RSA and ECDSA forms that sign digests. The [signature specs](https://recursiveai.org/onecrypto/docs/keys/#signature-specs) list pure Ed25519, Ed448, and ML-DSA as message-based forms. For those forms use `Prepare` with the message; replacing the message with its SHA-256 hash would sign a different input.

For message-based signing or `Prepare` rather than `PrepareDigest`, [the key guide](https://recursiveai.org/onecrypto/docs/keys/#encoding-version-1) warns: “The state holds the data for message schemes and for `Prepare`: treat it like the document.”
