Skip to content

Commit

Permalink
Extract RSA Keypair generation
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Sep 14, 2011
1 parent 6bc427b commit 4c169c2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
22 changes: 5 additions & 17 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,8 @@ def generate_rsa_pair
end

# Retrieves a valid RSA::KeyPair for the User's private key
def retrieve_private_key
# Create the private key from the key stored

# Retrieve the exponent and modulus from the key string
private_key.match /^RSA\.(.*?)\.(.*)$/
modulus = Base64::urlsafe_decode64($1)
exponent = Base64::urlsafe_decode64($2)

modulus = modulus.bytes.inject(0) {|num, byte| (num << 8) | byte }
exponent = exponent.bytes.inject(0) { |num, byte| (num << 8) | byte }

# Create the public key instance
key = RSA::Key.new(modulus, exponent)
keypair = RSA::KeyPair.new(key, nil)
def self.to_rsa_key
Crypto.make_rsa_key(nil, private_key)
end

# After a user is created, create the feed and reset the token
Expand Down Expand Up @@ -181,7 +169,7 @@ def send_follow_notification to_feed_id

salmon = OStatus::Salmon.from_follow(author.to_atom, f.author.to_atom)

envelope = salmon.to_xml retrieve_private_key
envelope = salmon.to_xml self.to_rsa_key

# Send envelope to Author's Salmon endpoint
uri = URI.parse(f.author.salmon_url)
Expand Down Expand Up @@ -209,7 +197,7 @@ def send_unfollow_notification to_feed_id

salmon = OStatus::Salmon.from_unfollow(author.to_atom, f.author.to_atom)

envelope = salmon.to_xml retrieve_private_key
envelope = salmon.to_xml self.to_rsa_key

# Send envelope to Author's Salmon endpoint
uri = URI.parse(f.author.salmon_url)
Expand All @@ -225,7 +213,7 @@ def send_mention_notification update_id, to_feed_id
base_uri = "http://#{author.domain}/"
salmon = OStatus::Salmon.new(u.to_atom(base_uri))

envelope = salmon.to_xml retrieve_private_key
envelope = salmon.to_xml self.to_rsa_key

# Send envelope to Author's Salmon endpoint
uri = URI.parse(f.author.salmon_url)
Expand Down
16 changes: 16 additions & 0 deletions lib/crypto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ def self.generate_keypair

keypair
end

# We don't yet do anything with the public key, but I added it so that when we
# need to, it'll be there.
def self.make_rsa_key(public_key, private_key)
# Retrieve the exponent and modulus from the key string
private_key.match /^RSA\.(.*?)\.(.*)$/
modulus = Base64::urlsafe_decode64($1)
exponent = Base64::urlsafe_decode64($2)

modulus = modulus.bytes.inject(0) {|num, byte| (num << 8) | byte }
exponent = exponent.bytes.inject(0) { |num, byte| (num << 8) | byte }

# Create the public key instance
key = RSA::Key.new(modulus, exponent)
RSA::KeyPair.new(key, nil)
end
end

0 comments on commit 4c169c2

Please sign in to comment.