Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Implemented an RSA::KeyPair#to_hash method.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Sep 6, 2010
1 parent 744ec83 commit 614e1fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/rsa/key_pair.rb
Expand Up @@ -87,6 +87,17 @@ def modulus
end
alias_method :n, :modulus

##
# Returns a hash table representation of this key pair.
#
# @example
# key_pair.to_hash #=> {:n => ..., :d => ..., :e => ...}
#
# @return [Hash]
def to_hash
{:n => modulus, :d => private_key ? private_key.exponent : nil, :e => public_key ? public_key.exponent : nil}
end

##
# Encrypts the given `plaintext` using the public key from this key
# pair.
Expand Down
14 changes: 14 additions & 0 deletions spec/key_pair_spec.rb
Expand Up @@ -61,6 +61,20 @@
end
end

context "#to_hash" do
it "returns a hash" do
@key_pair.to_hash.should be_a(Hash)
end

it "returns a hash with the correct keys" do
[:n, :d, :e].each { |key| @key_pair.to_hash.should have_key(key) }
end

it "returns a hash with the correct values" do
@key_pair.to_hash.should == {:n => @n, :d => @d, :e => @e}
end
end

context "#encrypt(Integer)" do
it "accepts an integer argument" do
lambda { @key_pair.encrypt(42) }.should_not raise_error(ArgumentError)
Expand Down

0 comments on commit 614e1fd

Please sign in to comment.