from ctypes import *

import ssl


# since we don't every have to access the internals to the BN structure we 
# can just subclass c_void_p
class BN(c_void_p):
    def __init__(self, value=0):
        c_void_p.__init__(self)
        self._dll = ssl.dll
        if not self._dll.BN_hex2bn(byref(self), "%X" % value):
            raise ssl.Error("Couldn't convert long to bn")
        
    def __del__(self):
        if self.value:
            self._dll.BN_free(self)
        del self._dll

    def __long__(self):
        return long(ssl.dll.BN_bn2hex(self), 16)

BN_p = POINTER(BN)
ssl.dll.BN_bn2hex.argtypes = (BN,)
ssl.dll.BN_bn2hex.restype = c_char_p
ssl.dll.BN_hex2bn.argtypes = (BN_p, c_char_p,)
