'Converts a python integer to a BIGNUM. The returned BIGNUM will not
be garbage collected (to support adding them to structs that take
ownership of the object). Be sure to register it for GC if it will
be discarded after use.'

def _int_to_bn(self, num, bn=None):

    assert ((bn is None) or (bn != self._ffi.NULL))
    if (bn is None):
        bn = self._ffi.NULL
    if six.PY3:
        binary = num.to_bytes(int(((num.bit_length() / 8.0) + 1)), 'big')
        bn_ptr = self._lib.BN_bin2bn(binary, len(binary), bn)
        self.openssl_assert((bn_ptr != self._ffi.NULL))
        return bn_ptr
    else:
        hex_num = (hex(num).rstrip('L').lstrip('0x').encode('ascii') or '0')
        bn_ptr = self._ffi.new('BIGNUM    **')
        bn_ptr[0] = bn
        res = self._lib.BN_hex2bn(bn_ptr, hex_num)
        self.openssl_assert((res != 0))
        self.openssl_assert((bn_ptr[0] != self._ffi.NULL))
        return bn_ptr[0]
