1<?php
2use Crypto\Hash;
3use Crypto\AlgorihtmException;
4
5namespace Crypto;
6
7$algorithm = 'sha256';
8
9if (!Hash::hasAlgorithm($algorithm)) {
10	die("Algorithm $algorithm not found" . PHP_EOL);
11}
12
13try {
14	// create Hash object
15	$hash = new Hash($algorithm);
16
17	// Algorithm method for retrieving algorithm
18	echo "Algorithm: " . $hash->getAlgorithmName() . PHP_EOL;
19
20	// Params
21	echo "Size: " . $hash->getSize() . PHP_EOL;
22	echo "Block size: " . $hash->getBlockSize() . PHP_EOL;
23
24	// Test data
25	$data1 = "Test";
26	$data2 = "Data";
27	$data = $data1 . $data2;
28
29	// Simple hash (object created using static method)
30	$hash = Hash::sha256();
31	$hash->update($data);
32	$sim_hash = $hash->hexdigest();
33
34	// init/update/final hash
35	$hash->update($data1);
36	$hash->update($data2);
37	$iuf_hash = $hash->hexdigest();
38
39	// Create hash in one expression
40	$one_hash = Hash::sha256($data)->hexdigest();
41
42	// Raw data output (used hex format for printing)
43	echo "Hash (sim): " . $sim_hash . PHP_EOL;
44	echo "Hash (iuf): " . $iuf_hash . PHP_EOL;
45	echo "Hash (one): " . $one_hash . PHP_EOL;
46	// sim = iuf = con
47}
48catch (AlgorithmException $e) {
49	echo $e->getMessage() . PHP_EOL;
50}
51