require 'rubygems' require 'activesupport' require 'digest/md5' class Signature PASSWORD = 'YOUR_SECRET_PASSWORD_HERE' # use this password for nginx directive "secure_link_md5" DIR = 'flv' # the internal nginx location to access to media files SERVER_NAME = 'http://your_server.com:8080' # nginx listen address and port attr_reader :expiration_time, :file_name, :md5 def initialize(file_name) @expiration_time = (Time.now + 2.hours).to_i @file_name = file_name md5_calculate end def generate # nginx secured URL to access to media files, "video" is the location, not real directory on filesystem "#{SERVER_NAME}/video/#{@md5}/#{expiration_time}/#{file_name}" end private def md5_calculate s = "#{PASSWORD}#{@expiration_time}/#{DIR}/#{@file_name}" a = Base64.encode64(Digest::MD5.digest(s)) @md5 = a.tr("+/", "-_").sub('==', '').chomp end end url = Signature.new('video1.flv') puts url.expiration_time #=> 1326559618 puts url.md5 #=> HLz1px_YzSNcbcaskzA6nQ puts url.generate #=> http://your_server.com:8080/video/HLz1px_YzSNcbcaskzA6nQ/1326559618/video1.flv