Skip to content

Instantly share code, notes, and snippets.

@waynehoover
Last active October 11, 2016 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynehoover/6577723 to your computer and use it in GitHub Desktop.
Save waynehoover/6577723 to your computer and use it in GitHub Desktop.
nginx rails secure_link example
class Order < ActiveRecord::Base
DOWNLOAD_EXPIRY = 10.days
def secure_download_link
time = (Time.now + DOWNLOAD_EXPIRY).to_i
hmac = Digest::MD5.base64digest("#{time}#{self.file_path} #{ENV['DOWNLOAD_SECRET']}").tr("+/","-_").gsub("==",'')
"http://files.domain.com/downloads/#{file_path}?md5=#{hmac}&expires=#{time}"
end
...
end
@sekrett
Copy link

sekrett commented Oct 11, 2016

.gsub("==",'') could be replaced with .tr('=', '')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment