summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoro <o@immerda.ch>2011-06-26 03:37:51 +0200
committero <o@immerda.ch>2011-06-26 03:37:51 +0200
commit288bfcaa4d115da073650b0ea69dd24b1cc60cd7 (patch)
tree5ab76f021e224166038c2d10678051af3bb46714
parent464d394a6f1b2fe180364e880badd49c5105e458 (diff)
added some crypto algo objects
-rw-r--r--lib/fpg/_helpers/class.rb5
-rw-r--r--lib/fpg/_helpers/tagged_subclasses.rb14
-rw-r--r--lib/fpg/parse/public_key_encrypted_session_key.rb2
-rw-r--r--lib/fpg/public_key_algos/algo.rb7
-rw-r--r--lib/fpg/public_key_algos/rsa.rb9
-rw-r--r--lib/fpg/public_key_algos/rsa_encryption_only.rb9
-rw-r--r--lib/fpg/public_key_algos/rsa_sign_only.rb9
7 files changed, 54 insertions, 1 deletions
diff --git a/lib/fpg/_helpers/class.rb b/lib/fpg/_helpers/class.rb
new file mode 100644
index 0000000..b952540
--- /dev/null
+++ b/lib/fpg/_helpers/class.rb
@@ -0,0 +1,5 @@
+class Class
+ def subclasses
+ ObjectSpace.each_object(Class).select { |klass| klass < self }
+ end
+end
diff --git a/lib/fpg/_helpers/tagged_subclasses.rb b/lib/fpg/_helpers/tagged_subclasses.rb
new file mode 100644
index 0000000..fb9edf0
--- /dev/null
+++ b/lib/fpg/_helpers/tagged_subclasses.rb
@@ -0,0 +1,14 @@
+module FPG
+ module TaggedSubclasses
+ def self.included(base)
+ base.extend FindSubclasses
+ end
+ module FindSubclasses
+ def with_tag(tag)
+ klass = subclasses.detect{ |klass| klass.tag == tag }
+ raise "did not find a subclass for tag: #{tag}" if klass.nil?
+ klass.new
+ end
+ end
+ end
+end
diff --git a/lib/fpg/parse/public_key_encrypted_session_key.rb b/lib/fpg/parse/public_key_encrypted_session_key.rb
index dff5861..cf30f9d 100644
--- a/lib/fpg/parse/public_key_encrypted_session_key.rb
+++ b/lib/fpg/parse/public_key_encrypted_session_key.rb
@@ -15,7 +15,7 @@ module FPG
self.key_id= stream.read(8)
end
def parse_public_key_algo
- self.encryption_algo= stream.getbyte
+ self.encryption_algo= PublicKeyAlgos::Algo.with_tag(stream.getbyte)
end
def parse_session_key
self.session_key= stream.read(byte_size-10)
diff --git a/lib/fpg/public_key_algos/algo.rb b/lib/fpg/public_key_algos/algo.rb
new file mode 100644
index 0000000..03d28d0
--- /dev/null
+++ b/lib/fpg/public_key_algos/algo.rb
@@ -0,0 +1,7 @@
+module FPG
+ module PublicKeyAlgos
+ class Algo
+ include TaggedSubclasses
+ end
+ end
+end
diff --git a/lib/fpg/public_key_algos/rsa.rb b/lib/fpg/public_key_algos/rsa.rb
new file mode 100644
index 0000000..4234dd1
--- /dev/null
+++ b/lib/fpg/public_key_algos/rsa.rb
@@ -0,0 +1,9 @@
+module FPG
+ module PublicKeyAlgos
+ class Rsa < Algo
+ def self.tag
+ 1
+ end
+ end
+ end
+end
diff --git a/lib/fpg/public_key_algos/rsa_encryption_only.rb b/lib/fpg/public_key_algos/rsa_encryption_only.rb
new file mode 100644
index 0000000..56bc2f1
--- /dev/null
+++ b/lib/fpg/public_key_algos/rsa_encryption_only.rb
@@ -0,0 +1,9 @@
+module FPG
+ module PublicKeyAlgos
+ class RsaEncryptionOnly < Rsa
+ def self.tag
+ 2
+ end
+ end
+ end
+end
diff --git a/lib/fpg/public_key_algos/rsa_sign_only.rb b/lib/fpg/public_key_algos/rsa_sign_only.rb
new file mode 100644
index 0000000..d9ad97a
--- /dev/null
+++ b/lib/fpg/public_key_algos/rsa_sign_only.rb
@@ -0,0 +1,9 @@
+module FPG
+ module PublicKeyAlgos
+ class RsaSignOnly < Rsa
+ def self.tag
+ 3
+ end
+ end
+ end
+end