Skip to content

Instantly share code, notes, and snippets.

@zhum
Created June 26, 2013 06:06
Show Gist options
  • Save zhum/5865085 to your computer and use it in GitHub Desktop.
Save zhum/5865085 to your computer and use it in GitHub Desktop.
ruby class for openssh keys checking.
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