A comprehensive Go cryptographic library providing secure key generation, signing, verification, and Ethereum-compatible cryptographic operations with libp2p integration.
This library provides a complete cryptographic toolkit for Go applications, offering support for multiple key types, secure key generation, signing and verification operations, and Ethereum-compatible cryptographic functions. It's designed to work seamlessly with libp2p and provides a clean, type-safe API for cryptographic operations.
- Multiple Key Types: Support for Ed25519, Secp256k1, and Ethereum-compatible keys
- Secure Key Generation: Cryptographically secure random key pair generation
- Signing & Verification: Complete signing and verification operations
- Ethereum Compatibility: Ethereum message signing and verification support
- libp2p Integration: Native support for libp2p cryptographic operations
- Key Management: Secure key storage and retrieval with keystore functionality
- ID System: Unique identifier generation and management based on public keys
- Comprehensive Testing: Extensive test coverage for all cryptographic operations
go get gitlab.com/nunet/depinkit/cryptopackage main
import (
"fmt"
"log"
"gitlab.com/nunet/depinkit/crypto"
)
func main() {
// Generate a new Ed25519 key pair
privKey, pubKey, err := crypto.GenerateKeyPair(crypto.Ed25519)
if err != nil {
log.Fatal(err)
}
// Create a unique ID from the public key
id, err := crypto.IDFromPublicKey(pubKey)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Generated ID: %s\n", id.String())
// Sign some data
data := []byte("Hello, Crypto!")
signature, err := privKey.Sign(data)
if err != nil {
log.Fatal(err)
}
// Verify the signature
ok, err := pubKey.Verify(data, signature)
if err != nil {
log.Fatal(err)
}
if ok {
fmt.Println("Signature verified successfully!")
} else {
fmt.Println("Signature verification failed!")
}
}The library supports multiple key types:
const (
Ed25519 = crypto.Ed25519 // Fast, secure elliptic curve
Secp256k1 = crypto.Secp256k1 // Bitcoin/Ethereum compatible
Eth = 127 // Ethereum-specific key type
)Generate key pairs for different algorithms:
// Ed25519 keys (fast, secure)
privKey, pubKey, err := crypto.GenerateKeyPair(crypto.Ed25519)
// Secp256k1 keys (Bitcoin/Ethereum compatible)
privKey, pubKey, err := crypto.GenerateKeyPair(crypto.Secp256k1)The library provides a unique ID system based on public keys:
type ID struct{ PublicKey []byte }
// Create ID from public key
id, err := crypto.IDFromPublicKey(pubKey)
// Convert ID back to public key
pubKey, err := crypto.PublicKeyFromID(id)
// String representation (base32 encoded)
idString := id.String() // e.g., "ABC123..."
// Parse ID from string
id, err := crypto.IDFromString("ABC123...")// Generate key pair
privKey, pubKey, err := crypto.GenerateKeyPair(crypto.Ed25519)
if err != nil {
log.Fatal(err)
}
// Convert keys to/from bytes
privBytes, err := crypto.PrivateKeyToBytes(privKey)
pubBytes, err := crypto.PublicKeyToBytes(pubKey)
// Reconstruct keys from bytes
privKey, err = crypto.BytesToPrivateKey(privBytes)
pubKey, err = crypto.BytesToPublicKey(pubBytes)// Sign data
data := []byte("Important message")
signature, err := privKey.Sign(data)
if err != nil {
log.Fatal(err)
}
// Verify signature
ok, err := pubKey.Verify(data, signature)
if err != nil {
log.Fatal(err)
}
if ok {
fmt.Println("Signature is valid!")
}// Create Ethereum public key from raw bytes
ethPubKey, err := crypto.UnmarshalEthPublicKey(ethKeyBytes)
if err != nil {
log.Fatal(err)
}
// Verify Ethereum signature
ok, err := ethPubKey.Verify(message, signature)
if err != nil {
log.Fatal(err)
}
// Get raw key bytes
rawBytes, err := ethPubKey.Raw()// Generate random entropy
entropy, err := crypto.RandomEntropy(32) // 32 bytes of random data
if err != nil {
log.Fatal(err)
}
// Calculate SHA3 hash
data := []byte("data to hash")
hash, err := crypto.Sha3(data)
if err != nil {
log.Fatal(err)
}
// Hash multiple byte arrays
hash, err := crypto.Sha3(data1, data2, data3)// Check if key type is supported
if crypto.AllowedKey(crypto.Ed25519) {
fmt.Println("Ed25519 is supported")
}
// Validate key types
switch keyType {
case crypto.Ed25519:
// Handle Ed25519
case crypto.Secp256k1:
// Handle Secp256k1
default:
return fmt.Errorf("unsupported key type: %d", keyType)
}// Create a new keystore
keystore := crypto.NewKeystore()
// Store a key with a name
err := keystore.StoreKey("my-key", privKey, passphrase)
if err != nil {
log.Fatal(err)
}
// Retrieve a key
retrievedKey, err := keystore.GetKey("my-key", passphrase)
if err != nil {
log.Fatal(err)
}
// List all keys
keys := keystore.ListKeys()
for _, keyName := range keys {
fmt.Printf("Stored key: %s\n", keyName)
}Key: Interface for all cryptographic keysPrivKey: Interface for private keysPubKey: Interface for public keysID: Unique identifier based on public key
GenerateKeyPair(t int) (PrivKey, PubKey, error): Generate new key pairAllowedKey(t int) bool: Check if key type is supportedPublicKeyToBytes(k PubKey) ([]byte, error): Convert public key to bytesBytesToPublicKey(data []byte) (PubKey, error): Convert bytes to public keyPrivateKeyToBytes(k PrivKey) ([]byte, error): Convert private key to bytesBytesToPrivateKey(data []byte) (PrivKey, error): Convert bytes to private key
IDFromPublicKey(k PubKey) (ID, error): Create ID from public keyPublicKeyFromID(id ID) (PubKey, error): Get public key from IDIDFromString(s string) (ID, error): Parse ID from string
RandomEntropy(length int) ([]byte, error): Generate random entropySha3(data ...[]byte) ([]byte, error): Calculate SHA3 hash
UnmarshalEthPublicKey(data []byte) (PubKey, error): Parse Ethereum public keyEthPublicKey: Ethereum-compatible public key implementation
The library includes comprehensive tests for all functionality:
go test ./...- Key generation and validation
- Signing and verification operations
- ID creation and parsing
- Ethereum compatibility
- Keystore operations
- Error handling
github.com/libp2p/go-libp2p/core: libp2p cryptographic operationsgithub.com/decred/dcrd/dcrec/secp256k1/v4: Secp256k1 curve supportgolang.org/x/crypto: SHA3 and other cryptographic primitivesgithub.com/stretchr/testify: Testing utilities
- Key Storage: Always use secure storage for private keys
- Random Generation: The library uses cryptographically secure random generation
- Key Validation: Always validate key types before use
- Memory Safety: Keys are properly zeroed when possible
- Constant Time: Critical operations use constant-time implementations
- Ed25519: Fast signing and verification, small key sizes
- Secp256k1: Compatible with Bitcoin/Ethereum, moderate performance
- SHA3: Optimized implementation for hashing operations
Apache License 2.0 - see LICENSE file for details.
Contributions are welcome! Please ensure all tests pass and add tests for new functionality.