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
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
102 103 104 |
# File 'lib/rbnacl/aead/base.rb', line 102 def self.key_bytes self::KEYBYTES end |
.nonce_bytes ⇒ Integer
The nonce bytes for the AEAD class
88 89 90 |
# File 'lib/rbnacl/aead/base.rb', line 88 def self.nonce_bytes self::NPUBBYTES end |
.tag_bytes ⇒ Integer
The number bytes in the tag or authenticator from this AEAD class
116 117 118 |
# File 'lib/rbnacl/aead/base.rb', line 116 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
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rbnacl/aead/base.rb', line 67 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 |
# 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
109 110 111 |
# File 'lib/rbnacl/aead/base.rb', line 109 def key_bytes self.class.key_bytes end |
#nonce_bytes ⇒ Integer
The nonce bytes for the AEAD instance
95 96 97 |
# File 'lib/rbnacl/aead/base.rb', line 95 def nonce_bytes self.class.nonce_bytes end |
#primitive ⇒ Symbol
The crypto primitive for this aead instance
81 82 83 |
# File 'lib/rbnacl/aead/base.rb', line 81 def primitive self.class.primitive end |
#tag_bytes ⇒ Integer
The number of bytes in the tag or authenticator for this AEAD instance
123 124 125 |
# File 'lib/rbnacl/aead/base.rb', line 123 def tag_bytes self.class.tag_bytes end |