Class: RbNaCl::AEAD::Base
- Inherits:
-
Object
- Object
- RbNaCl::AEAD::Base
- Defined in:
- lib/rbnacl/aead/base.rb
Overview
Abstract base class for Authenticated Encryption with Additional Data
This construction encrypts a message, and computes an authentication tag for the encrypted message and some optional additional data
RbNaCl provides wrappers for both ChaCha20-Poly1305 AEAD implementations in libsodium: the original, and the IETF version.
Direct Known Subclasses
ChaCha20Poly1305IETF, ChaCha20Poly1305Legacy, XChaCha20Poly1305IETF
Constant Summary collapse
- KEYBYTES =
Number of bytes in a valid key
0
- NPUBBYTES =
Number of bytes in a valid nonce
0
Class Method Summary collapse
-
.key_bytes ⇒ Integer
The key bytes for the AEAD class.
-
.nonce_bytes ⇒ Integer
The nonce bytes for the AEAD class.
-
.tag_bytes ⇒ Integer
The number bytes in the tag or authenticator from this AEAD class.
Instance Method Summary collapse
-
#decrypt(nonce, ciphertext, additional_data) ⇒ String
Decrypts and verifies an encrypted message with additional authenticated data.
-
#encrypt(nonce, message, additional_data) ⇒ String
Encrypts and authenticates a message with additional authenticated data.
-
#initialize(key) ⇒ RbNaCl::AEAD::Chacha20Poly1305IETF
constructor
Create a new AEAD using the IETF chacha20poly1305 construction.
-
#key_bytes ⇒ Integer
The key bytes for the AEAD instance.
-
#nonce_bytes ⇒ Integer
The nonce bytes for the AEAD instance.
-
#primitive ⇒ Symbol
The crypto primitive for this aead instance.
-
#tag_bytes ⇒ Integer
The number of bytes in the tag or authenticator for this AEAD instance.
Constructor Details
#initialize(key) ⇒ RbNaCl::AEAD::Chacha20Poly1305IETF
Create a new AEAD using the IETF chacha20poly1305 construction
Sets up AEAD with a secret key for encrypting and decrypting messages.
32 33 34 |
# File 'lib/rbnacl/aead/base.rb', line 32 def initialize(key) @key = Util.check_string(key, key_bytes, "Secret key") end |
Class Method Details
.key_bytes ⇒ Integer
The key bytes for the AEAD class
104 105 106 |
# File 'lib/rbnacl/aead/base.rb', line 104 def self.key_bytes self::KEYBYTES end |
.nonce_bytes ⇒ Integer
The nonce bytes for the AEAD class
90 91 92 |
# File 'lib/rbnacl/aead/base.rb', line 90 def self.nonce_bytes self::NPUBBYTES end |
.tag_bytes ⇒ Integer
The number bytes in the tag or authenticator from this AEAD class
118 119 120 |
# File 'lib/rbnacl/aead/base.rb', line 118 def self.tag_bytes self::ABYTES end |
Instance Method Details
#decrypt(nonce, ciphertext, additional_data) ⇒ String
Decrypts and verifies an encrypted message with additional authenticated data
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rbnacl/aead/base.rb', line 68 def decrypt(nonce, ciphertext, additional_data) Util.check_length(nonce, nonce_bytes, "Nonce") = Util.zeros(1) = Util.zeros(data_len(ciphertext) - tag_bytes) success = do_decrypt(, , nonce, ciphertext, additional_data) raise CryptoError, "Decryption failed. Ciphertext failed verification." unless success end |
#encrypt(nonce, message, additional_data) ⇒ String
Encrypts and authenticates a message with additional authenticated data
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rbnacl/aead/base.rb', line 46 def encrypt(nonce, , additional_data) Util.check_length(nonce, nonce_bytes, "Nonce") ciphertext_len = Util.zeros(1) ciphertext = Util.zeros(data_len() + tag_bytes) success = do_encrypt(ciphertext, ciphertext_len, nonce, , additional_data) raise CryptoError, "Encryption failed" unless success ciphertext end |
#key_bytes ⇒ Integer
The key bytes for the AEAD instance
111 112 113 |
# File 'lib/rbnacl/aead/base.rb', line 111 def key_bytes self.class.key_bytes end |
#nonce_bytes ⇒ Integer
The nonce bytes for the AEAD instance
97 98 99 |
# File 'lib/rbnacl/aead/base.rb', line 97 def nonce_bytes self.class.nonce_bytes end |
#primitive ⇒ Symbol
The crypto primitive for this aead instance
83 84 85 |
# File 'lib/rbnacl/aead/base.rb', line 83 def primitive self.class.primitive end |
#tag_bytes ⇒ Integer
The number of bytes in the tag or authenticator for this AEAD instance
125 126 127 |
# File 'lib/rbnacl/aead/base.rb', line 125 def tag_bytes self.class.tag_bytes end |