Passed
Push — 1.11.x ( 31fff3...86e7ae )
by Yannick
15:37 queued 10s
created

Crypt   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 34
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A GetHash() 0 3 1
A ReadHash() 0 13 3
1
<?php
2
/**
3
 *
4
 * (c) Copyright Ascensio System SIA 2021
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 */
19
20
require_once __DIR__ . "/../../../main/inc/global.inc.php";
21
22
class Crypt {
23
24
    /**
25
     * Generate token for the object
26
     *
27
     * @param array $object - object to signature
28
     *
29
     * @return string
30
     */
31
    public static function GetHash($object)
32
    {
33
        return \Firebase\JWT\JWT::encode($object, api_get_security_key());
34
    }
35
36
    /**
37
     * Create an object from the token
38
     *
39
     * @param string $token - token
40
     *
41
     * @return array
42
     */
43
    public static function ReadHash($token)
44
    {
45
        $result = null;
46
        $error = null;
47
        if ($token === null) {
48
            return [$result, "token is empty"];
49
        }
50
        try {
51
            $result = \Firebase\JWT\JWT::decode($token, api_get_security_key(), array("HS256"));
52
        } catch (\UnexpectedValueException $e) {
53
            $error = $e->getMessage();
54
        }
55
        return [$result, $error];
56
    }
57
}
58