Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PyNaCl-1.6.2-py3.11-linux-x86_64.egg/nacl/bindings/crypto_shorthash.py: 48%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

1# Copyright 2016 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 

15 

16import nacl.exceptions as exc 

17from nacl._sodium import ffi, lib 

18from nacl.exceptions import ensure 

19 

20has_crypto_shorthash_siphashx24 = bool( 

21 lib.PYNACL_HAS_CRYPTO_SHORTHASH_SIPHASHX24 

22) 

23 

24BYTES: int = lib.crypto_shorthash_siphash24_bytes() 

25KEYBYTES: int = lib.crypto_shorthash_siphash24_keybytes() 

26 

27XBYTES = 0 

28XKEYBYTES = 0 

29 

30if has_crypto_shorthash_siphashx24: 

31 XBYTES = lib.crypto_shorthash_siphashx24_bytes() 

32 XKEYBYTES = lib.crypto_shorthash_siphashx24_keybytes() 

33 

34 

35def crypto_shorthash_siphash24(data: bytes, key: bytes) -> bytes: 

36 """Compute a fast, cryptographic quality, keyed hash of the input data 

37 

38 :param data: 

39 :type data: bytes 

40 :param key: len(key) must be equal to 

41 :py:data:`.KEYBYTES` (16) 

42 :type key: bytes 

43 """ 

44 if len(key) != KEYBYTES: 

45 raise exc.ValueError(f"Key length must be exactly {KEYBYTES} bytes") 

46 digest = ffi.new("unsigned char[]", BYTES) 

47 rc = lib.crypto_shorthash_siphash24(digest, data, len(data), key) 

48 

49 ensure(rc == 0, raising=exc.RuntimeError) 

50 return ffi.buffer(digest, BYTES)[:] 

51 

52 

53def crypto_shorthash_siphashx24(data: bytes, key: bytes) -> bytes: 

54 """Compute a fast, cryptographic quality, keyed hash of the input data 

55 

56 :param data: 

57 :type data: bytes 

58 :param key: len(key) must be equal to 

59 :py:data:`.XKEYBYTES` (16) 

60 :type key: bytes 

61 :raises nacl.exceptions.UnavailableError: If called when using a 

62 minimal build of libsodium. 

63 """ 

64 ensure( 

65 has_crypto_shorthash_siphashx24, 

66 "Not available in minimal build", 

67 raising=exc.UnavailableError, 

68 ) 

69 

70 if len(key) != XKEYBYTES: 

71 raise exc.ValueError(f"Key length must be exactly {XKEYBYTES} bytes") 

72 digest = ffi.new("unsigned char[]", XBYTES) 

73 rc = lib.crypto_shorthash_siphashx24(digest, data, len(data), key) 

74 

75 ensure(rc == 0, raising=exc.RuntimeError) 

76 return ffi.buffer(digest, XBYTES)[:]