Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/oscrypto/_rand.py: 43%

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

14 statements  

1# coding: utf-8 

2from __future__ import unicode_literals, division, absolute_import, print_function 

3 

4import os 

5 

6from ._errors import pretty_message 

7from ._types import type_name, int_types 

8 

9 

10__all__ = [ 

11 'rand_bytes', 

12] 

13 

14 

15def rand_bytes(length): 

16 """ 

17 Returns a number of random bytes suitable for cryptographic purposes 

18 

19 :param length: 

20 The desired number of bytes 

21 

22 :raises: 

23 ValueError - when any of the parameters contain an invalid value 

24 TypeError - when any of the parameters are of the wrong type 

25 OSError - when an error is returned by OpenSSL 

26 

27 :return: 

28 A byte string 

29 """ 

30 

31 if not isinstance(length, int_types): 

32 raise TypeError(pretty_message( 

33 ''' 

34 length must be an integer, not %s 

35 ''', 

36 type_name(length) 

37 )) 

38 

39 if length < 1: 

40 raise ValueError('length must be greater than 0') 

41 

42 if length > 1024: 

43 raise ValueError('length must not be greater than 1024') 

44 

45 return os.urandom(length)