jaraco.crypto / ctypescrypto / rand.py

import ctypes

from evp import lib

class RandError(Exception):
	pass

lib.RAND_bytes.argtypes = ctypes.c_char_p, ctypes.c_int
lib.RAND_pseudo_bytes.argtypes = ctypes.c_char_p, ctypes.c_int

def bytes(num, check_result=False):
	if num <= 0:
		raise ValueError("num must be > 0")
	bytes = ctypes.create_string_buffer(num)
	result = lib.RAND_bytes(bytes, num) 
	if check_result and result == 0:
		msg = "Random Number Generator not seeded sufficiently"
		raise RandError(msg)
	return bytes.raw[:num]

def pseudo_bytes(num):
	if num <= 0:
		raise ValueError("num must be > 0")
	bytes = ctypes.create_string_buffer(num)
	lib.RAND_pseudo_bytes(bytes, num)
	return bytes.raw[:num]

lib.RAND_seed.argtypes = ctypes.c_char_p, ctypes.c_int
lib.RAND_add.argtypes = ctypes.c_char_p, ctypes.c_int, ctypes.c_double

def seed(data, entropy=None):
	if not isinstance(data, basestring):
		raise TypeError("data must be a string")
	size = len(data)
	params = [data, len(data)]
	if entropy:
		func = lib.RAND_add
		params.append(entropy)
	else:
		func = lib.RAND_seed
	func(*params)

status = lib.RAND_status
cleanup = lib.RAND_cleanup
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.