Debian Codegraph @

proton/session/srp/pmhash.py

raw
1 """
2 Copyright (c) 2023 Proton AG
3
4 This file is part of Proton.
5
6 Proton is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Proton is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with ProtonVPN. If not, see <https://www.gnu.org/licenses/>.
18 """
19 # Custom expanded version of SHA512
20 import hashlib
21
22
23 class PMHash:
24 digest_size = 256
25 name = 'PMHash'
26
27 def __init__(self, b=b""):
28 self.b = b
29
30 def update(self, b):
31 self.b += b
32
33 def digest(self):
34 return hashlib.sha512(
35 self.b + b'\0'
36 ).digest() + hashlib.sha512(
37 self.b + b'\1'
38 ).digest() + hashlib.sha512(
39 self.b + b'\2'
40 ).digest() + hashlib.sha512(
41 self.b + b'\3'
42 ).digest()
43
44 def hexdigest(self):
45 return self.digest().hex()
46
47 def copy(self):
48 return PMHash(self.b)
49
50
51 def pmhash(b=b""):
52 return PMHash(b)