PHP Classes

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: 12 years ago
Size: 4,505 bytes
 

Contents

Class file image Download
<?
include 'ocb2.php';

$tests = array(
  array(
   
'adata'=>'0123456789ABCDEF',
   
'key'=>'JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxObIk=',
   
'text'=>'Androcles
  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
forest.

    Gratitude is the sign of noble souls.'
 
),
  array(
   
'adata'=>'0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF',
   
'key'=>'RSgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVtbVHCRc=',
   
'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."'
 
),
  array(
   
'adata'=>'swordfish',
   
'key'=>'khbV2Yl5+xvRMQummN+1rC/9ctvQGt+3uOGv7WomfpY=',
   
'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."'
 
),
  array(
   
'adata'=>'the swordfish is the most delicious of all weapon named fish, save the pike.',
   
'key'=>'unyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXTmk=',
   
'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
themselves."

    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);
 
$enc->setKey(Base64_Decode($tests[$i]['key']));
 
$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']) {
    echo
"<br>FAILED IN PHP<br>$temp<br><br>";
  }
  unset(
$tests[$i]['text']);
}
$json = json_encode($tests);
echo <<<EOD
<html>
<body>
<script src="sjcl.js"></script>
<pre>
<script>
var tests =
$json

for (var i = 0, len = tests.length; i < len; i++) {
  document.writeln(sjcl.codec.utf8String.fromBits(
    sjcl.mode.ocb2.decrypt(
      new sjcl.cipher.aes(sjcl.codec.base64.toBits(tests[i]['key'])),
      sjcl.codec.base64.toBits(tests[i]['ct']),
      sjcl.codec.base64.toBits(tests[i]['iv']),
      sjcl.codec.utf8String.toBits(tests[i]['adata'])
    )
  )+"\\n\\n");
}
</script>
<pre>
</body>
</html>
EOD;

?>