Created
June 26, 2013 06:06
-
-
Save zhum/5865085 to your computer and use it in GitHub Desktop.
ruby class for openssh keys checking.
This file contains 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
class OpenSSHKey | |
def self.valid? key | |
string=key.to_s | |
oneliner = string.lines.count>1 ? to_openssh(string) : string | |
tmpfile="/tmp/openssh-check-#{rand(1000000).to_i}" | |
File.open(tmpfile, "w") { |file| file.puts oneliner} | |
begin | |
system "ssh-keygen -l -f '#{tmpfile}' >/dev/null 2>/dev/null" | |
ensure | |
File.unlink tmpfile | |
end | |
end | |
def self.to_openssh key | |
string=key.to_s | |
return string if string.lines.count==1 | |
tmpfile="/tmp/openssh-check-#{rand(1000000).to_i}" | |
File.open(tmpfile, "w") { |file| file.puts string} | |
result=`ssh-keygen -i -f '#{tmpfile}' 2>/dev/null` | |
begin | |
$?.exitstatus==0 ? | |
result | |
: | |
nil | |
ensure | |
File.unlink tmpfile | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment