Sha256: 342b9835cd8f4855986be26595cdb7c772d1abbcd2e6fb828167a064435917f8

Contents?: true

Size: 820 Bytes

Versions: 102

Compression:

Stored size: 820 Bytes

Contents

# frozen_string_literal: true

# From the manual page https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL.html

require 'openssl'

module Example
  def Example.encrypt
    cipher = OpenSSL::Cipher.new 'AES-256-CBC'
    cipher.encrypt
    iv = cipher.random_iv

    pwd = 'some hopefully not to easily guessable password'
    salt = OpenSSL::Random.random_bytes 16
    iter = 20000
    key_len = cipher.key_len
    digest = OpenSSL::Digest::SHA256.new

    key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
    cipher.key = key

    document = 'the document'

    encrypted = cipher.update document
    encrypted << cipher.final
    encrypted
  end
end

if __FILE__ == $0
  ciphertext = Example.encrypt
  require 'base64'
  puts "Computed ciphertext #{Base64.urlsafe_encode64(ciphertext)}"
end

Version data entries

102 entries across 102 versions & 1 rubygems

Version Path
appmap-0.83.4 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.83.3 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.83.2 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.83.1 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.83.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.82.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.81.1 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.81.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.80.2 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.80.1 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.80.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.79.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.78.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.77.4 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.77.3 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.77.2 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.77.1 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.77.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.76.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb
appmap-0.75.0 test/fixtures/openssl_recorder/lib/openssl_encrypt.rb