/ RNS / Cryptography / Hashes.py
Hashes.py
 1  # Reticulum License
 2  #
 3  # Copyright (c) 2016-2025 Mark Qvist
 4  #
 5  # Permission is hereby granted, free of charge, to any person obtaining a copy
 6  # of this software and associated documentation files (the "Software"), to deal
 7  # in the Software without restriction, including without limitation the rights
 8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9  # copies of the Software, and to permit persons to whom the Software is
10  # furnished to do so, subject to the following conditions:
11  #
12  # - The Software shall not be used in any kind of system which includes amongst
13  #   its functions the ability to purposefully do harm to human beings.
14  #
15  # - The Software shall not be used, directly or indirectly, in the creation of
16  #   an artificial intelligence, machine learning or language model training
17  #   dataset, including but not limited to any use that contributes to the
18  #   training or development of such a model or algorithm.
19  #
20  # - The above copyright notice and this permission notice shall be included in
21  #   all copies or substantial portions of the Software.
22  #
23  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29  # SOFTWARE.
30  
31  import importlib.util
32  if importlib.util.find_spec('hashlib') != None:
33      import hashlib
34  else:
35      hashlib = None
36  
37  if hasattr(hashlib, "sha512"):
38      from hashlib import sha512 as ext_sha512
39  else:
40      from .SHA512 import sha512 as ext_sha512
41  
42  if hasattr(hashlib, "sha256"):
43      from hashlib import sha256 as ext_sha256
44  else:
45      from .SHA256 import sha256 as ext_sha256
46  
47  """
48  The SHA primitives are abstracted here to allow platform-
49  aware hardware acceleration in the future. Currently only
50  uses Python's internal SHA-256 implementation. All SHA-256
51  calls in RNS end up here.
52  """
53  
54  def sha256(data):
55      digest = ext_sha256()
56      digest.update(data)
57  
58      return digest.digest()
59  
60  def sha512(data):
61      digest = ext_sha512()
62      digest.update(data)
63  
64      return digest.digest()