Created
November 30, 2011 02:18
-
-
Save zszugyi/1407691 to your computer and use it in GitHub Desktop.
class to convert the public key part of an RSA key to the .NET XML format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'openssl' | |
| require 'base64' | |
| class RSAKeyToXmlConverter | |
| def initialize rsa_key_string | |
| @key = OpenSSL::PKey::RSA.new(rsa_key_string) | |
| end | |
| def to_public_key | |
| <<XML_KEY | |
| <RSAKeyValue> | |
| <Modulus> | |
| #{to_base_64(@key.n)} | |
| </Modulus> | |
| <Exponent> | |
| #{to_base_64(@key.e)} | |
| </Exponent> | |
| </RSAKeyValue> | |
| XML_KEY | |
| end | |
| private | |
| def to_base_64 num | |
| hexstring = num.to_s(16).downcase | |
| Base64.encode64([hexstring].pack('H*')).split("\n").join | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment