Welcome to mirror list, hosted at ThFree Co, Russian Federation.

crypt.php « lib - github.com/ONLYOFFICE/onlyoffice-nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14222d28ce477f93a3103a0f077fcc847e1642b3 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
 *
 * (c) Copyright Ascensio System Limited 2010-2017
 *
 * This program is freeware. You can redistribute it and/or modify it under the terms of the GNU 
 * General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html). 
 * In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that 
 * Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
 *
 * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
 *
 * You can contact Ascensio System SIA by email at sales@onlyoffice.com
 *
 * The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display 
 * Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
 *
 * Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains 
 * relevant author attributions when distributing the software. If the display of the logo in its graphic 
 * form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE" 
 * in every copy of the program you distribute. 
 * Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
 *
 */

namespace OCA\Onlyoffice;

use OCA\Onlyoffice\AppConfig;

/**
 * Hash generator
 *
 * @package OCA\Onlyoffice
 */
class Crypt {

    /**
     * The secret key from the application configuration
     *
     * @var string
     */
    private $skey;

    /**
     * @param OCA\Onlyoffice\AppConfig $config - application configutarion
     */
    public function __construct(AppConfig $appConfig) {
        $this->skey = $appConfig->GetSKey();
    }

    /**
     * Generate base64 hash for the object
     * 
     * @param array $object - object to signature hash
     *
     * @return string
     */
    public function GetHash($object) {
        $primaryKey = json_encode($object);
        $hash = $this->SignatureCreate($primaryKey);
        return $hash;
    }

    /**
     * Create an object from the base64 hash
     * 
     * @param string $hash - base64 hash
     *
     * @return array
     */
    public function ReadHash($hash) {
        $result = NULL;
        if ($hash === NULL) {
            return $result;
        }
        try {
            $payload = base64_decode($hash);
            $payloadParts = explode("?", $payload);

            $encode = base64_encode( hash( "sha256", ($payloadParts[1] . $this->skey), true ) );

            if ($payloadParts[0] === $encode) {
                $result = json_decode($payloadParts[1]);
            }
        } catch (\Exception $e) {
        }
        return $result;
    }

    /**
     * Generate base64 hash for the object
     * 
     * @param string $primary_key - string to the signature hash
     *
     * @return string
     */
    private function SignatureCreate($primary_key) {
        $payload = base64_encode( hash( "sha256", ($primary_key . $this->skey), true ) ) . "?" . $primary_key;
        $base64Str = base64_encode($payload);

        return $base64Str;
    }
}