CryptX - 加密工具包
CryptX 是发行版的入口点。在正常代码中,请加载下面列出的具体模块之一。
## one-shot hashing
use Crypt::Digest qw(digest_data_hex);
my $sha256 = digest_data_hex('SHA256', 'hello world');
## classic AES-CBC encryption with padding
use Crypt::Mode::CBC;
my $cbc = Crypt::Mode::CBC->new('AES');
my $iv = random_bytes(16); # 16-byte AES block-size IV
my $cbc_ciphertext = $cbc->encrypt('hello world', $key, $iv);
## authenticated encryption (AEAD) with AES
use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate);
my $key = random_bytes(32); # 32-byte AES-256 key
my $nonce = random_bytes(12); # 12-byte unique nonce
my ($ciphertext, $tag) = gcm_encrypt_authenticate('AES', $key, $nonce, 'header', 'hello world');
## message authentication
use Crypt::Mac::HMAC qw(hmac_hex);
my $mac = hmac_hex('SHA256', $key, 'hello world');
## secure random data + UUID helpers
use Crypt::PRNG qw(random_bytes random_string);
use Crypt::Misc qw(random_v4uuid random_v7uuid);
my $salt = random_bytes(16);
my $token = random_string(24);
my $uuid4 = random_v4uuid();
my $uuid7 = random_v7uuid();
## classic password-based key derivation
use Crypt::KeyDerivation qw(pbkdf2);
my $dk = pbkdf2('password', $salt, 100_000, 'SHA256', 32);
## bare stream cipher (authenticate separately)
use Crypt::Stream::ChaCha;
my $stream = Crypt::Stream::ChaCha->new($key, $nonce);
my $stream_ciphertext = $stream->crypt('hello world');
## modern signatures
use Crypt::PK::Ed25519;
my $signer = Crypt::PK::Ed25519->new->generate_key;
my $sig = $signer->sign_message('hello world');
my $ok = $signer->verify_message($sig, 'hello world');
## key agreement
use Crypt::PK::X25519;
my $alice = Crypt::PK::X25519->new->generate_key;
my $bob = Crypt::PK::X25519->new->generate_key;
my $shared_secret = $alice->shared_secret($bob);
Perl 密码学模块,构建于随附的 LibTomCrypt 库之上。 该发行版还包含 Math::BigInt::LTM,一个基于随附的 LibTomMath 库构建的 Math::BigInt 后端, 该库由 LibTomCrypt 内部使用。
此模块主要作为顶层发行版/文档页面。实际使用时,请使用下面列出的具体模块之一。
对于新设计,建议优先使用认证加密(AEAD),而非裸加密模式:
random_bytes、random_bytes_hex、random_bytes_b64、random_bytes_b64u、random_string 和 random_string_from 覆盖了大多数用例。OO API 和特定算法的包装器(Crypt::PRNG::ChaCha20、Crypt::PRNG::Fortuna 等)主要用于确定性流或与特定 PRNG 的互操作。random_v4uuid 生成不透明的随机标识符。当您需要大致按创建时间排序(毫秒粒度)的标识符时,使用 random_v7uuid。UUID 是标识符,不能替代秘密随机字节。流密码逐字节加密数据,无需块填充。对于大多数应用程序,请优先选择 AEAD 模式(见上文),它将加密与认证捆绑在一起。仅当您单独处理认证时才使用裸流密码。
仅当认证单独处理或不需要认证时才使用这些模式:
单独的 Crypt::Cipher::AES、Crypt::Cipher::Twofish 等模块实现原始的单个块加密,很少直接使用。在几乎所有情况下,您都应该通过 AEAD 模式(Crypt::AuthEnc::GCM、Crypt::AuthEnc::CCM)或分组密码模式(Crypt::Mode::CBC、Crypt::Mode::CTR)来使用它们。选择密码时,AES 是默认选择;它在大多数现代 CPU 上都有硬件加速。
仅将 Crypt::Checksum::CRC32 和 Crypt::Checksum::Adler32 用于非对抗性完整性检查,例如意外损坏检测。它们不是密码学完整性或真实性机制。对于密码学用途,请优先使用 Crypt::Digest、Crypt::Mac,或来自 Crypt::AuthEnc 的 AEAD 模式。