Signature::getUidSignature()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Validation;
15
16
class Signature
17
{
18
    const TIMESTAMP_OFFSET = 180;
19
20
    /**
21
     * @param int $timestamp Unix Timestamp
22
     *
23
     * @return bool
24
     */
25 5
    public function checkTimestamp($timestamp)
26
    {
27 5
        return (abs(time() - $timestamp) <= static::TIMESTAMP_OFFSET);
28
    }
29
30
    /**
31
     * @param string $uid
32
     * @param int    $timestamp
33
     * @param string $secret
34
     *
35
     * @return string
36
     */
37 8
    public function getUidSignature($uid, $timestamp, $secret)
38
    {
39 8
        $baseString = $timestamp . '_' . $uid;
40
41 8
        return $this->calculateSignature($baseString, $secret);
42
    }
43
44
    /**
45
     * @param string $uid
46
     * @param string $friendUid
47
     * @param int    $timestamp
48
     * @param string $secret
49
     *
50
     * @return string
51
     */
52 6
    public function getFriendUidSignature($uid, $friendUid, $timestamp, $secret)
53
    {
54 6
        $baseString = sprintf('%d_%s_%s', $timestamp, $friendUid, $uid);
55
56 6
        return $this->calculateSignature($baseString, $secret);
57
    }
58
59
    /**
60
     * @param string $baseString
61
     * @param string $key
62
     *
63
     * @return string
64
     */
65 19
    public function calculateSignature($baseString, $key)
66
    {
67 19
        $hmac = hash_hmac('sha1', utf8_encode($baseString), base64_decode($key), true);
68
69 19
        return base64_encode($hmac);
70
    }
71
}
72