Class: Miscreant::Internals::AES::CTR

Inherits:
Object
  • Object
show all
Defined in:
lib/miscreant/internals/aes/ctr.rb

Overview

The AES-CTR unauthenticated stream cipher

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ CTR

Create a new AES-CTR instance

Parameters:

  • key (String)

    16-byte or 32-byte Encoding::BINARY cryptographic key



12
13
14
15
16
17
# File 'lib/miscreant/internals/aes/ctr.rb', line 12

def initialize(key)
  Util.validate_bytestring("key", key, length: [16, 32])
  @cipher = OpenSSL::Cipher::AES.new(key.bytesize * 8, :CTR)
  @cipher.encrypt
  @cipher.key = key
end

Instance Method Details

#encrypt(iv, message) ⇒ Object

Encrypt the given message using the given counter (i.e. IV)

Parameters:

  • iv (String)

    initial counter value as a 16-byte Encoding::BINARY string

  • message (String)

    message to be encrypted



30
31
32
33
34
35
36
# File 'lib/miscreant/internals/aes/ctr.rb', line 30

def encrypt(iv, message)
  Util.validate_bytestring("IV", iv, length: Block::SIZE)
  return "".b if message.empty?

  @cipher.iv = iv
  @cipher.update(message) + @cipher.final
end

#inspectString

Inspect this AES-CTR instance

Returns:

  • (String)

    description of this instance



22
23
24
# File 'lib/miscreant/internals/aes/ctr.rb', line 22

def inspect
  to_s
end