Skip to content
Browse files

bcrypt testcase

  • Loading branch information...
1 parent 3eecd85 commit 0030dd14be23fbfb76389a36387c909922209202 @Trellmor Trellmor committed Jun 5, 2013
Showing with 59 additions and 0 deletions.
  1. +59 −0 test/hash.php
View
59 test/hash.php
@@ -0,0 +1,59 @@
+<?php
+
+define("BCRYPT", true);
+define("BCRYPT_ROUNDS", 12);
+
+require_once __DIR__ . '/../weave_hash.php';
+
+$pwd = "asdfASDFghjkGHJK2134$%&";
+
+try {
+ $hash = WeaveHashFactory::factory();
+ $time_start = microtime(true);
+ echo $hash->hash($pwd) . "\n";
+ $time = microtime(true) - $time_start;
+ echo "Hashing took " . $time . " seconds\n";
+
+ if (!$hash->verify($pwd, '$2a$12$O2Bn6lDUYS5NDIJ1uCZjGezSI/jeGTD7Ow0bd3PFMRBcGIqfqI4Oi')) {
+ throw new Exception("bcrypt hash compare failed");
+ }
+
+ if (!$hash->needsUpdate(md5($pwd))) {
+ throw new Exception("bcrypt hash needs update.");
+ }
+
+ if ($hash->needsUpdate('$2a$12$O2Bn6lDUYS5NDIJ1uCZjGezSI/jeGTD7Ow0bd3PFMRBcGIqfqI4Oi')) {
+ throw new Exception("bcrypt hash doesn't needs update.");
+ }
+
+ if (!$hash->verify($pwd, 'a96b71c678b01b98b9f7a0d8ec4b633b')) {
+ throw new Exception("bcrypt hash compare with md5 failed");
+ }
+
+ $hash2 = new WeaveHashBCrypt(6);
+
+ if (!$hash2->needsUpdate('$2a$12$O2Bn6lDUYS5NDIJ1uCZjGezSI/jeGTD7Ow0bd3PFMRBcGIqfqI4Oi')) {
+ throw new Exception("bcrypt hash needs update because of different rounds.");
+ }
+
+ $hashmd5 = new WeaveHashMD5();
+ if (!$hashmd5->verify($pwd, 'a96b71c678b01b98b9f7a0d8ec4b633b')) {
+ throw new Exception("md5 hash compare failed");
+ }
+
+ if (!$hashmd5->needsUpdate('$2a$12$O2Bn6lDUYS5NDIJ1uCZjGezSI/jeGTD7Ow0bd3PFMRBcGIqfqI4Oi')) {
+ throw new Exception("md5 hash needs update.");
+ }
+
+ if ($hashmd5->needsUpdate(md5($pwd))) {
+ throw new Exception("md5 hash doesn't need update.");
+ }
+
+ echo "all tests ok\n";
+ exit(0);
+} catch(Exception $e) {
+ echo $e->getMessage() . "\n";
+ exit(1);
+}
+
+?>

0 comments on commit 0030dd1

Please sign in to comment.
Something went wrong with that request. Please try again.