Documentation
¶
Overview ¶
Package ed25519 provides Ed25519 digital signature cryptography.
This package implements Ed25519 public-key signature system based on the elliptic curve Ed25519. It provides key generation, signing, and verification operations with simplified interfaces for key pair management.
Features ¶
- Ed25519 key pair generation
- Digital signature creation and verification
- PEM/PKCS8/PKIX format support
- SSH public key format conversion
- Type-safe key management
Basic Example ¶
keyPair := &ed25519.KeyPair{}
err := keyPair.Generate()
signature := keyPair.Sign("message")
valid := keyPair.Verify("message", signature)
Package ed25519 provides Ed25519 digital signature cryptography.
This package implements Ed25519 public-key signature system based on the elliptic curve Ed25519. It provides key generation, signing, and verification operations with simplified interfaces for key pair management.
Features ¶
- Ed25519 key pair generation
- Digital signature creation and verification
- PEM/PKCS8/PKIX format support
- SSH public key format conversion
- Type-safe key management
Basic Example ¶
privateKey := &ed25519.PrivateKey{}
err := privateKey.SetDefault()
signature := privateKey.Sign("message")
Package ed25519 provides Ed25519 digital signature cryptography.
This package implements Ed25519 public-key signature system based on the elliptic curve Ed25519. It provides key generation, signing, and verification operations with simplified interfaces for key pair management.
Features ¶
- Ed25519 key pair generation
- Digital signature creation and verification
- PEM/PKCS8/PKIX format support
- SSH public key format conversion
- Type-safe key management
Basic Example ¶
publicKey := &ed25519.PublicKey{}
publicKey.SetPemPKIX(pemString)
valid := publicKey.Verify("message", signature)
Index ¶
- type KeyPair
- type PrivateKey
- func (pk *PrivateKey) Get() ed25519.PrivateKey
- func (pk *PrivateKey) GetPemPKCS8() (string, error)
- func (pk *PrivateKey) GetPublicKey() PublicKey
- func (pk *PrivateKey) Set(privateKey ed25519.PrivateKey)
- func (pk *PrivateKey) SetDefault() error
- func (pk *PrivateKey) SetPemPKCS8(pemPKCS8 string) error
- func (pk *PrivateKey) Sign(message string) []byte
- func (pk *PrivateKey) Verify(message string, signature []byte) bool
- type PublicKey
- func (pk *PublicKey) Get() ed25519.PublicKey
- func (pk *PublicKey) GetPemPKIX() (string, error)
- func (pk *PublicKey) GetSsh() (string, error)
- func (pk *PublicKey) GetSshPublicKey() (ssh.PublicKey, error)
- func (pk *PublicKey) Set(publicKey ed25519.PublicKey)
- func (pk *PublicKey) SetPemPKIX(pemPKIX string) error
- func (pk *PublicKey) SetSsh(sshKey string) error
- func (pk *PublicKey) SetSshPublicKey(publicKey ssh.PublicKey) error
- func (pk *PublicKey) Verify(message string, signature []byte) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KeyPair ¶
type KeyPair struct {
// contains filtered or unexported fields
}
KeyPair is struct that provides key pair related methods.
func (*KeyPair) Generate ¶
Generate creates a new Ed25519 key pair.
This method generates a random Ed25519 private key and derives the corresponding public key using the system's cryptographic random generator.
Returns ¶
- error: Error if key generation fails, nil on success
Behavior ¶
The generated key pair:
- Private key: 64 bytes (32-byte seed + 32-byte public key)
- Public key: 32 bytes
- Uses crypto/rand for secure randomness
- Automatically derives public key from private key
Examples ¶
Basic usage:
keyPair := &ed25519.KeyPair{}
err := keyPair.Generate()
if err != nil {
log.Fatal(err)
}
func (*KeyPair) GetKeyPair ¶
func (kp *KeyPair) GetKeyPair() (privateKey PrivateKey, publicKey PublicKey)
GetKeyPair retrieves the private and public keys.
This method returns both keys from the key pair for separate operations or storage.
Returns ¶
- privateKey: PrivateKey for signing operations
- publicKey: PublicKey for verification operations
Examples ¶
Extract keys:
privateKey, publicKey := keyPair.GetKeyPair() pemPrivate, _ := privateKey.GetPemPKCS8() pemPublic, _ := publicKey.GetPemPKIX()
func (*KeyPair) SetKeyPair ¶
func (kp *KeyPair) SetKeyPair(privateKey PrivateKey, publicKey PublicKey)
SetKeyPair sets the private and public keys.
This method initializes the key pair with existing keys, useful for loading keys from storage or external sources.
Parameters ¶
- privateKey: PrivateKey to set
- publicKey: PublicKey to set (must correspond to private key)
Examples ¶
Load existing keys:
privateKey := ed25519.PrivateKey{}
privateKey.SetPemPKCS8(pemString)
publicKey := privateKey.GetPublicKey()
keyPair.SetKeyPair(privateKey, publicKey)
func (*KeyPair) Sign ¶
Sign creates a digital signature for the message.
This method signs the message using the private key, producing a 64-byte signature that can be verified with the corresponding public key.
Parameters ¶
- message: Text message to sign
Returns ¶
- []byte: 64-byte Ed25519 signature
Behavior ¶
The signature:
- Always 64 bytes regardless of message length
- Deterministic for same message and key
- Cryptographically secure (128-bit security level)
- Cannot be forged without the private key
Examples ¶
Sign a message:
signature := keyPair.Sign("Hello, World!")
fmt.Printf("Signature: %x\n", signature)
func (*KeyPair) Verify ¶
Verify verifies a digital signature.
This method verifies that the signature was created by the private key corresponding to this key pair's public key.
Parameters ¶
- message: Original text message
- signature: 64-byte signature to verify
Returns ¶
- bool: true if signature is valid, false otherwise
Behavior ¶
Verification checks:
- Signature matches message and public key
- Signature has not been tampered with
- Returns false for invalid signatures (no panic)
Examples ¶
Verify a signature:
signature := keyPair.Sign("message")
valid := keyPair.Verify("message", signature)
if valid {
fmt.Println("Signature is valid")
}
type PrivateKey ¶
type PrivateKey struct {
// contains filtered or unexported fields
}
PrivateKey is struct that provides private key related methods.
func (*PrivateKey) Get ¶
func (pk *PrivateKey) Get() ed25519.PrivateKey
Get retrieves the underlying ed25519.PrivateKey.
Returns ¶
- ed25519.PrivateKey: Raw private key (64 bytes)
Examples ¶
key := privateKey.Get()
func (*PrivateKey) GetPemPKCS8 ¶
func (pk *PrivateKey) GetPemPKCS8() (string, error)
GetPemPKCS8 returns the private key in PEM-encoded PKCS#8 format.
Returns ¶
- string: PEM-encoded private key
- error: Error if encoding fails, nil on success
Examples ¶
pemString, err := privateKey.GetPemPKCS8()
func (*PrivateKey) GetPublicKey ¶
func (pk *PrivateKey) GetPublicKey() PublicKey
GetPublicKey derives the public key from the private key.
Returns ¶
- PublicKey: Corresponding public key
Examples ¶
publicKey := privateKey.GetPublicKey()
func (*PrivateKey) Set ¶
func (pk *PrivateKey) Set(privateKey ed25519.PrivateKey)
Set sets the private key from an ed25519.PrivateKey.
Parameters ¶
- privateKey: Ed25519 private key to set
Examples ¶
privateKey.Set(key)
func (*PrivateKey) SetDefault ¶
func (pk *PrivateKey) SetDefault() error
SetDefault generates a new random private key.
Returns ¶
- error: Error if key generation fails, nil on success
Examples ¶
err := privateKey.SetDefault()
func (*PrivateKey) SetPemPKCS8 ¶
func (pk *PrivateKey) SetPemPKCS8(pemPKCS8 string) error
SetPemPKCS8 sets the private key from a PEM-encoded PKCS#8 string.
Parameters ¶
- pemPKCS8: PEM-encoded private key string
Returns ¶
- error: Error if decoding or parsing fails, nil on success
Examples ¶
err := privateKey.SetPemPKCS8(pemString)
func (*PrivateKey) Sign ¶
func (pk *PrivateKey) Sign(message string) []byte
Sign creates a digital signature for the message.
Parameters ¶
- message: Text message to sign
Returns ¶
- []byte: 64-byte Ed25519 signature
Examples ¶
signature := privateKey.Sign("Hello, World!")
func (*PrivateKey) Verify ¶
func (pk *PrivateKey) Verify(message string, signature []byte) bool
Verify verifies a digital signature using the public key.
Parameters ¶
- message: Original text message
- signature: 64-byte signature to verify
Returns ¶
- bool: true if signature is valid, false otherwise
Examples ¶
valid := privateKey.Verify("message", signature)
type PublicKey ¶
type PublicKey struct {
// contains filtered or unexported fields
}
PublicKey is struct that provides public key related methods.
func (*PublicKey) Get ¶
Get retrieves the underlying ed25519.PublicKey.
Returns ¶
- ed25519.PublicKey: Raw public key (32 bytes)
Examples ¶
key := publicKey.Get()
func (*PublicKey) GetPemPKIX ¶
GetPemPKIX returns the public key in PEM-encoded PKIX format.
Returns ¶
- string: PEM-encoded public key
- error: Error if encoding fails, nil on success
Examples ¶
pemString, err := publicKey.GetPemPKIX()
func (*PublicKey) GetSsh ¶
GetSsh returns the public key in SSH authorized_keys format.
Returns ¶
- string: SSH public key string
- error: Error if encoding fails, nil on success
Examples ¶
sshKey, err := publicKey.GetSsh()
func (*PublicKey) GetSshPublicKey ¶
GetSshPublicKey returns the key as an ssh.PublicKey.
Returns ¶
- ssh.PublicKey: SSH public key interface
- error: Error if conversion fails, nil on success
Examples ¶
sshKey, err := publicKey.GetSshPublicKey()
func (*PublicKey) Set ¶
Set sets the public key from an ed25519.PublicKey.
Parameters ¶
- publicKey: Ed25519 public key to set
Examples ¶
publicKey.Set(key)
func (*PublicKey) SetPemPKIX ¶
SetPemPKIX sets the public key from a PEM-encoded PKIX string.
Parameters ¶
- pemPKIX: PEM-encoded public key string
Returns ¶
- error: Error if decoding or parsing fails, nil on success
Examples ¶
err := publicKey.SetPemPKIX(pemString)
func (*PublicKey) SetSsh ¶
SetSsh sets the public key from an SSH authorized_keys format string.
Parameters ¶
- sshKey: SSH public key string
Returns ¶
- error: Error if parsing fails, nil on success
Examples ¶
err := publicKey.SetSsh(sshKey)
func (*PublicKey) SetSshPublicKey ¶
SetSshPublicKey sets the public key from an ssh.PublicKey.
Parameters ¶
- publicKey: SSH public key to set
Returns ¶
- error: Error if conversion fails, nil on success
Examples ¶
err := publicKey.SetSshPublicKey(sshKey)