Class: Stenohttp2::Common::CipherBuilder

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/stenohttp2/common/cipher_builder.rb

Constant Summary collapse

DEFAULT_SALT =

OpenSSL::Random.random_bytes(16)

"\xCA|k\xC3Qw(\xB1E\xF3<\xA7\xCC\x96$\x8A".freeze
DEFAULT_PASSWORD =

don't try this at home

'this is a secret'.freeze
CIPHER_TYPE =
'AES-256-CFB'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encryptor: true) ⇒ CipherBuilder

Returns a new instance of CipherBuilder.



26
27
28
# File 'lib/stenohttp2/common/cipher_builder.rb', line 26

def initialize(encryptor: true)
  @encryptor = encryptor
end

Class Method Details

.decryptorObject



20
21
22
# File 'lib/stenohttp2/common/cipher_builder.rb', line 20

def decryptor
  new(encryptor: false).build
end

.encryptorObject



16
17
18
# File 'lib/stenohttp2/common/cipher_builder.rb', line 16

def encryptor
  new(encryptor: true).build
end

Instance Method Details

#buildObject



31
32
33
34
35
36
37
38
# File 'lib/stenohttp2/common/cipher_builder.rb', line 31

def build
  cipher = OpenSSL::Cipher.new(CIPHER_TYPE)
  encryptor ? cipher.encrypt : cipher.decrypt
  cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(
    DEFAULT_PASSWORD, DEFAULT_SALT, 20_000, cipher.key_len
  )
  cipher
end