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
4 zero bytes followed by n as a little-endian 64 bit integer.
-
#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, "Decrpyt failed. #{e.}", e.backtrace 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, "Encrypt failed. #{e.}", e.backtrace end |
#nonce_to_bytes(n) ⇒ Object
4 zero bytes followed by n as a little-endian 64 bit integer.
24 25 26 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 24 def nonce_to_bytes(n) "\x00" * 4 + [n].pack('Q<') 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.
34 35 36 |
# File 'lib/noise/functions/cipher/cha_cha_poly.rb', line 34 def rekey(k) encrypt(k, MAX_NONCE, '', "\x00" * 32)[0...32] end |