-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples.php
113 lines (100 loc) · 3.16 KB
/
examples.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
require 'vendor/autoload.php';
use TheFox\Pow\Hashcash;
// Example 1: simple mint (real world example)
$hashcash = new Hashcash();
$hashcash->setBits(20);
$hashcash->setResource('[email protected]');
try {
$stamp = $hashcash->mint();
printf("stamp1: '%s'\n", $stamp);
} catch (Exception $e) {
printf("ERROR 1: %s\n", $e->getMessage());
}
// Example 2a: simple mint, another way (real world example)
$hashcash = new Hashcash(20, '[email protected]');
$stamp = '';
try {
$stamp = $hashcash->mint();
printf("stamp2: '%s'\n", $stamp);
} catch (Exception $e) {
printf("ERROR 2: %s\n", $e->getMessage());
}
// Example 2b: verify a stamp (real world example)
$hashcash = new Hashcash();
try {
printf("stamp2 verify: '%s'\n", $hashcash->verify($stamp) ? 'Ok' : 'failed');
} catch (Exception $e) {
printf("ERROR 3: %s\n", $e->getMessage());
}
// Example 3a: use a certain date
$hashcash = new Hashcash(20, '[email protected]');
$hashcash->setDate('870221');
$stamp = '';
try {
$stamp = $hashcash->mint();
printf("stamp3: '%s'\n", $stamp);
} catch (Exception $e) {
printf("ERROR 4: %s\n", $e->getMessage());
}
// Example 3b: verify a stamp, must fail
$hashcash = new Hashcash();
$hashcash->setExpiration(3600 * 24 * 2); // Expire in 2 days.
try {
printf("stamp3 verify: '%s' (must fail)\n", $hashcash->verify($stamp) ? 'Ok' : 'failed');
} catch (Exception $e) {
printf("ERROR 5: %s\n", $e->getMessage());
}
// Example 4a: use a certain date
$hashcash = new Hashcash(20, '[email protected]');
$hashcash->setDate('870221');
$stamp = '';
try {
$stamp = $hashcash->mint();
printf("stamp4: '%s'\n", $stamp);
} catch (Exception $e) {
printf("ERROR 6: %s\n", $e->getMessage());
}
// Example 4a: verify a stamp, ignore expiration
$hashcash = new Hashcash();
$hashcash->setExpiration(0); // Never expire.
try {
printf( "stamp4 verify: '%s'\n",$hashcash->verify($stamp) ? 'Ok' : 'failed');
} catch (Exception $e) {
printf("ERROR 7: %s\n", $e->getMessage());
}
// Example 5: use 1 attempt, must fail with this configuration
$hashcash = new Hashcash(19, '[email protected]');
$hashcash->setDate('140427');
$hashcash->setSalt('axfcrlV1hxLvF6J9BeDiLw==');
$hashcash->setMintAttemptsMax(1);
$stamp = '';
try {
$stamp = $hashcash->mint();
printf("stamp5: '%s'\n", $stamp);
} catch (Exception $e) {
printf("ERROR 8: %s\n", $e->getMessage());
}
// Example 6: use infinite attempts
$hashcash = new Hashcash(19, '[email protected]');
$hashcash->setDate('140427');
$hashcash->setSalt('axfcrlV1hxLvF6J9BeDiLw==');
$hashcash->setMintAttemptsMax(0); // Use infinite attempts
$stamp = '';
try {
$stamp = $hashcash->mint();
printf("stamp6: '%s'\n", $stamp);
} catch (Exception $e) {
printf("ERROR 9: %s\n", $e->getMessage());
}
// Example 7a: use short syntax
$stamp = Hashcash::newInstance(20, '[email protected]')
->setDate(date(Hashcash::DATE_FORMAT12))
->mint()
;
// Example 7b: verify short syntax which is valid for 3 minutes
try {
print "stamp7 verify: '" . (Hashcash::newInstance()->setExpiration(60 * 3)->verify($stamp) ? 'Ok' : 'failed') . "'\n";
} catch (Exception $e) {
printf("ERROR 10: %s\n", $e->getMessage());
}