forked from ptarjan/crypto-request-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.php
More file actions
executable file
·65 lines (55 loc) · 1.73 KB
/
generate.php
File metadata and controls
executable file
·65 lines (55 loc) · 1.73 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env php
<?php
function base64_url_encode($input) {
$str = strtr(base64_encode($input), '+/=', '-_.');
$str = str_replace('.', '', $str); // remove padding
return $str;
}
function pkcs5_pad($input, $blocksize) {
$pad = $blocksize - (strlen($input) % $blocksize);
return $input . str_repeat(chr($pad), $pad);
}
function encrypt_data($data, $secret) {
$data = json_encode($data);
if (function_exists('openssl_cipher_iv_length')) {
$mode = 'aes-256-cbc';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($mode));
$ed = openssl_encrypt(
$data,
$mode,
$secret,
true,
$iv);
} else {
$cipher = MCRYPT_RIJNDAEL_128;
$mode = MCRYPT_MODE_CBC;
$data = pkcs5_pad($data, mcrypt_get_block_size($cipher, $mode));
$iv = mcrypt_create_iv(
mcrypt_get_iv_size($cipher, $mode), MCRYPT_DEV_URANDOM);
$ed = mcrypt_encrypt(
$cipher, $secret, $data, $mode, $iv);
}
return array(
'payload' => base64_url_encode($ed),
'iv' => base64_url_encode($iv),
);
}
function generate_signed_request($data, $secret, $encrypt=false) {
// wrap data inside payload if we are encrypting
if ($encrypt) {
$data = encrypt_data($data, $secret);
}
// always present, and always at the top level
$data['algorithm'] = $encrypt ? 'AES-256-CBC HMAC-SHA256' : 'HMAC-SHA256';
$data['issued_at'] = time();
// sign it
$payload = base64_url_encode(json_encode($data));
$sig = base64_url_encode(
hash_hmac('sha256', $payload, $secret, $raw=true));
return $sig.'.'.$payload;
}
$secret = '13750c9911fec5865d01f3bd00bdf4db';
echo generate_signed_request(
array('the' => array('answer' => "the answer is forty two")),
$secret,
$_SERVER['DO_ENCRYPT'] == '1');