Sha256: f26e42ec92a304fecbae13df557da2d16665cbc793388c7b0a9a9c24563efacc

Contents?: true

Size: 656 Bytes

Versions: 1

Compression:

Stored size: 656 Bytes

Contents

require 'rbnacl/libsodium'

require 'cryptor/cipher'

module Cryptor
  module Ciphers
    # XSalsa20+Poly1305 authenticated stream cipher
    class XSalsa20Poly1305 < Cipher
      register :xsalsa20poly1305, key_bytes: RbNaCl::SecretBoxes::XSalsa20Poly1305.key_bytes

      def encrypt(key, plaintext)
        box(key).encrypt(plaintext)
      end

      def decrypt(key, ciphertext)
        box(key).decrypt(ciphertext)
      rescue RbNaCl::CryptoError => ex
        raise CorruptedMessageError, ex.to_s
      end

      private

      def box(key)
        RbNaCl::SimpleBox.new(RbNaCl::SecretBoxes::XSalsa20Poly1305.new(key))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cryptor-0.0.2 lib/cryptor/ciphers/xsalsa20poly1305.rb