Digest::hash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Acquia\Hmac\Digest;
4
5
class Digest implements DigestInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $algorithm;
11
12
    /**
13
     * @param string $algorithm
14
     */
15 28
    public function __construct($algorithm = 'sha256')
16
    {
17 28
        $this->algorithm = $algorithm;
18 28
    }
19
20
    /**
21
     * {@inheritDoc}
22
     */
23 21
    public function sign($message, $secretKey)
24
    {
25
        // The Acquia HMAC spec requires that we use MIME Base64 encoded
26
        // secrets, but PHP requires them to be decoded before signing.
27 21
        $digest = hash_hmac($this->algorithm, $message, base64_decode($secretKey, true), true);
28
29 21
        return base64_encode($digest);
30
    }
31
32
33
    /**
34
     * {@inheritDoc}
35
     */
36 6
    public function hash($message)
37
    {
38 6
        return base64_encode(hash($this->algorithm, $message, true));
39
    }
40
}
41