Page 1 of 1

PHP Equivalent to the Ruby EzCrypto::Key.with_password

Posted: Fri Feb 03, 2012 1:17 am
by mchaggis
Hi,

Not sure if anyone will be able to help me, bu I'm hoping there may be a least a couple of people who know Ruby (I must confess I never really be tempted)

I am trying to write a bit of code that will allow me to allow my users to automatically get logged into a 3rd parties site. The great thing is that they all for "Single Sign-On", essentially the automated logging in of a user account. The bad thing is that they only have code examples for Ruby and .NET :crazy:

The doumentation is at: http://docs.bookingbug.com/display/API/Single_Sign_On

The Ruby code is:

Code: Select all

require 'rubygems'require 'rubygems'
require 'ezcrypto'
require 'json'
require 'cgi'
require 'base64'

class ApiToken
  attr_accessor :data

  BOOKINGBUG_AFFILIATE_ID = "xxxxxxxx"
  BOOKINGBUG_API_KEY = "xxxxxxxxxxxxxxxxxxxx"

  def initialize(options = {})
    options.merge!({:expires => (Time.now + 60 * 60).to_s})

    key = EzCrypto::Key.with_password BOOKINGBUG_AFFILIATE_ID, BOOKINGBUG_API_KEY
    encrypted = key.encrypt(options.to_json)
    @data = Base64.encode64(encrypted).gsub(/\n/,'')
    @data = CGI.escape(@data)
  end

  def to_s
    @data
  end

  def token
    "sso=#{@data}&affiliate_id=#{BOOKINGBUG_AFFILIATE_ID}"
  end

end
The important bit that I need to translate to PHP is

Code: Select all

    key = EzCrypto::Key.with_password BOOKINGBUG_AFFILIATE_ID, BOOKINGBUG_API_KEY
    encrypted = key.encrypt(options.to_json)
I have worked out that the following nearly there:

Code: Select all

$BOOKINGBUG_AFFILIATE_ID = 'xxxxxxxx';
$BOOKINGBUG_API_KEY = 'xxxxxxxxxxxxxxxxxxxx';
$options = array(
        'user' => 'username',
        'password' => 'password',
        'expires' => (mktime() + 60 * 60),
        'redirect' => $_SERVER['REQUEST_URI']
);
// $key = EzCrypto::Key.with_password( $BOOKINGBUG_AFFILIATE_ID, $BOOKINGBUG_API_KEY )
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, json_encode($options), MCRYPT_MODE_CBC);
What I am struggling with, is what the commented out line should be. I.e. An equivalent function to EzCrypto::Key.with_password.

I'm assuming it's some form of hash, maybe md5, but after searching the web I have not been able to find the missing link. I have targeted conversion on the Ruby code as it seems less complex compared to the example .NET code that they give.

Any help, much appreciated!