hash.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2013 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import, division, print_function
  15. import nacl.bindings
  16. import nacl.encoding
  17. BLAKE2B_BYTES = nacl.bindings.crypto_generichash_BYTES
  18. BLAKE2B_BYTES_MIN = nacl.bindings.crypto_generichash_BYTES_MIN
  19. BLAKE2B_BYTES_MAX = nacl.bindings.crypto_generichash_BYTES_MAX
  20. BLAKE2B_KEYBYTES = nacl.bindings.crypto_generichash_KEYBYTES
  21. BLAKE2B_KEYBYTES_MIN = nacl.bindings.crypto_generichash_KEYBYTES_MIN
  22. BLAKE2B_KEYBYTES_MAX = nacl.bindings.crypto_generichash_KEYBYTES_MAX
  23. BLAKE2B_SALTBYTES = nacl.bindings.crypto_generichash_SALTBYTES
  24. BLAKE2B_PERSONALBYTES = nacl.bindings.crypto_generichash_PERSONALBYTES
  25. SIPHASH_BYTES = nacl.bindings.crypto_shorthash_siphash24_BYTES
  26. SIPHASH_KEYBYTES = nacl.bindings.crypto_shorthash_siphash24_KEYBYTES
  27. _b2b_hash = nacl.bindings.crypto_generichash_blake2b_salt_personal
  28. _sip_hash = nacl.bindings.crypto_shorthash_siphash24
  29. def sha256(message, encoder=nacl.encoding.HexEncoder):
  30. return encoder.encode(nacl.bindings.crypto_hash_sha256(message))
  31. def sha512(message, encoder=nacl.encoding.HexEncoder):
  32. return encoder.encode(nacl.bindings.crypto_hash_sha512(message))
  33. def blake2b(data, digest_size=BLAKE2B_BYTES, key=b'',
  34. salt=b'', person=b'',
  35. encoder=nacl.encoding.HexEncoder):
  36. """
  37. One-shot blake2b digest
  38. :param data: the digest input byte sequence
  39. :type data: bytes
  40. :param digest_size: the requested digest size; must be at most
  41. :py:data:`.BLAKE2B_BYTES_MAX`;
  42. the default digest size is :py:data:`.BLAKE2B_BYTES`
  43. :type digest_size: int
  44. :param key: the key to be set for keyed MAC/PRF usage; if set, the key
  45. must be at most :py:data:`.BLAKE2B_KEYBYTES_MAX` long
  46. :type key: bytes
  47. :param salt: an initialization salt at most
  48. :py:data:`.BLAKE2B_SALTBYTES` long; it will be zero-padded
  49. if needed
  50. :type salt: bytes
  51. :param person: a personalization string at most
  52. :py:data:`.BLAKE2B_PERSONALBYTES` long; it will be
  53. zero-padded if needed
  54. :type person: bytes
  55. :param encoder: the encoder to use on returned digest
  56. :type encoder: class
  57. :return: encoded bytes data
  58. :rtype: the return type of the choosen encoder
  59. """
  60. digest = _b2b_hash(data, digest_size=digest_size, key=key,
  61. salt=salt, person=person)
  62. return encoder.encode(digest)
  63. generichash = blake2b
  64. def siphash24(message, key=b'', encoder=nacl.encoding.HexEncoder):
  65. """
  66. Computes a keyed MAC of ``message`` using siphash-2-4
  67. :param message: The message to hash.
  68. :type message: bytes
  69. :param key: the message authentication key for the siphash MAC construct
  70. :type key: bytes(:py:data:`.SIPHASH_KEYBYTES`)
  71. :param encoder: A class that is able to encode the hashed message.
  72. :return: The hashed message.
  73. :rtype: bytes(:py:data:`.SIPHASH_BYTES`)
  74. The :py:func:`.siphash24` construct is also exposed with the
  75. :py:func:`.shorthash` name for compatibility with libsodium names.
  76. """
  77. digest = _sip_hash(message, key)
  78. return encoder.encode(digest)
  79. shorthash = siphash24