Authentication-Code-Examples

Jump to: navigation, search

Calculating the Authentication Signature

Here are some code examples you can use to get started.

Calculating the Signature in C#

Here's a C# example you can start with:
protected void someMethod()
{
    //get the timestamp value
    string timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds.ToString();

    //grab just the integer portion
    timestamp = timestamp.Substring(0, timestamp.IndexOf("."));

    //set the API key (note that this is not a valid key!
    string apikey = "ye9s7rzjp9qe9hdzp6cks995";

    //set the shared secret key
    string secret = "78FRfJT7Qs";

    //call the function to create the hash
        string sig = CreateMD5Hash(apikey + secret + timestamp);
}

//note, requires "using System.Security.Cryptography;"
protected string CreateMD5Hash(string input)
{
    // Use input string to calculate MD5 hash
        MD5 md5 = MD5.Create();
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        byte[] hashBytes = md5.ComputeHash(inputBytes);

        // Convert the byte array to hexadecimal string
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("x2"));  //this will use lowercase letters, use "X2" instead of "x2" to get uppercase
        }
        return sb.ToString();
}

Calculating the Signature in PHP

Here's a PHP example you can start with:
class SigGen
{
  public static function getTimeStamp()
  {
    return time();
  }

  public static function getSharedSecret()
  {
    return "78FRfJT7Qs";
  }

  public static function getAPIKey()
  {
    return "ye9s7rzjp9qe9hdzp6cks995";
  }

  public static function createMD5Hash()
  {
    $string = self::getAPIKey() . self::getSharedSecret() . self::getTimeStamp();
    $md = md5($string);
    return $md;
  }

  public static function getResult()
  {
    $url = "http://api.rovicorp.com/data/v1/album/images?albumid=MW0000111184&apikey=";
    $url = $url .  self::getAPIKey() . "&sig=" . self::createMD5Hash();
    $contents = file_get_contents($url);  
    return $contents;
  }
}

echo SigGen::getResult()."\n";

Calculating the Signature in Ruby

Here's a Ruby example you can start with:
require 'digest/md5'
api_key = 'valid_apikey'
shared_secret = 'valid_secret_key'
timestamp = Time.now.to_i.to_s
sig = Digest::MD5.hexdigest(api_key + shared_secret + timestamp)

echo SigGen::getResult()."\n";

Calculating the Signature in Python

Here's a Python example you can start with:
# python class used to interface with AMG API

import requests # http://pypi.python.org/pypi/requests
import time
import hashlib
import urllib

class AllMusicGuide(object):
    api_url = 'http://api.rovicorp.com/data/v1'

    key = 'YOUR_KEY_HERE'
    secret = 'YOUR_SECRET_HERE'

    def _sig(self):
        timestamp = int(time.time())

        m = hashlib.md5()
        m.update(self.key)
        m.update(self.secret)
        m.update(str(timestamp))

        return m.hexdigest()

    def get(self, resource, params=None):
        """Take a dict of params, and return what we get from the api"""

        if not params:
            params = {}

        params = urllib.urlencode(params)

        sig = self._sig()

        url = "%s/%s?apikey=%s&sig=%s&%s" % (self.api_url, resource, self.key, sig, params)

        resp = requests.get(url)

        if resp.status_code != 200:
            # THROW APPROPRIATE ERROR
            pass

        return resp.content

Calculating the Signature in Javascript

This requires an MD5 library, available at http://developer.rovicorp.com/files/md5_2.js. Here's a Javascript example you can start with:
function genSig() {
    var apikey = "valid_apikey";
    var secret = "valid_secret";
    var curdate = new Date();
    var gmtstring = curdate.toGMTString();
    var utc = Date.parse(gmtstring) / 1000;
    return = hex_md5(apikey + secret + utc);
}

Calculating the Signature in Groovy in Soap UI

This requires project-level custom properties for apikey, secret, and sig. Here's a Groovy example you can start with:
import java.security.MessageDigest;
def zeroEpoch = Calendar.getInstance(TimeZone.getTimeZone('GMT'));
def timestamp =  (int)(zeroEpoch.getTimeInMillis() / 1000);

def key = context.expand('${#Project#apikey}');
def sec = context.expand('${#Project#secret}');
def sigText =  key + sec + timestamp;
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(sigText.getBytes("UTF-8"));
BigInteger hash = new BigInteger(1, md5.digest());
return hash.toString(16);

↑ Top

Personal tools