You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.5 KiB

7 years ago
  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. SIPHASHX_BYTES = nacl.bindings.crypto_shorthash_siphashx24_BYTES
  28. SIPHASHX_KEYBYTES = nacl.bindings.crypto_shorthash_siphashx24_KEYBYTES
  29. _b2b_hash = nacl.bindings.crypto_generichash_blake2b_salt_personal
  30. _sip_hash = nacl.bindings.crypto_shorthash_siphash24
  31. _sip_hashx = nacl.bindings.crypto_shorthash_siphashx24
  32. def sha256(message, encoder=nacl.encoding.HexEncoder):
  33. return encoder.encode(nacl.bindings.crypto_hash_sha256(message))
  34. def sha512(message, encoder=nacl.encoding.HexEncoder):
  35. return encoder.encode(nacl.bindings.crypto_hash_sha512(message))
  36. def blake2b(data, digest_size=BLAKE2B_BYTES, key=b'',
  37. salt=b'', person=b'',
  38. encoder=nacl.encoding.HexEncoder):
  39. """
  40. One-shot blake2b digest
  41. :param data: the digest input byte sequence
  42. :type data: bytes
  43. :param digest_size: the requested digest size; must be at most
  44. :py:data:`.BLAKE2B_BYTES_MAX`;
  45. the default digest size is :py:data:`.BLAKE2B_BYTES`
  46. :type digest_size: int
  47. :param key: the key to be set for keyed MAC/PRF usage; if set, the key
  48. must be at most :py:data:`.BLAKE2B_KEYBYTES_MAX` long
  49. :type key: bytes
  50. :param salt: an initialization salt at most
  51. :py:data:`.BLAKE2B_SALTBYTES` long; it will be zero-padded
  52. if needed
  53. :type salt: bytes
  54. :param person: a personalization string at most
  55. :py:data:`.BLAKE2B_PERSONALBYTES` long; it will be
  56. zero-padded if needed
  57. :type person: bytes
  58. :param encoder: the encoder to use on returned digest
  59. :type encoder: class
  60. :return: encoded bytes data
  61. :rtype: the return type of the choosen encoder
  62. """
  63. digest = _b2b_hash(data, digest_size=digest_size, key=key,
  64. salt=salt, person=person)
  65. return encoder.encode(digest)
  66. generichash = blake2b
  67. def siphash24(message, key=b'', encoder=nacl.encoding.HexEncoder):
  68. """
  69. Computes a keyed MAC of ``message`` using siphash-2-4
  70. :param message: The message to hash.
  71. :type message: bytes
  72. :param key: the message authentication key for the siphash MAC construct
  73. :type key: bytes(:py:data:`.SIPHASH_KEYBYTES`)
  74. :param encoder: A class that is able to encode the hashed message.
  75. :return: The hashed message.
  76. :rtype: bytes(:py:data:`.SIPHASH_BYTES`)
  77. The :py:func:`.siphash24` construct is also exposed with the
  78. :py:func:`.shorthash` name for compatibility with libsodium names.
  79. """
  80. digest = _sip_hash(message, key)
  81. return encoder.encode(digest)
  82. shorthash = siphash24
  83. def siphashx24(message, key=b'', encoder=nacl.encoding.HexEncoder):
  84. """
  85. Computes a keyed MAC of ``message`` using the 128 bit siphash-2-4
  86. variant siphashx24
  87. :param message: The message to hash.
  88. :type message: bytes
  89. :param key: the message authentication key for the siphash MAC construct
  90. :type key: bytes(:py:data:`.SIPHASHX_KEYBYTES`)
  91. :param encoder: A class that is able to encode the hashed message.
  92. :return: The hashed message.
  93. :rtype: bytes(:py:data:`.SIPHASHX_BYTES`)
  94. .. versionadded:: 1.2
  95. """
  96. digest = _sip_hashx(message, key)
  97. return encoder.encode(digest)

Powered by TurnKey Linux.