Module: Hide

Defined in:
lib/hide.rb,
lib/hide/ae.rb,
lib/hide/version.rb

Overview

The primary module for ‘hide`

Provides basic encryption/decryption support

Defined Under Namespace

Classes: AE

Constant Summary collapse

VERSION =

Gem version specifier

"1.0.2".freeze

Class Method Summary collapse

Class Method Details

.decrypt(data, key, salt, iter, iv, key_length = 32) ⇒ Object

Decrypts any datastream with provided decryption credential and configuration.

This method does not provide support for authenticated encryption. For authenticated encryption support, please use ‘Hide::AE` module

Returns the decrypted data



37
38
39
40
41
42
43
# File 'lib/hide.rb', line 37

def decrypt data, key, salt, iter, iv, key_length = 32
  decipher = OpenSSL::Cipher.new "AES-256-CBC"
  decipher.decrypt
  decipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(key, salt, iter, key_length)
  decipher.iv = iv
  decipher.update(data) + decipher.final
end

.encrypt(data, key, salt, iter, iv = SecureRandom.random_bytes(16), key_length = 32) ⇒ Object

Encrypts a data stream without any authentication identifier.

For authenticated encryption support please use ‘Hide::AE` module

Returns a hash containing encrypted data and IV



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hide.rb', line 17

def encrypt(
  data, key, salt, iter, iv = SecureRandom.random_bytes(16), key_length = 32
)
  cipher = OpenSSL::Cipher.new "AES-256-CBC"
  cipher.encrypt
  cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(key, salt, iter, key_length)
  cipher.iv = iv
  {
    data: cipher.update(data) + cipher.final,
    iv: iv
  }
end