Skip to content

Instantly share code, notes, and snippets.

@stbuehler
Created September 2, 2016 19:12
Show Gist options
  • Save stbuehler/2a2d920e4defb9a93b93cf5c0cf9cb2f to your computer and use it in GitHub Desktop.
Save stbuehler/2a2d920e4defb9a93b93cf5c0cf9cb2f to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rotp'
require 'uri'
require 'cgi'
otpuri = STDIN.readline
otpuri = URI(otpuri)
if otpuri.scheme != "otpauth"
raise "invalid uri scheme"
end
if otpuri.host != "totp"
raise "invalid uri host '#{otpuri.host}' (only TOTP supported)"
end
opts = CGI::parse(otpuri.query)
def getopt(opts, name, default)
opt = opts[name]
return default if !opt || opt.length != 1
return opt[0]
end
totp = ROTP::TOTP.new(opts['secret'][0], interval: getopt(opts, 'period', '30').to_i, digits: getopt(opts, 'digits', '6').to_i)
puts totp.now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment