From 518c60b62b9028d3532c9631a98318b151d35fab Mon Sep 17 00:00:00 2001 From: Metin Yorulmaz Date: Fri, 8 Aug 2014 14:56:05 -0400 Subject: [PATCH] Add key pair generator --- lib/rsa_authority.rb | 6 ++++++ lib/rsa_authority/key_gen.rb | 11 +++++++++++ spec/key_gen_spec.rb | 16 ++++++++++++++++ spec/spec_helper.rb | 4 ++++ 4 files changed, 37 insertions(+) create mode 100644 lib/rsa_authority/key_gen.rb create mode 100644 spec/key_gen_spec.rb diff --git a/lib/rsa_authority.rb b/lib/rsa_authority.rb index de6b863..a069ade 100644 --- a/lib/rsa_authority.rb +++ b/lib/rsa_authority.rb @@ -1,4 +1,10 @@ +require 'openssl' +require 'base64' +require 'cgi' + require "rsa_authority/version" +require "rsa_authority/key_gen" + module RSAAuthority # Your code goes here... diff --git a/lib/rsa_authority/key_gen.rb b/lib/rsa_authority/key_gen.rb new file mode 100644 index 0000000..ce3a580 --- /dev/null +++ b/lib/rsa_authority/key_gen.rb @@ -0,0 +1,11 @@ +module RSAAuthority + class KeyGen + def self.generate(key_name) + rsa_private_key = OpenSSL::PKey::RSA.generate(2048) + + File.open("#{key_name}.pem", "w") { |io| io.write(rsa_private_key.to_s) } + + File.open("#{key_name}.pub", "w") { |io| io.write(rsa_private_key.public_key) } + end + end +end diff --git a/spec/key_gen_spec.rb b/spec/key_gen_spec.rb new file mode 100644 index 0000000..9ae2a3a --- /dev/null +++ b/spec/key_gen_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe RSAAuthority::KeyGen do + let(:test_key_name) { 'example_key' } + let(:public_key) { "#{test_key_name}.pub" } + let(:private_key) { "#{test_key_name}.pem" } + + after { File.delete public_key, private_key } + + it "generates public and privete key pair" do + RSAAuthority::KeyGen.generate(test_key_name) + + expect(File.exists?(public_key)).to be(true) + expect(File.exists?(private_key)).to be(true) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 89642b2..5321738 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,7 @@ +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) + +require 'rsa_authority' + # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause this