Sha256: 19c8b47f7b0eff50f461f18a85b40efa2ecf2abe614e0693e6daf19bb21695bd

Contents?: true

Size: 820 Bytes

Versions: 7

Compression:

Stored size: 820 Bytes

Contents

# frozen_string_literal: true

module Noise
  module Functions
    module Cipher
      class AesGcm
        def encrypt(k, n, ad, plaintext)
          cipher = OpenSSL::Cipher::AES.new(256, :GCM).encrypt
          cipher.key = k
          cipher.iv = nonce_to_bytes(n)
          cipher.auth_data = ad
          cipher.update(plaintext) + cipher.final + cipher.auth_tag
        end

        def decrypt(k, n, ad, ciphertext)
          cipher = OpenSSL::Cipher::AES.new(256, :GCM).decrypt
          cipher.key = k
          cipher.iv = nonce_to_bytes(n)
          cipher.auth_data = ad
          cipher.auth_tag = ciphertext[-16..-1]
          cipher.update(ciphertext[0...-16]) + cipher.final
        end

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

Version data entries

7 entries across 7 versions & 1 rubygems

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