/// <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 string ToString(string format) { if (fDisposed) { throw new ObjectDisposedException("this"); } IntPtr str = IntPtr.Zero; if (format.ToLower() == "d") { str = OpenSSL.BN_bn2dec(fBigNum); } else if (format.ToLower() == "x") { str = OpenSSL.BN_bn2hex(fBigNum); } else { throw new FormatException(); } string res = Marshal.PtrToStringAnsi(str); OpenSSL.CRYPTO_free(str); return(res); }
/// <summary> /// Generates a random BigNum /// </summary> /// <param name="bits">How many bits the generated random number shoud have</param> /// <returns>Generated random number</returns> public static BigNumber Random(int bits) { RNG.Seed(); BigNumber rand = new BigNumber(); OpenSSL.BN_rand(rand.fBigNum, bits, 1, 1); return(rand); }
public static void Cleanup() { if (fSeeded) { OpenSSL.RAND_cleanup(); } fSeeded = false; }
/// <summary> /// Explicitly frees resources consumed by this BigNum instance /// </summary> public void Dispose() { if (!fDisposed) { GC.SuppressFinalize(this); OpenSSL.BN_free(fBigNum); fDisposed = true; } }
/// <summary> /// Clears the BigNum from memory /// </summary> public void Clear() { if (fDisposed) { throw new ObjectDisposedException("this"); } OpenSSL.BN_clear(fBigNum); }
/// <summary> /// Replaces the content of the BigNum with a random BigNum /// </summary> /// <param name="bn">BigNum to replace</param> /// <param name="bits">How many bits the generated random number shoud have</param> public static void Random(BigNumber bn, int bits) { if (bn.fDisposed) { throw new ObjectDisposedException("BigNum"); } RNG.Seed(); OpenSSL.BN_rand(bn.fBigNum, bits, 1, 1); }
/// <summary> /// Returns the BigNum as a buffer /// </summary> /// <returns>Content of BigNum as byte array in OpenSSL byte order</returns> public byte[] ToBigArray() { if (fDisposed) { throw new ObjectDisposedException("this"); } byte[] buf = new byte[(OpenSSL.BN_num_bits(fBigNum) + 7) / 8]; OpenSSL.BN_bn2bin(fBigNum, buf); return(buf); }
/// <summary> /// Seeds the Pseudo RNG with content from a file /// </summary> /// <param name="file">File to read</param> /// <param name="max_bytes">Number of bytes to read (-1 for the full contents)</param> /// <returns>Number of bytes actually read</returns> public static int LoadFile(string file, int max_bytes) { if (File.Exists(file)) { int read = OpenSSL.RAND_load_file(Encoding.ASCII.GetBytes(file), max_bytes); fSeeded = true; return(read); } else { throw new IOException("The file does not exist."); } }
public static bool 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"); } return(OpenSSL.BN_cmp(left.fBigNum, right.fBigNum) == -1); }
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 byte[] Random(int bytes) { if (!fSeeded) { Seed(); } byte[] buf = new byte[bytes]; if (OpenSSL.RAND_bytes(buf, bytes) == 1) { return(buf); } else { uint err = OpenSSL.ERR_get_error(); throw new OpenSSLException(Encoding.ASCII.GetString(OpenSSL.ERR_get_error_string(err, null))); } }
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)); }
/// <summary> /// Compares the values of two BigNums /// </summary> /// <param name="obj">BigNum to compare with</param> /// <returns>If the two values are equal</returns> public override bool Equals(object obj) { if (!(obj is BigNumber)) { throw new ArgumentException("obj must be a BigNum to compare"); } BigNumber cmp = obj as BigNumber; //Ensure that the BigNums have not been disposed if (fDisposed) { throw new ObjectDisposedException("this"); } if (cmp.fDisposed) { throw new ObjectDisposedException("cmp"); } return(OpenSSL.BN_cmp(fBigNum, cmp.fBigNum) == 0); }
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 an array of bytes /// </summary> /// <param name="data">An array of bytes in OpenSSL byte order (big endian)</param> public BigNumber(byte[] data) { fBigNum = OpenSSL.BN_bin2bn(data, data.Length, IntPtr.Zero); }
public BigNumber() { fBigNum = OpenSSL.BN_new(); }
/// <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)); }
/// <summary> /// Adds the content of the screen to the Pseudo RNG /// </summary> public static void Screen() { OpenSSL.RAND_screen(); fSeeded = true; }
public BigNumber(int data) { fBigNum = OpenSSL.BN_new(); OpenSSL.BN_dec2bn(ref fBigNum, Encoding.ASCII.GetBytes(Convert.ToString(data))); }
/// <summary> /// Seeds the RNG with user data /// </summary> /// <param name="data">Data to seed the RNG with</param> public static void Seed(byte[] data) { OpenSSL.RAND_seed(data, data.Length); fSeeded = true; }