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_hash.py: 77%

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

22 statements  

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 

15 

16from nacl import exceptions as exc 

17from nacl._sodium import ffi, lib 

18from nacl.exceptions import ensure 

19 

20# crypto_hash_BYTES = lib.crypto_hash_bytes() 

21crypto_hash_BYTES: int = lib.crypto_hash_sha512_bytes() 

22crypto_hash_sha256_BYTES: int = lib.crypto_hash_sha256_bytes() 

23crypto_hash_sha512_BYTES: int = lib.crypto_hash_sha512_bytes() 

24 

25 

26def crypto_hash(message: bytes) -> bytes: 

27 """ 

28 Hashes and returns the message ``message``. 

29 

30 :param message: bytes 

31 :rtype: bytes 

32 """ 

33 digest = ffi.new("unsigned char[]", crypto_hash_BYTES) 

34 rc = lib.crypto_hash(digest, message, len(message)) 

35 ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError) 

36 return ffi.buffer(digest, crypto_hash_BYTES)[:] 

37 

38 

39def crypto_hash_sha256(message: bytes) -> bytes: 

40 """ 

41 Hashes and returns the message ``message``. 

42 

43 :param message: bytes 

44 :rtype: bytes 

45 """ 

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

47 rc = lib.crypto_hash_sha256(digest, message, len(message)) 

48 ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError) 

49 return ffi.buffer(digest, crypto_hash_sha256_BYTES)[:] 

50 

51 

52def crypto_hash_sha512(message: bytes) -> bytes: 

53 """ 

54 Hashes and returns the message ``message``. 

55 

56 :param message: bytes 

57 :rtype: bytes 

58 """ 

59 digest = ffi.new("unsigned char[]", crypto_hash_sha512_BYTES) 

60 rc = lib.crypto_hash_sha512(digest, message, len(message)) 

61 ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError) 

62 return ffi.buffer(digest, crypto_hash_sha512_BYTES)[:]