Ruby Token Generator Code

Example Ruby code to generate an API Token

require 'openssl'

def generate_token(secret, ip, timestamp)
        tokenString = secret + ":" + ip + ":" + timestamp
        digest = OpenSSL::Digest.new('md5')
        token = OpenSSL::HMAC.hexdigest(digest, secret, tokenString)
        
end

secret = 'testsecret'
timestamp = Time.now.strftime("%s000")
ip = '192.168.1.1'

puts generate_token(secret, ip, timestamp)

Example

Running this code with the timestamp 1424082443000 yields the value:

092268a2d33f46bf990676efdab34d2d

Last updated

Was this helpful?