Class: Noise::Functions::Cipher::ChaChaPoly
- Inherits:
-
Object
- Object
- Noise::Functions::Cipher::ChaChaPoly
- Defined in:
- lib/noise/functions/cipher/cha_cha_poly.rb
Constant Summary collapse
- MAX_NONCE =
2**64 - 1
Instance Method Summary collapse
- #decrypt(k, n, ad, ciphertext) ⇒ Object
- #encrypt(k, n, ad, plaintext) ⇒ Object
- #nonce_to_bytes(n) ⇒ Object
-
#rekey(k) ⇒ Object
Returns a new 32-byte cipher key as a pseudorandom function of k.
Instance Method Details
#decrypt(k, n, ad, ciphertext) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 16 def decrypt(k, n, ad, ciphertext) cipher = RbNaCl::AEAD::ChaCha20Poly1305IETF.new(String.new(k).force_encoding('ASCII-8BIT')) cipher.decrypt(nonce_to_bytes(n), ciphertext, ad) rescue ::RbNaCl::CryptoError => e raise Noise::Exceptions::DecryptError, e end |
#encrypt(k, n, ad, plaintext) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 9 def encrypt(k, n, ad, plaintext) cipher = RbNaCl::AEAD::ChaCha20Poly1305IETF.new(String.new(k).force_encoding('ASCII-8BIT')) cipher.encrypt(nonce_to_bytes(n), plaintext, ad) rescue ::RbNaCl::CryptoError => e raise Noise::Exceptions::EncryptError, e end |
#nonce_to_bytes(n) ⇒ Object
23 24 25 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 23 def nonce_to_bytes(n) "\x00" * 4 + format('%16x', n).htb.reverse end |
#rekey(k) ⇒ Object
Returns a new 32-byte cipher key as a pseudorandom function of k. If this function is not specifically defined for some set of cipher functions, then it defaults to returning the first 32 bytes from ENCRYPT(k,maxnonce, zerolen, zeros), where maxnonce equals 2**64-1, zerolen is a zero-length byte sequence, and zeros is a sequence of 32 bytes filled with zeros.
33 34 35 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 33 def rekey(k) encrypt(k, MAX_NONCE, '', "\x00" * 32)[0..32] end |