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
21
22
23
24
25
|
# File 'lib/rye/key.rb', line 21
def initialize(data, name=nil)
@data = data
@name = name || 'default'
parse_data
end
|
Instance Attribute Details
#authtype ⇒ Object
Authentication type: RSA or DSA
17
18
19
|
# File 'lib/rye/key.rb', line 17
def authtype
@authtype
end
|
#keytype ⇒ Object
Key type: public or private
19
20
21
|
# File 'lib/rye/key.rb', line 19
def keytype
@keytype
end
|
#name ⇒ Object
A nickname for this key. If a path was specified this defaults to the basename.
15
16
17
|
# File 'lib/rye/key.rb', line 15
def name
@name
end
|
Class Method Details
.from_file(path) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/rye/key.rb', line 36
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
27
28
29
30
31
32
33
34
|
# File 'lib/rye/key.rb', line 27
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)
83
84
85
86
87
|
# File 'lib/rye/key.rb', line 83
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
49
50
51
52
53
|
# File 'lib/rye/key.rb', line 49
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
54
55
56
|
# File 'lib/rye/key.rb', line 54
def self.sign_aws(secret, string)
::Base64.encode64(self.sign(secret, string, "sha1")).strip
end
|
.supported_authentication?(val) ⇒ Boolean
110
111
112
|
# File 'lib/rye/key.rb', line 110
def self.supported_authentication?(val)
["RSA", "DSA"].member?(val || '')
end
|
.supported_keytype?(val) ⇒ Boolean
114
115
116
|
# File 'lib/rye/key.rb', line 114
def self.supported_keytype?(val)
["PRIVATE", "PUBLIC"].member?(val || '')
end
|
Instance Method Details
#decrypt(text) ⇒ Object
73
|
# File 'lib/rye/key.rb', line 73
def decrypt(text); @keypair.send("#{keytype.downcase}_decrypt", ::Base64.decode64(text)); end
|
#dsa? ⇒ Boolean
78
|
# File 'lib/rye/key.rb', line 78
def dsa?; @authtype.upcase == "DSA"; end
|
#dump ⇒ Object
89
90
91
92
|
# File 'lib/rye/key.rb', line 89
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
72
|
# File 'lib/rye/key.rb', line 72
def encrypt(text); ::Base64.encode64(@keypair.send("#{keytype.downcase}_encrypt", text)); end
|
#encrypted? ⇒ Boolean
79
|
# File 'lib/rye/key.rb', line 79
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">
106
107
108
|
# File 'lib/rye/key.rb', line 106
def inspect
'<%s:%s authtype="%s" keytype="%s">' % [self.class.to_s, name, @authtype, @keytype]
end
|
#private? ⇒ Boolean
75
|
# File 'lib/rye/key.rb', line 75
def private?; @keytype.upcase == "PRIVATE"; end
|
#private_key ⇒ Object
58
59
60
61
|
# File 'lib/rye/key.rb', line 58
def private_key
raise OpenSSL::PKey::PKeyError, "No private key" if public? || !@keypair
@keypair.to_s
end
|
#public? ⇒ Boolean
76
|
# File 'lib/rye/key.rb', line 76
def public?; @keytype.upcase == "PUBLIC"; end
|
#public_key ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/rye/key.rb', line 63
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
77
|
# File 'lib/rye/key.rb', line 77
def rsa?; @authtype.upcase == "RSA"; end
|
#sign(string, digesttype = "sha1") ⇒ Object
45
46
47
|
# File 'lib/rye/key.rb', line 45
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>
98
99
100
|
# File 'lib/rye/key.rb', line 98
def to_s
'<%s:%s>' % [self.class.to_s, name]
end
|