cryptography

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cryptography Skill

加密技术Skill

Sorcha.Cryptography provides multi-algorithm support (ED25519, P-256, RSA-4096), symmetric encryption (AES, ChaCha20), and BIP39/44 HD wallet derivation. All operations return
CryptoResult<T>
for explicit error handling—no exceptions for crypto failures.
Sorcha.Cryptography提供多算法支持(ED25519、P-256、RSA-4096)、对称加密(AES、ChaCha20)以及BIP39/44 HD钱包派生功能。所有操作均返回
CryptoResult<T>
以实现显式错误处理——加密操作失败时不会抛出异常。

Quick Start

快速开始

Key Generation

密钥生成

csharp
// Inject ICryptoModule
var keySetResult = await _cryptoModule.GenerateKeySetAsync(WalletNetworks.ED25519);
if (!keySetResult.IsSuccess)
    throw new InvalidOperationException($"Key generation failed: {keySetResult.Status}");

var keySet = keySetResult.Value!;
// keySet.PrivateKey.Key = 64 bytes (ED25519)
// keySet.PublicKey.Key = 32 bytes (ED25519)
csharp
// 注入ICryptoModule
var keySetResult = await _cryptoModule.GenerateKeySetAsync(WalletNetworks.ED25519);
if (!keySetResult.IsSuccess)
    throw new InvalidOperationException($"密钥生成失败: {keySetResult.Status}");

var keySet = keySetResult.Value!;
// keySet.PrivateKey.Key = 64字节 (ED25519)
// keySet.PublicKey.Key = 32字节 (ED25519)

Signing & Verification

签名与验证

csharp
// Hash then sign
byte[] hash = SHA256.HashData(transactionData);
var signResult = await _cryptoModule.SignAsync(
    hash,
    (byte)WalletNetworks.ED25519,
    keySet.PrivateKey.Key!);

// Verify
var status = await _cryptoModule.VerifyAsync(
    signResult.Value!,
    hash,
    (byte)WalletNetworks.ED25519,
    keySet.PublicKey.Key!);
bool isValid = status == CryptoStatus.Success;
csharp
// 先哈希再签名
byte[] hash = SHA256.HashData(transactionData);
var signResult = await _cryptoModule.SignAsync(
    hash,
    (byte)WalletNetworks.ED25519,
    keySet.PrivateKey.Key!);

// 验证
var status = await _cryptoModule.VerifyAsync(
    signResult.Value!,
    hash,
    (byte)WalletNetworks.ED25519,
    keySet.PublicKey.Key!);
bool isValid = status == CryptoStatus.Success;

HD Wallet Creation

HD钱包创建

csharp
var keyRing = await _keyManager.CreateMasterKeyRingAsync(WalletNetworks.ED25519, password: null);
// keyRing.Mnemonic = "word1 word2 ... word12" — user must backup
// keyRing.MasterKeySet contains derived keys
csharp
var keyRing = await _keyManager.CreateMasterKeyRingAsync(WalletNetworks.ED25519, password: null);
// keyRing.Mnemonic = "word1 word2 ... word12" — 用户必须备份
// keyRing.MasterKeySet 包含派生的密钥

Key Concepts

核心概念

ConceptUsageExample
WalletNetworks
Algorithm selection
ED25519
,
NISTP256
,
RSA4096
CryptoResult<T>
Error handling
.IsSuccess
,
.Status
,
.Value
KeySet
Public/private pair
.PrivateKey.Key
,
.PublicKey.Key
KeyRing
Full wallet with mnemonic
.Mnemonic
,
.MasterKeySet
.Zeroize()
Secure memory clearingCall when done with keys
概念用途示例
WalletNetworks
算法选择
ED25519
,
NISTP256
,
RSA4096
CryptoResult<T>
错误处理
.IsSuccess
,
.Status
,
.Value
KeySet
公钥/私钥对
.PrivateKey.Key
,
.PublicKey.Key
KeyRing
助记词完整钱包
.Mnemonic
,
.MasterKeySet
.Zeroize()
安全内存清理完成密钥操作后调用

Common Patterns

常见模式

Platform-Specific Key Storage

平台特定密钥存储

csharp
// Encryption provider abstraction handles platform differences
var encrypted = await _encryptionProvider.EncryptAsync(privateKey, "wallet-key-id");
// Windows: DPAPI, Linux: Secret Service, Dev: AES-GCM
csharp
// 加密提供程序抽象处理平台差异
var encrypted = await _encryptionProvider.EncryptAsync(privateKey, "wallet-key-id");
// Windows: DPAPI, Linux: Secret Service, 开发环境: AES-GCM

Address Generation

地址生成

csharp
var address = _walletUtilities.PublicKeyToWallet(publicKey, (byte)WalletNetworks.ED25519);
// Returns: "ws1q8tuvvdykly8n0fy5jkuu8cjw0fu0p6jl5rp9g..."
csharp
var address = _walletUtilities.PublicKeyToWallet(publicKey, (byte)WalletNetworks.ED25519);
// 返回: "ws1q8tuvvdykly8n0fy5jkuu8cjw0fu0p6jl5rp9g..."

See Also

另请参阅

  • patterns - Algorithm selection, signing workflows, key management
  • workflows - Wallet creation, transaction signing, encryption
  • patterns - 算法选择、签名工作流、密钥管理
  • workflows - 钱包创建、交易签名、加密

Related Skills

相关技能

  • See the nbitcoin skill for HD wallet derivation paths (BIP32/39/44)
  • See the postgresql skill for encrypted key storage patterns
  • See the xunit and fluent-assertions skills for testing crypto code
  • 有关HD钱包派生路径(BIP32/39/44),请查看nbitcoin skill
  • 有关加密密钥存储模式,请查看postgresql skill
  • 有关加密代码测试,请查看xunitfluent-assertions skills

Documentation Resources

文档资源

Fetch latest cryptography documentation with Context7.
How to use Context7:
  1. Use
    mcp__context7__resolve-library-id
    to search for "libsodium" or "System.Security.Cryptography"
  2. Query with
    mcp__context7__query-docs
    using the resolved library ID
Recommended Queries:
  • "ED25519 signing verification"
  • "AES-GCM authenticated encryption"
  • "BIP39 mnemonic seed derivation"
使用Context7获取最新加密技术文档。
如何使用Context7:
  1. 使用
    mcp__context7__resolve-library-id
    搜索"libsodium"或"System.Security.Cryptography"
  2. 使用解析后的库ID,通过
    mcp__context7__query-docs
    进行查询
推荐查询:
  • "ED25519 signing verification"
  • "AES-GCM authenticated encryption"
  • "BIP39 mnemonic seed derivation"