Add rspec and fix a bug in XOR in the process
This commit is contained in:
parent
1247158968
commit
11001d010c
|
@ -18,8 +18,7 @@ module Rex
|
|||
|
||||
value.each_byte do |byte|
|
||||
xor_byte = byte ^ xor_key
|
||||
xor_byte = [xor_byte].pack('c').first
|
||||
buf << xor_byte
|
||||
buf << [xor_byte].pack('c')
|
||||
end
|
||||
|
||||
buf
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
require 'spec_helper'
|
||||
require 'securerandom'
|
||||
require 'openssl'
|
||||
|
||||
|
||||
describe Rex::Text do
|
||||
|
||||
let(:iv) {
|
||||
SecureRandom.random_bytes(16)
|
||||
}
|
||||
|
||||
let(:key) {
|
||||
SecureRandom.random_bytes(32)
|
||||
}
|
||||
|
||||
let(:value) {
|
||||
'Hello World'
|
||||
}
|
||||
|
||||
describe '#encrypt_aes256' do
|
||||
it 'raises a CipherError exception due to a short IV' do
|
||||
iv = SecureRandom.random_bytes(1)
|
||||
expect { Rex::Text.encrypt_aes256(iv, key, value) }.to raise_error(OpenSSL::Cipher::CipherError)
|
||||
end
|
||||
|
||||
it 'raises a CipherError exception due to a short key' do
|
||||
key = SecureRandom.random_bytes(1)
|
||||
expect { Rex::Text.encrypt_aes256(iv, key, value) }.to raise_error(OpenSSL::Cipher::CipherError)
|
||||
end
|
||||
|
||||
it 'encrypts the string Hello World' do
|
||||
encrypted_str = Rex::Text.encrypt_aes256(iv, key, value)
|
||||
expect(encrypted_str).not_to eq(value)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#decrypt_aes256' do
|
||||
it 'raises a CipherError exception due to a short IV' do
|
||||
iv = SecureRandom.random_bytes(1)
|
||||
expect { Rex::Text.decrypt_aes256(iv, key, value) }.to raise_error(OpenSSL::Cipher::CipherError)
|
||||
end
|
||||
|
||||
it 'raises a CipherError exception due to a short key' do
|
||||
key = SecureRandom.random_bytes(1)
|
||||
expect { Rex::Text.decrypt_aes256(iv, key, value) }.to raise_error(OpenSSL::Cipher::CipherError)
|
||||
end
|
||||
|
||||
it 'decrypts the value to Hello World' do
|
||||
encrypted_str = Rex::Text.encrypt_aes256(iv, key, value)
|
||||
decrypted_str = Rex::Text.decrypt_aes256(iv, key, encrypted_str)
|
||||
expect(decrypted_str).to eq(value)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
require 'spec_helper'
|
||||
require 'securerandom'
|
||||
|
||||
|
||||
describe Rex::Text do
|
||||
|
||||
describe '#rc4' do
|
||||
|
||||
let(:key) {
|
||||
SecureRandom.random_bytes(32)
|
||||
}
|
||||
|
||||
let(:value) {
|
||||
'Hello World'
|
||||
}
|
||||
|
||||
it 'encrypts a string' do
|
||||
expect(Rex::Text.rc4(key, value)).not_to eq(value)
|
||||
end
|
||||
|
||||
it 'decrypts a string' do
|
||||
encrypted_str = Rex::Text.rc4(key, value)
|
||||
decrypted_str = Rex::Text.rc4(key, encrypted_str)
|
||||
expect(decrypted_str).to eq(value)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -0,0 +1,32 @@
|
|||
require 'spec_helper'
|
||||
|
||||
|
||||
describe Rex::Text do
|
||||
|
||||
describe '#xor' do
|
||||
|
||||
let(:hello_world_str) {
|
||||
'hello world'
|
||||
}
|
||||
|
||||
let(:xor_hello_world_str) {
|
||||
"\x67\x6a\x63\x63\x60\x2f\x78\x60\x7d\x63\x6b"
|
||||
}
|
||||
|
||||
it 'XORs with an integer type key' do
|
||||
xor_key = 0x0f
|
||||
expect(Rex::Text.xor(xor_key, hello_world_str)).to eq(xor_hello_world_str)
|
||||
end
|
||||
|
||||
it 'XORs with a string type key' do
|
||||
xor_key = "0x0f"
|
||||
expect(Rex::Text.xor(xor_key, hello_world_str)).to eq(xor_hello_world_str)
|
||||
end
|
||||
|
||||
it 'raises an ArgumentError due to an out of range key' do
|
||||
bad_key = 0x1024
|
||||
expect { Rex::Text.xor(bad_key, hello_world_str) }.to raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue