| 
    
       # encoding: ASCII-8BIT
 
     | 
  
  
     | 
    
       # frozen_string_literal: false
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       p "RUBY: #{RUBY_VERSION}"
 
     | 
  
  
     | 
    
       require 'rbnacl'
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       class Encrypter
 
     | 
  
  
     | 
    
         extend RbNaCl::Sodium
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
         sodium_type :stream
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
         sodium_primitive :xchacha20
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
         sodium_function :stream_xchacha20_xor,
 
     | 
  
  
     | 
    
                         :crypto_stream_xchacha20_xor,
 
     | 
  
  
     | 
    
                         %i[pointer pointer ulong_long pointer pointer]
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
         attr_reader :key
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
         def initialize(key)
 
     | 
  
  
     | 
    
           @key = key
 
     | 
  
  
     | 
    
         end
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
         def encrypt_with_rbnacl_buffer(nonce, message)
 
     | 
  
  
     | 
    
           c = RbNaCl::Util.zeros(message.bytesize)
 
     | 
  
  
     | 
    
           self.class.stream_xchacha20_xor(c, message, message.bytesize, nonce, key)
 
     | 
  
  
     | 
    
           c
 
     | 
  
  
     | 
    
         end
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
         def encrypt_with_local_buffer(nonce, message)
 
     | 
  
  
     | 
    
           c = "\0" * message.bytesize
 
     | 
  
  
     | 
    
           self.class.stream_xchacha20_xor(c, message, message.bytesize, nonce, key)
 
     | 
  
  
     | 
    
           c
 
     | 
  
  
     | 
    
         end
 
     | 
  
  
     | 
    
       end
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       begin
 
     | 
  
  
     | 
    
         "\xC0".encode('UTF-8')
 
     | 
  
  
     | 
    
         p 'FAIL: plaintext is not valid UTF-8 and did not error during encoding to UTF-8'
 
     | 
  
  
     | 
    
       rescue StandardError
 
     | 
  
  
     | 
    
       end
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       SIZE = ARGV[0].to_i || 32
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       input = ("\xC0" * SIZE) + ' '
 
     | 
  
  
     | 
    
       nonce = 'B' * 24
 
     | 
  
  
     | 
    
       key = 'A' * 32
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       enc = Encrypter.new(key)
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       ciphertext_rbnacl = enc.encrypt_with_rbnacl_buffer(nonce, input)
 
     | 
  
  
     | 
    
       ciphertext_local = enc.encrypt_with_local_buffer(nonce, input)
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       plaintext_rbnacl = enc.encrypt_with_rbnacl_buffer(nonce, ciphertext_rbnacl)
 
     | 
  
  
     | 
    
       plaintext_local = enc.encrypt_with_local_buffer(nonce, ciphertext_local)
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       begin
 
     | 
  
  
     | 
    
         input.encode('UTF-8')
 
     | 
  
  
     | 
    
         p 'FAIL: input is not valid UTF-8 and did not error during encoding to UTF-8'
 
     | 
  
  
     | 
    
       rescue Encoding::UndefinedConversionError
 
     | 
  
  
     | 
    
       end
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       begin
 
     | 
  
  
     | 
    
         ciphertext_rbnacl.encode('UTF-8')
 
     | 
  
  
     | 
    
         p 'FAIL: ciphertext_rbnacl is not valid UTF-8 and did not error during encoding to UTF-8'
 
     | 
  
  
     | 
    
       rescue Encoding::UndefinedConversionError
 
     | 
  
  
     | 
    
         p 'OK: ciphertext_rbnacl is not valid UTF-8'
 
     | 
  
  
     | 
    
       end
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       begin
 
     | 
  
  
     | 
    
         ciphertext_local.encode('UTF-8')
 
     | 
  
  
     | 
    
         p 'FAIL: ciphertext_local is not valid UTF-8 and did not error during encoding to UTF-8'
 
     | 
  
  
     | 
    
       rescue Encoding::UndefinedConversionError
 
     | 
  
  
     | 
    
         p 'OK: ciphertext_local is not valid UTF-8'
 
     | 
  
  
     | 
    
       end
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       begin
 
     | 
  
  
     | 
    
         plaintext_rbnacl.encode('UTF-8')
 
     | 
  
  
     | 
    
         p 'FAIL: plaintext_rbnacl is not valid UTF-8 and did not error during encoding to UTF-8'
 
     | 
  
  
     | 
    
       rescue Encoding::UndefinedConversionError
 
     | 
  
  
     | 
    
         p 'OK: plaintext_rbnacl is not valid UTF-8'
 
     | 
  
  
     | 
    
       end
 
     | 
  
  
     | 
    
       
     | 
  
  
     | 
    
       begin
 
     | 
  
  
     | 
    
         plaintext_local.encode('UTF-8')
 
     | 
  
  
     | 
    
         p 'FAIL: plaintext_local is not valid UTF-8 and did not error during encoding to UTF-8'
 
     | 
  
  
     | 
    
       rescue Encoding::UndefinedConversionError
 
     | 
  
  
     | 
    
         p 'OK: plaintext_local is not valid UTF-8'
 
     | 
  
  
     | 
    
       end
 
     |