Skip to content

Instantly share code, notes, and snippets.

@cheald

cheald/auth.rb Secret

Created November 12, 2014 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cheald/7cbbe18ce101fdc35ebc to your computer and use it in GitHub Desktop.
Save cheald/7cbbe18ce101fdc35ebc to your computer and use it in GitHub Desktop.
require 'openssl'
def password(secret, offset = 0, digits = 6)
digest = OpenSSL::HMAC.digest("sha1", [secret].pack("H*"), [(Time.now.to_i - offset) / 30].pack("xxxxN"))
(digest[digest[19].to_i & 0x0F, 4].unpack("N").first & 0x7FFFFFFF) % (10 ** digits)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment