---
title: How to Sign and Verify a Message
description: "Learn how to sign messages on Solana."
---

The primary function of a keypair is to sign messages, transactions and enable
verification of the signature. Verification of a signature allows the recipient
to be sure that the data was signed by the owner of a specific private key.

<CodeTabs storage="cookbook" flags="r">

```typescript !! title="Kit" file=packages/docs-examples/cookbook/wallets/sign-message/kit.ts#region=sign

```

```typescript !! title="Legacy" file=packages/docs-examples/cookbook/wallets/sign-message/legacy.ts#region=sign

```

```rust !! title="Rust" file=packages/docs-examples/cookbook/wallets/sign-message/rust/src/main.rs#region=sign

```

```py !! title="Python"

from solders.keypair import Keypair
from solders.pubkey import Pubkey
import nacl.signing
import nacl.encoding

def main():
    # Create a keypair
    keypair = Keypair()
    message = b"Hello, Solana!"

    # Sign the message
    signature = keypair.sign_message(message)

    print(f"Message: {message}")
    print(f"Signature: {signature}")
    print(f"Public Key: {keypair.pubkey()}")

    # Verify the signature
    try:
        # Use nacl to verify the signature
        verify_key = nacl.signing.VerifyKey(keypair.pubkey().__bytes__())
        verify_key.verify(message, signature.__bytes__())
        print("Signature is valid: True")
    except Exception as e:
        print(f"Signature is valid: False - {e}")

if __name__ == "__main__":
    main()
```

</CodeTabs>
