Skip to content

Commit

Permalink
corrected all rubocop errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmchan committed Oct 22, 2023
1 parent b182883 commit 1dbdb94
Showing 1 changed file with 50 additions and 32 deletions.
82 changes: 50 additions & 32 deletions spec/fabric/ec_crypto_suite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@

let(:private_key) { 'd62e76ab4a907d7634ada0d9709b4ed2bfc7c51c421127b7fc93c0141e461797' }
let(:public_key) do
'04a01f01fa942d2233a64aebe0b36c16ebdfd1c453ac5297591f20e2bfaba869e17e15f5f7367ee6f16121c64cac3ecdd517920a36f5145dc2a881ae9371873ac6'
'04a01f01fa942d2233a64aebe0b36c16ebdfd1c453ac5297591f20e2bfaba869e17e15f5f7367ee6f1' \
'6121c64cac3ecdd517920a36f5145dc2a881ae9371873ac6'
end
let(:random_secret) { 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }

describe '#initialize' do
context 'when no parameters are passed' do
it 'utilizes default options' do
it 'utilizes default key_size' do
expect(crypto_suite.key_size).to be(256)
end

it 'utilizes default digest_algorithm' do
expect(crypto_suite.digest_algorithm).to eql('SHA256')
end

it 'utilizes default curve' do
expect(crypto_suite.curve).to eql('prime256v1')
end

it 'utilizes default cipher' do
expect(crypto_suite.cipher).to eql('aes-256-cbc')
end
end
Expand All @@ -30,10 +40,19 @@
)
end

it 'utilizes options passed' do
it 'utilizes key_size option passed' do
expect(crypto_suite.key_size).to be(384)
end

it 'utilizes digest_algorithm option passed' do
expect(crypto_suite.digest_algorithm).to eql('SHA224')
end

it 'utilizes curve option passed' do
expect(crypto_suite.curve).to eql('secp384r1')
end

it 'utilizes cipher option passed' do
expect(crypto_suite.cipher).to eql('aes-128-cbc')
end
end
Expand Down Expand Up @@ -69,40 +88,33 @@
end

describe '#generate_private_key' do
it 'generates a valid EC private_key' do
it 'generates a private_key string' do
private_key = crypto_suite.generate_private_key
expect(private_key).to be_a(String)
end

public_key = crypto_suite.restore_public_key private_key

group = OpenSSL::PKey::EC::Group.new(crypto_suite.curve)

private_key_bn = OpenSSL::BN.new(private_key, 16)
public_key_bn = OpenSSL::BN.new(public_key, 16)
public_key_point = OpenSSL::PKey::EC::Point.new(group, public_key_bn)

asn1 = OpenSSL::ASN1::Sequence(
[
OpenSSL::ASN1::Integer.new(1),
OpenSSL::ASN1::OctetString(private_key_bn.to_s(2)),
OpenSSL::ASN1::ObjectId(crypto_suite.curve, 0, :EXPLICIT),
OpenSSL::ASN1::BitString(public_key_point.to_octet_string(:uncompressed), 1, :EXPLICIT)
]
)

pkey = OpenSSL::PKey::EC.new(asn1.to_der)

it 'generates a private_key' do
private_key = crypto_suite.generate_private_key
pkey = crypto_suite.pkey_from_private_key(private_key)
expect(pkey.private?).to be(true)
end

it 'matches itself' do
private_key = crypto_suite.generate_private_key
pkey = crypto_suite.pkey_from_private_key(private_key)
expect(pkey.private_key.to_s(16).downcase).to eql(private_key)
end
end

describe '#generate_csr' do
it 'generates a OpenSSL::X509::Request with the proper key' do
req = crypto_suite.generate_csr(private_key)
subject(:response_value) { crypto_suite.generate_csr(private_key) }

it 'generates a OpenSSL::X509::Request' do
expect(response_value).to be_a(OpenSSL::X509::Request)
end

expect(req).to be_a(OpenSSL::X509::Request)
expect(req.public_key.private_key.to_s(16).downcase).to eql(private_key)
it 'returns the proper value' do
expect(response_value.public_key.private_key.to_s(16).downcase).to eql(private_key)
end
end

Expand All @@ -122,7 +134,8 @@

describe '#hexdigest' do
it 'generates a hexdigest' do
expect(crypto_suite.hexdigest('hello world')).to eql('b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9')
expected_digest = 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
expect(crypto_suite.hexdigest('hello world')).to eql(expected_digest)
end
end

Expand Down Expand Up @@ -187,10 +200,14 @@
end

describe '#pkey_from_private_key' do
subject(:return_value) { crypto_suite.pkey_from_private_key(private_key) }

it 'converts a private key into a OpenSSL::PKey::EC' do
pkey = crypto_suite.pkey_from_private_key(private_key)
expect(pkey).to be_a(OpenSSL::PKey::EC)
expect(pkey.private_key).to eql(OpenSSL::BN.new(private_key, 16))
expect(return_value).to be_a(OpenSSL::PKey::EC)
end

it 'private_key is expected to equal the original private key' do
expect(return_value.private_key).to eql(OpenSSL::BN.new(private_key, 16))
end
end

Expand Down Expand Up @@ -222,7 +239,8 @@
'-----END CERTIFICATE-----'
end
let(:random_certificate_public_key) do
'04ca2509600ed223fbb645ff28b8d0f4229e3a81d8ca70607c571dd591a8c5c79f9886d68c7c76f5e588df19616e34bab2c4b186bc352ae87946b0127d40870fb4'
'04ca2509600ed223fbb645ff28b8d0f4229e3a81d8ca70607c571dd591a8c5c79f9886d' \
'68c7c76f5e588df19616e34bab2c4b186bc352ae87946b0127d40870fb4'
end

it 'returns public_key from x509 certificate' do
Expand Down

0 comments on commit 1dbdb94

Please sign in to comment.