Sha256: c1d2fe9089932b15ccf0f7a9b092993435bececb9edcca7b51dd9a4002e894fe

Contents?: true

Size: 849 Bytes

Versions: 1

Compression:

Stored size: 849 Bytes

Contents

# frozen_string_literal: true

module Noise
  module Functions
    module Cipher
      class ChaChaPoly
        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

        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

        def nonce_to_bytes(n)
          "\x00" * 4 + format('%16x', n).htb.reverse
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
noise-ruby-0.7.3 lib/noise/functions/cipher/cha_cha_poly.rb