/// <summary> /// Generates a prime number /// </summary> /// <param name="bits">Number of bits the resulting prime should have</param> /// <returns>Generated prime</returns> public static BigNumber GeneratePrime(int bits) { IntPtr res = OpenSSL.BN_new(); OpenSSL.BN_generate_prime(res, bits, Convert.ToInt32(true), IntPtr.Zero, IntPtr.Zero, null, IntPtr.Zero); return(new BigNumber(res)); }
public BigNumber(string data, string format) { fBigNum = OpenSSL.BN_new(); if (format.ToLower() == "d") { OpenSSL.BN_dec2bn(ref fBigNum, Encoding.ASCII.GetBytes(data)); } else if (format.ToLower() == "x") { OpenSSL.BN_hex2bn(ref fBigNum, Encoding.ASCII.GetBytes(data)); } else { throw new FormatException(); } }
public static BigNumber operator -(BigNumber left, BigNumber right) { //Ensure that the BigNums have not been disposed if (left.fDisposed) { throw new ObjectDisposedException("left"); } if (right.fDisposed) { throw new ObjectDisposedException("right"); } //Actually do the operation IntPtr r = OpenSSL.BN_new(); OpenSSL.BN_sub(r, left.fBigNum, right.fBigNum); return(new BigNumber(r)); }
public static BigNumber operator /(BigNumber left, BigNumber right) { //Ensure that the BigNums have not been disposed if (left.fDisposed) { throw new ObjectDisposedException("left"); } if (right.fDisposed) { throw new ObjectDisposedException("right"); } //Actually do the operation IntPtr res = OpenSSL.BN_new(); IntPtr ctx = OpenSSL.BN_CTX_new(); OpenSSL.BN_div(res, IntPtr.Zero, left.fBigNum, right.fBigNum, ctx); OpenSSL.BN_CTX_free(ctx); return(new BigNumber(res)); }
/// <summary> /// Raises this BigNum to a power and takes the modulus /// </summary> /// <param name="exp">The exponent</param> /// <param name="mod">Value to use when taking the modulus</param> /// <returns>this**exp % mod</returns> public BigNumber PowMod(BigNumber exp, BigNumber mod) { if (fDisposed) { throw new ObjectDisposedException("this"); } if (exp.fDisposed) { throw new ObjectDisposedException("exp"); } if (mod.fDisposed) { throw new ObjectDisposedException("mod"); } IntPtr r = OpenSSL.BN_new(); IntPtr ctx = OpenSSL.BN_CTX_new(); OpenSSL.BN_mod_exp(r, fBigNum, exp.fBigNum, mod.fBigNum, ctx); OpenSSL.BN_CTX_free(ctx); return(new BigNumber(r)); }
/// <summary> /// Creates a new BigNum instance from a decimal string /// </summary> /// <param name="data">Number in decimal</param> public BigNumber(string data) { fBigNum = OpenSSL.BN_new(); OpenSSL.BN_dec2bn(ref fBigNum, Encoding.ASCII.GetBytes(data)); }
public BigNumber(int data) { fBigNum = OpenSSL.BN_new(); OpenSSL.BN_dec2bn(ref fBigNum, Encoding.ASCII.GetBytes(Convert.ToString(data))); }
public BigNumber() { fBigNum = OpenSSL.BN_new(); }