Module: Mongo::Crypt::Hooks Private

Defined in:
lib/mongo/crypt/hooks.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

A helper module that implements cryptography methods required for native Ruby crypto hooks. These methods are passed into FFI as C callbacks and called from the libmongocrypt library.

API:

  • private

Class Method Summary collapse

Class Method Details

.aes(key, iv, input, decrypt: false) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An AES encrypt or decrypt method.

Raises:

  • Exceptions raised during encryption are propagated to caller.

API:

  • private

Parameters:

  • The 32-byte AES encryption key

  • The 16-byte AES IV

  • The data to be encrypted/decrypted

  • (defaults to: false)

    Whether this method is decrypting. Default is false, which means the method will create an encryption cipher by default

Returns:

  • Output



42
43
44
45
46
47
48
49
50
51
# File 'lib/mongo/crypt/hooks.rb', line 42

def aes(key, iv, input, decrypt: false)
  cipher = OpenSSL::Cipher::AES.new(256, :CBC)

  decrypt ? cipher.decrypt : cipher.encrypt
  cipher.key = key
  cipher.iv = iv
  cipher.padding = 0

  encrypted = cipher.update(input)
end

.hash_sha256(input) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A crypto hash (SHA-256) function

Raises:

  • Exceptions raised during encryption are propagated to caller.

API:

  • private

Parameters:

  • The data to be hashed

Returns:



87
88
89
# File 'lib/mongo/crypt/hooks.rb', line 87

def hash_sha256(input)
  Digest::SHA2.new(256).digest(input)
end

.hmac_sha(digest_name, key, input) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An HMAC SHA-512 or SHA-256 function

Raises:

  • Exceptions raised during encryption are propagated to caller.

API:

  • private

Parameters:

  • The name of the digest, either “SHA256” or “SHA512”

  • The 32-byte AES encryption key

  • The data to be tagged

Returns:



75
76
77
# File 'lib/mongo/crypt/hooks.rb', line 75

def hmac_sha(digest_name, key, input)
  OpenSSL::HMAC.digest(digest_name, key, input)
end

.random(num_bytes) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Crypto secure random function

Raises:

  • Exceptions raised during encryption are propagated to caller.

API:

  • private

Parameters:

  • The number of random bytes requested

Returns:



61
62
63
# File 'lib/mongo/crypt/hooks.rb', line 61

def random(num_bytes)
  SecureRandom.random_bytes(num_bytes)
end