CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/rex/crypto/aes256_spec.rb
Views: 11784
1
require 'spec_helper'
2
require 'securerandom'
3
4
5
RSpec.describe Rex::Crypto do
6
7
let(:iv) {
8
SecureRandom.random_bytes(16)
9
}
10
11
let(:key) {
12
SecureRandom.random_bytes(32)
13
}
14
15
let(:value) {
16
'Hello World'
17
}
18
19
describe '#encrypt_aes256' do
20
it 'raises an exception due to a short IV' do
21
iv = SecureRandom.random_bytes(1)
22
# Because it could raise either a OpenSSL::Cipher::CipherError or an ArgumentError
23
# dependong on the environment, we will just expect it to raise an exception
24
expect { Rex::Crypto.encrypt_aes256(iv, key, value) }.to raise_exception ArgumentError
25
end
26
27
it 'raises an exception due to a short key' do
28
key = SecureRandom.random_bytes(1)
29
# Because it could raise either a OpenSSL::Cipher::CipherError or an ArgumentError
30
# dependong on the environment, we will just expect it to raise an exception
31
expect { Rex::Crypto.encrypt_aes256(iv, key, value) }.to raise_exception ArgumentError
32
end
33
34
it 'encrypts the string Hello World' do
35
encrypted_str = Rex::Crypto.encrypt_aes256(iv, key, value)
36
expect(encrypted_str).not_to eq(value)
37
end
38
end
39
40
describe '#decrypt_aes256' do
41
it 'raises an exception due to a short IV' do
42
iv = SecureRandom.random_bytes(1)
43
# Because it could raise either a OpenSSL::Cipher::CipherError or an ArgumentError
44
# dependong on the environment, we will just expect it to raise an exception
45
expect { Rex::Crypto.decrypt_aes256(iv, key, value) }.to raise_exception ArgumentError
46
end
47
48
it 'raises an exception due to a short key' do
49
key = SecureRandom.random_bytes(1)
50
# Because it could raise either a OpenSSL::Cipher::CipherError or an ArgumentError
51
# dependong on the environment, we will just expect it to raise an exception
52
expect { Rex::Crypto.decrypt_aes256(iv, key, value) }.to raise_exception ArgumentError
53
end
54
55
it 'decrypts the value to Hello World' do
56
encrypted_str = Rex::Crypto.encrypt_aes256(iv, key, value)
57
decrypted_str = Rex::Crypto.decrypt_aes256(iv, key, encrypted_str)
58
expect(decrypted_str).to eq(value)
59
end
60
end
61
62
end
63
64