Sha256: da1f61def96886319e7ce94593ea8ca181ba93becae7e4d704772f59ab4a5fe7

Contents?: true

Size: 857 Bytes

Versions: 5

Compression:

Stored size: 857 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.new(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.new(e)
        end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
noise-ruby-0.7.1 lib/noise/functions/cipher/cha_cha_poly.rb
noise-ruby-0.7.0 lib/noise/functions/cipher/cha_cha_poly.rb
noise-ruby-0.6.3 lib/noise/functions/cipher/cha_cha_poly.rb
noise-ruby-0.6.2 lib/noise/functions/cipher/cha_cha_poly.rb
noise-ruby-0.6.1 lib/noise/functions/cipher/cha_cha_poly.rb