Beryl::Crypto
Cryptography written in Crystal.
The calls on this module always use the algorithm Beryl recommends today:
Beryl::Crypto.digest("contents of a file") # SHA-256
tag = Beryl::Crypto.authenticate("message", key: secret_key) # HMAC-SHA256
Beryl::Crypto.verify_authentication("message", tag, key: secret_key) # => true
Beryl::Crypto.derive_key(shared_secret, info: "cookies", bytesize: 32) # HKDF-SHA256
sealed = Beryl::Crypto.encrypt("a private note", key: secret_key) # XChaCha20-Poly1305
Beryl::Crypto.decrypt(sealed, key: secret_key) # => the note, or nil
signing_key = Beryl::Crypto.generate_signing_key # Ed25519
signature = Beryl::Crypto.sign("a message", signing_key: signing_key)
Beryl::Crypto.verify_signature("a message", signature, verification_key: signing_key.verification_key) # => true
mine = Beryl::Crypto.generate_key_agreement_key # X25519
Beryl::Crypto.agree_on_shared_secret(mine, peer_public_key) # => 32 bytes, or nil
Beryl::Crypto::V1 names each algorithm exactly, for code that must keep
producing the same bytes. Beryl::Crypto::Compatibility holds MD5, SHA-1,
PBKDF2 and AES-CBC for formats that require them.
Constants
The size of the random nonce encrypt places in front of each message.
Class methods
Returns the HMAC-SHA256 authentication tag of message under key (32 bytes).
Returns the message inside encrypted_message (the result of
encrypt), or nil when it was altered, was encrypted under another
key, or goes with other associated data.
Returns bytesize bytes of key derived from input_key_material with HKDF-SHA256. info names the purpose, so that different purposes get unrelated keys; salt is optional and need not be secret.
Returns message encrypted and authenticated under key (32 bytes)
with XChaCha20-Poly1305: a fresh random nonce (24 bytes), then the
ciphertext, then the tag (16 bytes). associated_data is authenticated
and left out of the result; decrypt must be given the same bytes.
Returns a new X25519 key agreement key (RFC 7748). Its public_key is
the half to send to the peer.
Returns a new Ed25519 signing key (RFC 8032). Its verification_key
is the public half to hand out.
Returns the Ed25519 signature of message (64 bytes).
Nested types
- Beryl::Crypto::Compatibility
- Beryl::Crypto::DerivedKeySizeIsNegative
- Beryl::Crypto::DerivedKeyTooLong
- Beryl::Crypto::Error
- Beryl::Crypto::FaultInPrivateKeyOperation
- Beryl::Crypto::InvalidPrivateKey
- Beryl::Crypto::InvalidPublicKey
- Beryl::Crypto::IterationCountTooSmall
- Beryl::Crypto::KeySizeMismatch
- Beryl::Crypto::KeySizeNotSupported
- Beryl::Crypto::KeyWiped
- Beryl::Crypto::MalformedPrivateKey
- Beryl::Crypto::MessageTooLong
- Beryl::Crypto::MessageTooLongForModulus
- Beryl::Crypto::NonceSizeMismatch
- Beryl::Crypto::OutputSizeMismatch
- Beryl::Crypto::PublicExponentNotSupported
- Beryl::Crypto::SaltTooLongForModulus
- Beryl::Crypto::TagSizeNotSupported
- Beryl::Crypto::TruncatedTagTooShort
- Beryl::Crypto::UnsupportedProcessor
- Beryl::Crypto::V1