Class: Rye::Key
- Inherits:
-
Object
show all
- Defined in:
- lib/rye/key.rb
Defined Under Namespace
Classes: BadFile, BadPerm
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data, name = nil) ⇒ Key
Returns a new instance of Key.
20
21
22
23
24
|
# File 'lib/rye/key.rb', line 20
def initialize(data, name=nil)
@data = data
@name = name || 'default'
parse_data
end
|
Instance Attribute Details
#authtype ⇒ Object
Authentication type: RSA or DSA
16
17
18
|
# File 'lib/rye/key.rb', line 16
def authtype
@authtype
end
|
#keytype ⇒ Object
Key type: public or private
18
19
20
|
# File 'lib/rye/key.rb', line 18
def keytype
@keytype
end
|
#name ⇒ Object
A nickname for this key. If a path was specified this defaults to the basename.
14
15
16
|
# File 'lib/rye/key.rb', line 14
def name
@name
end
|
Class Method Details
.from_file(path) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/rye/key.rb', line 35
def self.from_file(path)
raise BadFile, path unless File.exists?(path || '')
pkey = self.new File.read(path), File.basename(path)
file_perms = (File.stat(path).mode & 600)
raise BadPerm, path if file_perms != 0 && pkey.private?
pkey
end
|
.generate_pkey(authtype = "RSA", bits = 1024) ⇒ Object
26
27
28
29
30
31
32
33
|
# File 'lib/rye/key.rb', line 26
def self.generate_pkey(authtype="RSA", bits=1024)
unless Rye::Key.supported_authentication?(authtype)
raise OpenSSL::PKey::PKeyError, "Unknown authentication: #{authttype}"
end
bits &&= bits.to_i
klass = authtype.upcase == "RSA" ? OpenSSL::PKey::RSA : OpenSSL::PKey::DSA
pk = klass.new(bits)
end
|
.public_key_to_ssh2(pubkey) ⇒ Object
Returns a public key in SSH format (suitable for ~/.ssh/authorized_keys)
82
83
84
85
86
|
# File 'lib/rye/key.rb', line 82
def self.public_key_to_ssh2(pubkey)
authtype = pubkey.class.to_s.split('::').last.downcase
b64pub = ::Base64.encode64(pubkey.to_blob).strip.gsub(/[\r\n]/, '')
"ssh-%s %s" % [authtype, b64pub] end
|
.sign(secret, string, digesttype = "sha1") ⇒ Object
48
49
50
51
52
|
# File 'lib/rye/key.rb', line 48
def self.sign(secret, string, digesttype="sha1")
@@digest ||= {}
@@digest[digest] ||= OpenSSL::Digest::Digest.new(digesttype)
sig = OpenSSL::HMAC.hexdigest(@@digest[digest], secret, string).strip
end
|
.sign_aws(secret, string) ⇒ Object
53
54
55
|
# File 'lib/rye/key.rb', line 53
def self.sign_aws(secret, string)
::Base64.encode64(self.sign(secret, string, "sha1")).strip
end
|
.supported_authentication?(val) ⇒ Boolean
109
110
111
|
# File 'lib/rye/key.rb', line 109
def self.supported_authentication?(val)
["RSA", "DSA"].member?(val || '')
end
|
.supported_keytype?(val) ⇒ Boolean
113
114
115
|
# File 'lib/rye/key.rb', line 113
def self.supported_keytype?(val)
["PRIVATE", "PUBLIC"].member?(val || '')
end
|
Instance Method Details
#decrypt(text) ⇒ Object
72
|
# File 'lib/rye/key.rb', line 72
def decrypt(text); @keypair.send("#{keytype.downcase}_decrypt", ::Base64.decode64(text)); end
|
#dsa? ⇒ Boolean
77
|
# File 'lib/rye/key.rb', line 77
def dsa?; @authtype.upcase == "DSA"; end
|
#dump ⇒ Object
88
89
90
91
|
# File 'lib/rye/key.rb', line 88
def dump
puts @keypair.public_key.to_text
puts @keypair.public_key.to_pem
end
|
#encrypt(text) ⇒ Object
Encrypt text
with this public or private key. The key must
71
|
# File 'lib/rye/key.rb', line 71
def encrypt(text); ::Base64.encode64(@keypair.send("#{keytype.downcase}_encrypt", text)); end
|
#encrypted? ⇒ Boolean
78
|
# File 'lib/rye/key.rb', line 78
def encrypted?; @data && @data.match(/ENCRYPTED/); end
|
#inspect ⇒ Object
Reveals some metadata about the key. Does not print the key.
<Rye::Key:id_rsa.pub authtype="RSA" keytype="PRIVATE">
105
106
107
|
# File 'lib/rye/key.rb', line 105
def inspect
'<%s:%s authtype="%s" keytype="%s">' % [self.class.to_s, name, @authtype, @keytype]
end
|
#private? ⇒ Boolean
74
|
# File 'lib/rye/key.rb', line 74
def private?; @keytype.upcase == "PRIVATE"; end
|
#private_key ⇒ Object
57
58
59
60
|
# File 'lib/rye/key.rb', line 57
def private_key
raise OpenSSL::PKey::PKeyError, "No private key" if public? || !@keypair
@keypair.to_s
end
|
#public? ⇒ Boolean
75
|
# File 'lib/rye/key.rb', line 75
def public?; @keytype.upcase == "PUBLIC"; end
|
#public_key ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/rye/key.rb', line 62
def public_key
raise OpenSSL::PKey::PKeyError, "No public key" if !@keypair
pubkey = public? ? @keypair : @keypair.public_key
def pubkey.to_ssh2; Rye::Key.public_key_to_ssh2(self); end
pubkey
end
|
#rsa? ⇒ Boolean
76
|
# File 'lib/rye/key.rb', line 76
def rsa?; @authtype.upcase == "RSA"; end
|
#sign(string, digesttype = "sha1") ⇒ Object
44
45
46
|
# File 'lib/rye/key.rb', line 44
def sign(string, digesttype="sha1")
Rye::Key.sign(@keypair.to_s, string, digesttype)
end
|
#to_s ⇒ Object
Reveals the key basename. Does not print the key.
<Rye::Key:id_rsa.pub>
97
98
99
|
# File 'lib/rye/key.rb', line 97
def to_s
'<%s:%s>' % [self.class.to_s, name]
end
|