Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/lib/rex/crypto/aes256_spec.rb
Views: 11784
require 'spec_helper'1require 'securerandom'234RSpec.describe Rex::Crypto do56let(:iv) {7SecureRandom.random_bytes(16)8}910let(:key) {11SecureRandom.random_bytes(32)12}1314let(:value) {15'Hello World'16}1718describe '#encrypt_aes256' do19it 'raises an exception due to a short IV' do20iv = SecureRandom.random_bytes(1)21# Because it could raise either a OpenSSL::Cipher::CipherError or an ArgumentError22# dependong on the environment, we will just expect it to raise an exception23expect { Rex::Crypto.encrypt_aes256(iv, key, value) }.to raise_exception ArgumentError24end2526it 'raises an exception due to a short key' do27key = SecureRandom.random_bytes(1)28# Because it could raise either a OpenSSL::Cipher::CipherError or an ArgumentError29# dependong on the environment, we will just expect it to raise an exception30expect { Rex::Crypto.encrypt_aes256(iv, key, value) }.to raise_exception ArgumentError31end3233it 'encrypts the string Hello World' do34encrypted_str = Rex::Crypto.encrypt_aes256(iv, key, value)35expect(encrypted_str).not_to eq(value)36end37end3839describe '#decrypt_aes256' do40it 'raises an exception due to a short IV' do41iv = SecureRandom.random_bytes(1)42# Because it could raise either a OpenSSL::Cipher::CipherError or an ArgumentError43# dependong on the environment, we will just expect it to raise an exception44expect { Rex::Crypto.decrypt_aes256(iv, key, value) }.to raise_exception ArgumentError45end4647it 'raises an exception due to a short key' do48key = SecureRandom.random_bytes(1)49# Because it could raise either a OpenSSL::Cipher::CipherError or an ArgumentError50# dependong on the environment, we will just expect it to raise an exception51expect { Rex::Crypto.decrypt_aes256(iv, key, value) }.to raise_exception ArgumentError52end5354it 'decrypts the value to Hello World' do55encrypted_str = Rex::Crypto.encrypt_aes256(iv, key, value)56decrypted_str = Rex::Crypto.decrypt_aes256(iv, key, encrypted_str)57expect(decrypted_str).to eq(value)58end59end6061end626364