File: test.php

Recommend this page to a friend!
  Classes of Jordan Honeycutt  >  Offset CodeBook Mode 2.0  >  test.php  >  Download  
File: test.php
Role: Unit test script
Content type: text/plain
Description: test proof
Class: Offset CodeBook Mode 2.0
Encrypt and decrypt data using OCB 2
Author: By
Last change: added check/report for symmetry in encryption and decryption methods.
Date: 10 years ago
Size: 4,505 bytes


Class file image Download
include 'ocb2.php';

$tests = array(
  A slave named Androcles once escaped from his master and fled
to the forest. As he was wandering about there he came upon a
Lion lying down moaning and groaning. At first he turned to flee,
but finding that the Lion did not pursue him, he turned back and
went up to him. As he came near, the Lion put out his paw, which
was all swollen and bleeding, and Androcles found that a huge
thorn had got into it, and was causing all the pain. He pulled
out the thorn and bound up the paw of the Lion, who was soon able
to rise and lick the hand of Androcles like a dog. Then the Lion
took Androcles to his cave, and every day used to bring him meat
from which to live. But shortly afterwards both Androcles and the
Lion were captured, and the slave was sentenced to be thrown to
the Lion, after the latter had been kept without food for several
days. The Emperor and all his Court came to see the spectacle,
and Androcles was led out into the middle of the arena. Soon the
Lion was let loose from his den, and rushed bounding and roaring
towards his victim. But as soon as he came near to Androcles he
recognised his friend, and fawned upon him, and licked his hands
like a friendly dog. The Emperor, surprised at this, summoned
Androcles to him, who told him the whole story. Whereupon the
slave was pardoned and freed, and the Lion let loose to his native

    Gratitude is the sign of noble souls.'
'text'=>'The Cock and the Jewel
  A COCK, scratching for food for himself and his hens, found a
precious stone and exclaimed: "If your owner had found thee, and
not I, he would have taken thee up, and have set thee in thy
first estate; but I have found thee for no purpose. I would
rather have one barleycorn than all the jewels in the world."'
'text'=>'The Rose and the Amaranth
  AN AMARANTH planted in a garden near a Rose-Tree, thus addressed
it: "What a lovely flower is the Rose, a favorite alike with Gods
and with men. I envy you your beauty and your perfume." The Rose
replied, "I indeed, dear Amaranth, flourish but for a brief
season! If no cruel hand pluck me from my stem, yet I must perish
by an early doom. But thou art immortal and dost never fade, but
bloomest for ever in renewed youth."'
'adata'=>'the swordfish is the most delicious of all weapon named fish, save the pike.',
'text'=>'The Traveler and Fortune
  A TRAVELER wearied from a long journey lay down, overcome with
fatigue, on the very brink of a deep well. Just as he was about
to fall into the water, Dame Fortune, it is said, appeared to him
and waking him from his slumber thus addressed him: "Good Sir,
pray wake up: for if you fall into the well, the blame will be
thrown on me, and I shall get an ill name among mortals; for I
find that men are sure to impute their calamities to me, however
much by their own folly they have really brought them on

    Everyone is more or less master of his own fate. '

$enc = new ocb2('rijndael-128',null);
for (
$i = 0, $len = sizeof($tests); $i < $len; $i++) {
$iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
$tests[$i]['iv'] = base64_Encode($iv);
$tests[$i]['ct'] = base64_Encode($enc->encrypt($tests[$i]['text'],$iv,$tests[$i]['adata']));
$temp = $enc->decrypt(base64_Decode($tests[$i]['ct']),$iv,$tests[$i]['adata']);
  if (
$temp != $tests[$i]['text']) {
"<br>FAILED IN PHP<br>$temp<br><br>";
$json = json_encode($tests);
echo <<<EOD
<script src="sjcl.js"></script>
var tests =

for (var i = 0, len = tests.length; i < len; i++) {
      new sjcl.cipher.aes(sjcl.codec.base64.toBits(tests[i]['key'])),


For more information send a message to info at phpclasses dot org.