-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhash.php
More file actions
50 lines (39 loc) · 1.12 KB
/
hash.php
File metadata and controls
50 lines (39 loc) · 1.12 KB
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
<?php
use Crypto\Hash;
use Crypto\AlgorihtmException;
namespace Crypto;
$algorithm = 'sha256';
if (!Hash::hasAlgorithm($algorithm)) {
die("Algorithm $algorithm not found" . PHP_EOL);
}
try {
// create Hash object
$hash = new Hash($algorithm);
// Algorithm method for retrieving algorithm
echo "Algorithm: " . $hash->getAlgorithmName() . PHP_EOL;
// Params
echo "Size: " . $hash->getSize() . PHP_EOL;
echo "Block size: " . $hash->getBlockSize() . PHP_EOL;
// Test data
$data1 = "Test";
$data2 = "Data";
$data = $data1 . $data2;
// Simple hash (object created using static method)
$hash = Hash::sha256();
$hash->update($data);
$sim_hash = $hash->hexdigest();
// init/update/final hash
$hash->update($data1);
$hash->update($data2);
$iuf_hash = $hash->hexdigest();
// Create hash in one expression
$one_hash = Hash::sha256($data)->hexdigest();
// Raw data output (used hex format for printing)
echo "Hash (sim): " . $sim_hash . PHP_EOL;
echo "Hash (iuf): " . $iuf_hash . PHP_EOL;
echo "Hash (one): " . $one_hash . PHP_EOL;
// sim = iuf = con
}
catch (AlgorithmException $e) {
echo $e->getMessage() . PHP_EOL;
}