Skip to content

Commit

Permalink
Added general crypto class and extracted KeyPair
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Sep 14, 2011
1 parent 3ca3249 commit 749ac09
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 45 deletions.
50 changes: 5 additions & 45 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# that sign up via Twitter get a User model, though it's a bit empty in that
# particular case.

require_relative '../../lib/crypto'

class User
require 'digest/md5'
require 'openssl'
require 'rsa'

include MongoMapper::Document

Expand Down Expand Up @@ -51,52 +51,12 @@ def updates

# Before a user is created, we will generate some RSA keys
def generate_rsa_pair
key = RSA::KeyPair.generate(2048)

public_key = key.public_key
m = public_key.modulus
e = public_key.exponent

modulus = ""
until m == 0 do
modulus << [m % 256].pack("C")
m >>= 8
end
modulus.reverse!

exponent = ""
until e == 0 do
exponent << [e % 256].pack("C")
e >>= 8
end
exponent.reverse!

public_key = "RSA.#{Base64::urlsafe_encode64(modulus)}.#{Base64::urlsafe_encode64(exponent)}"

tmp_private_key = key.private_key
m = tmp_private_key.modulus
e = tmp_private_key.exponent

modulus = ""
until m == 0 do
modulus << [m % 256].pack("C")
m >>= 8
end
modulus.reverse!

exponent = ""
until e == 0 do
exponent << [e % 256].pack("C")
e >>= 8
end
exponent.reverse!

tmp_private_key = "RSA.#{Base64::urlsafe_encode64(modulus)}.#{Base64::urlsafe_encode64(exponent)}"
keypair = Crypto.generate_keypair

self.author.public_key = public_key
self.author.public_key = keypair.public_key
self.author.save

self.private_key = tmp_private_key
self.private_key = keypair.private_key
end

# Retrieves a valid RSA::KeyPair for the User's private key
Expand Down
54 changes: 54 additions & 0 deletions lib/crypto.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'openssl'
require 'rsa'

KeyPair = Struct.new(:public_key, :private_key)

class Crypto
def self.generate_keypair
keypair = KeyPair.new

key = RSA::KeyPair.generate(2048)

public_key = key.public_key
m = public_key.modulus
e = public_key.exponent

modulus = ""
until m == 0 do
modulus << [m % 256].pack("C")
m >>= 8
end
modulus.reverse!

exponent = ""
until e == 0 do
exponent << [e % 256].pack("C")
e >>= 8
end
exponent.reverse!

keypair.public_key = "RSA.#{Base64::urlsafe_encode64(modulus)}.#{Base64::urlsafe_encode64(exponent)}"

tmp_private_key = key.private_key
m = tmp_private_key.modulus
e = tmp_private_key.exponent

modulus = ""
until m == 0 do
modulus << [m % 256].pack("C")
m >>= 8
end
modulus.reverse!

exponent = ""
until e == 0 do
exponent << [e % 256].pack("C")
e >>= 8
end
exponent.reverse!

keypair.private_key = "RSA.#{Base64::urlsafe_encode64(modulus)}.#{Base64::urlsafe_encode64(exponent)}"

keypair
end
end

0 comments on commit 749ac09

Please sign in to comment.