Skip to content

Commit

Permalink
Merge branch 'CL-140'
Browse files Browse the repository at this point in the history
  • Loading branch information
geremy cohen committed Sep 4, 2012
2 parents daa59c5 + 4efff43 commit f8aa33f
Show file tree
Hide file tree
Showing 21 changed files with 975 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
508 changes: 508 additions & 0 deletions php/3.3/Pubnub.php

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions php/3.3/PubnubAES.php
@@ -0,0 +1,94 @@
<?php

#$plaintext = "this is my plaintext.";
$cipher_key = "enigma";

#printf("\ncipher key is %s\n", $cipher_key);

#$cipher_text = "q/xJqqN6qbiZMXYmiQC1Fw==";
#$decrypt = "RVOElAJIHskATgCCP+KlaQ==";
#$key = "67a4f45f0d1d9bc606486fc42dc49416";
#$iv = "0123456789012345";
## Manual Run
#$cipher_text = encrypt("hellohellohello!", $cipher_key, $iv);
#$p_text = decrypt($cipher_text, $cipher_key, $iv);
##

function decrypt($cipher_text, $cipher_key) {
$iv = "0123456789012345";

if (gettype($cipher_text) != "string")
return "DECRYPTION_ERROR";

$decoded = base64_decode($cipher_text);

$sha_cipher_key = hash("sha256", $cipher_key);
$padded_cipher_key = substr($sha_cipher_key, 0, 32);

$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $padded_cipher_key, $iv);

$decrypted = mdecrypt_generic($td, $decoded); // TODO: handle non-encrypted unicode corner-case
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

$unpadded = unpadPKCS7($decrypted, 16);
#printf("\ndecoded: %s", $unpadded);
return $unpadded;
}


function encrypt($plain_text, $cipher_key) {
$iv = "0123456789012345";

$sha_cipher_key = hash("sha256", $cipher_key);
$padded_cipher_key = substr($sha_cipher_key, 0, 32);
$padded_plain_text = pkcs5_pad($plain_text, 16);

// printf("sha256 key is %s\n", $sha_cipher_key);
// printf("padded cipher key is %s\n\n", $padded_cipher_key);
// printf("padded plain_text is %s\n\n", $padded_plain_text);

# This is the way to do AES-256 using mcrypt PHP - its not AES-128 or anything other than that!
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $padded_cipher_key, $iv);
$encrypted = mcrypt_generic($td, $padded_plain_text);
$encode = base64_encode($encrypted);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
#printf("\nencoded: %s", $encode);
return $encode;
}

function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}

function unpadPKCS7($data, $blockSize)
{
$length = strlen($data);
if ($length > 0) {
$first = substr($data, -1);

if (ord($first) <= $blockSize) {
for ($i = $length - 2; $i > 0; $i--)
if (ord($data [$i] != $first))
break;

return substr($data, 0, $i+1);
}
}
return $data;
}

function isBlank($word)
{
if (($word == null) || ($word == false))
return true;
else
return false;
}

?>
32 changes: 32 additions & 0 deletions php/3.3/pubnub.com.pem
@@ -0,0 +1,32 @@
-----BEGIN CERTIFICATE-----
MIIFgDCCBGigAwIBAgIHTpwnck1dVjANBgkqhkiG9w0BAQUFADCB3DELMAkGA1UE
BhMCVVMxEDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxJTAj
BgNVBAoTHFN0YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4xOTA3BgNVBAsTMGh0
dHA6Ly9jZXJ0aWZpY2F0ZXMuc3RhcmZpZWxkdGVjaC5jb20vcmVwb3NpdG9yeTEx
MC8GA1UEAxMoU3RhcmZpZWxkIFNlY3VyZSBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0
eTERMA8GA1UEBRMIMTA2ODg0MzUwHhcNMTExMTAyMjAwMjMzWhcNMTIxMTAyMTgz
MzM1WjBRMRUwEwYDVQQKFAwqLnB1Ym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vbYIKcHVibnViLmNv
bTAdBgNVHQ4EFgQU1LjhCRlC4sKfFRaCg+uhNjOz3u8wDQYJKoZIhvcNAQEFBQAD
ggEBAFZWrYmY4JqP+//8v/3/UztMpZWWlivZ44wpbxNIeQEnHVxpzjfG+eUZkZpV
1e3DG3pxDdehwNE3YIbnkCku0SaqOT4sU6AjfaLV3tHvvqFwBGQQqdAkuC+C1O1D
ynHq2vyPvsFu5jDDSOQ4IMgWSznay7b1bzp55NxPg16D3i9p+cy6nYUvHsjqjLn9
8qiWwFWYaNju5CYr+fIl5sR3AP+8Qd+IYDpx+icMt42yOgN1mBauMj0bMV3p9D9B
egQH1kN29mb65Q52t2wDooIqiTPrmMGwr+59SmCrchzMWFT/8wakPSo4aK2/Z5f1
mslRKVjaP39pSqkEdD2S1VvR4x0=
-----END CERTIFICATE-----
176 changes: 176 additions & 0 deletions php/3.3/pubnubCipherTextTests.php
@@ -0,0 +1,176 @@
<?php

require_once('Pubnub.php');

##
# php ./pubnubPlaintextTests.php
# php ./pubnubPlaintextTests.php [PUB-KEY] [SUB-KEY] [SECRET-KEY] [CIPHER-KEY] [USE SSL]

## Capture Publish and Subscribe Keys from Command Line


// TODO: Need SSL tests

$publish_key = 'demo';
$subscribe_key = 'demo';
$secret_key = false;
$cipher_key = "enigma";
$ssl_on = false;

$plain_text = "yay!";
$cipher_text = "q/xJqqN6qbiZMXYmiQC1Fw==";
$cipher_key = "enigma";

## Encryption Test
if (decrypt($cipher_text, $cipher_key) == $plain_text) {
echo "Standard encryption test PASS.\n\n";
} else
echo "Standard encryption test FAIL.\n\n";

## Decryption Test
if (encrypt($plain_text, $cipher_key) == $cipher_text) {
echo "Standard decryption test PASS.\n\n";
} else
echo "Standard decryption test FAIL.\n\n";


## ---------------------------------------------------------------------------
## Create Pubnub Object
## ---------------------------------------------------------------------------
$pubnub = new Pubnub( $publish_key, $subscribe_key, $secret_key, $cipher_key, $ssl_on );

## ---------------------------------------------------------------------------
## Define Messaging Channel
## ---------------------------------------------------------------------------
$channel = "hello_world";

## ---------------------------------------------------------------------------
## Publish Example
## ---------------------------------------------------------------------------
echo "Running publish\r\n";
$publish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => 'Pubnub Messaging API 1'
));
echo($publish_success[0] . $publish_success[1]);
echo "\r\n";

$publish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => 'Pubnub Messaging API 2'
));
echo($publish_success[0] . $publish_success[1]);
echo "\r\n";



$publish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => '漢語'
));
echo($publish_success[0] . $publish_success[1]);
echo "\r\n";

// Publish an associative array

$big_array = array();
$big_array["this stuff"]["can get"] = "complicated!";

$publish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => $big_array
));

echo($publish_success[0] . $publish_success[1]);
echo "\r\n";

// Publish an empty array
$emptyArray = array();
$publish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => $emptyArray
));

echo($publish_success[0] . $publish_success[1]);
echo "\r\n";

// This should return a failure (0) JSON Array
$publish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
));
echo($publish_success[0] . $publish_success[1]);
echo "\r\n";


## ---------------------------------------------------------------------------
## detailedHistory Example
## ---------------------------------------------------------------------------
echo "Running detailedHistory\r\n";
$history = $pubnub->detailedHistory(array(
'channel' => $channel,
'count' => 5,
'end' => "13466530169226760"
));
print_r($history);
echo "\r\n";

## ---------------------------------------------------------------------------
## History Example
## ---------------------------------------------------------------------------
echo "Running history\r\n";
$history = $pubnub->history(array(
'channel' => $channel,
'limit' => 2
));
echo($history);
echo "\r\n";

## ---------------------------------------------------------------------------
## Here_Now Example
## ---------------------------------------------------------------------------
echo "Running here_now\r\n";
$here_now = $pubnub->here_now(array(
'channel' => $channel
));
var_dump($here_now);
echo "\r\n";

## ---------------------------------------------------------------------------
## Timestamp Example
## ---------------------------------------------------------------------------
echo "Running timestamp\r\n";
$timestamp = $pubnub->time();
echo('Timestamp: ' . $timestamp);
echo "\r\n";

## ---------------------------------------------------------------------------
## Presence Example
## ---------------------------------------------------------------------------
//echo("\nWaiting for Presence message... Hit CTRL+C to finish.\n");
//
//$pubnub->presence(array(
// 'channel' => $channel,
// 'callback' => function($message) {
// print_r($message);
// echo "\r\n";
// return false;
// }
//));

## ---------------------------------------------------------------------------
## Subscribe Example
## ---------------------------------------------------------------------------
echo("\nWaiting for Publish message... Hit CTRL+C to finish.\n");

$pubnub->subscribe(array(
'channel' => $channel,
'callback' => function($message) {
print_r($message);
echo "\r\n";
return true;
}
));

?>

0 comments on commit f8aa33f

Please sign in to comment.